Spaces:
Sleeping
Sleeping
import streamlit as st | |
import cv2 | |
import numpy as np | |
def main(): | |
st.title("Camera Access with Streamlit") | |
# Create a placeholder for the video feed | |
stframe = st.empty() | |
# Start the camera when the button is clicked | |
if st.button('Start'): | |
st.write("Starting the camera...") | |
# Attempt to open the system camera | |
cap = cv2.VideoCapture(0) | |
if not cap.isOpened(): | |
st.error("Failed to open the camera. Please check your camera settings and permissions.") | |
return | |
while True: | |
ret, frame = cap.read() | |
if not ret: | |
st.error("Failed to grab frame from the camera.") | |
break | |
# Flip the frame horizontally for natural interaction | |
frame = cv2.flip(frame, 1) | |
# Convert the frame to RGB | |
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) | |
# Display the frame in the Streamlit app | |
stframe.image(frame_rgb, channels="RGB") | |
# Break the loop if the stop button is pressed | |
if st.button('Stop'): | |
st.write("Stopping the camera...") | |
break | |
# Release the camera resource | |
cap.release() | |
cv2.destroyAllWindows() | |
if __name__ == "__main__": | |
main() | |