File size: 1,332 Bytes
2188b23
 
 
 
 
afcaca0
2188b23
5f0d39d
 
 
 
0ed1226
879dd2c
 
 
0ed1226
879dd2c
9272026
879dd2c
9272026
879dd2c
afcaca0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9272026
 
0ed1226
9272026
2188b23
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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()