AndrewLam489 commited on
Commit
150a67b
·
verified ·
1 Parent(s): 2085218

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -24
app.py CHANGED
@@ -1,27 +1,24 @@
1
  import streamlit as st
2
- from streamlit_webrtc import webrtc_streamer, WebRtcMode
3
- import av
4
 
5
- st.title("Webcam Display Streamlit App")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- # Define the callback for transforming frames (without applying any filters)
8
- def transform(frame: av.VideoFrame):
9
- img = frame.to_ndarray(format="bgr24") # Convert to NumPy array (BGR format)
10
-
11
- # Simply return the image without applying any filters
12
- return av.VideoFrame.from_ndarray(img, format="bgr24")
13
-
14
- # Streamlit buttons (optional, to stop the stream or interact further)
15
- stop_button_pressed = st.button("Stop")
16
-
17
- # Display the video stream
18
- webrtc_streamer(
19
- key="streamer",
20
- video_frame_callback=transform, # The transform function is only used to process frames
21
- sendback_audio=False, # We don't need audio in this case
22
- mode=WebRtcMode.RECVONLY, # We are only receiving the video stream (not sending any video back)
23
- )
24
-
25
- # If you want a "Stop" button that halts the webcam stream, you can handle this through Streamlit
26
- if stop_button_pressed:
27
- st.write("Stream stopped.")
 
1
  import streamlit as st
2
+ import cv2
 
3
 
4
+ def main():
5
+ st.set_page_config(page_title="Streamlit WebCam App")
6
+ st.title("Webcam Display Steamlit App")
7
+ st.caption("Powered by OpenCV, Streamlit")
8
+ cap = cv2.VideoCapture(0)
9
+ frame_placeholder = st.empty()
10
+ stop_button_pressed = st.button("Stop")
11
+ while cap.isOpened() and not stop_button_pressed:
12
+ ret, frame = cap.read()
13
+ if not ret:
14
+ st.write("Video Capture Ended")
15
+ break
16
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
17
+ frame_placeholder.image(frame,channels="RGB")
18
+ if cv2.waitKey(1) & 0xFF == ord("q") or stop_button_pressed:
19
+ break
20
+ cap.release()
21
+ cv2.destroyAllWindows()
22
 
23
+ if __name__ == "__main__":
24
+ main()