Raumkommander commited on
Commit
c7f4ab1
·
verified ·
1 Parent(s): c803588

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -8,34 +8,33 @@ st.title("Live Webcam Stream - Original and Flipped")
8
  # Start webcam capture
9
  cap = cv2.VideoCapture(0)
10
 
11
- # Create two columns to display the original and flipped video streams
12
- col1, col2 = st.columns(2)
13
- original_placeholder = col1.empty()
14
- flipped_placeholder = col2.empty()
 
 
 
 
15
 
16
- # Stream the video
17
- while True:
18
- success, frame = cap.read()
19
- if not success:
20
- st.error("Failed to capture image")
21
- break
22
-
23
- # Convert original frame to RGB format
24
- original_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
25
- original_img = Image.fromarray(original_frame)
26
-
27
- # Flip the frame horizontally
28
- flipped_frame = cv2.flip(original_frame, 1)
29
- flipped_img = Image.fromarray(flipped_frame)
30
-
31
- # Display both original and flipped frames in their respective columns
32
- original_placeholder.image(original_img, caption="Original Video Stream", use_column_width=True)
33
- flipped_placeholder.image(flipped_img, caption="Flipped Video Stream", use_column_width=True)
34
 
35
  # Stop streaming if the user presses the button
36
  stop_button = st.button("Stop Streaming")
37
  if stop_button:
38
- break
 
39
 
40
- cap.release()
41
- st.write("Stream stopped.")
 
8
  # Start webcam capture
9
  cap = cv2.VideoCapture(0)
10
 
11
+ # Check if the webcam is opened successfully
12
+ if not cap.isOpened():
13
+ st.error("Failed to access the webcam. Please check your device.")
14
+ else:
15
+ # Create two columns to display the original and flipped video streams
16
+ col1, col2 = st.columns(2)
17
+ original_placeholder = col1.empty()
18
+ flipped_placeholder = col2.empty()
19
 
20
+ # Capture and display the webcam feed frame by frame
21
+ frame = cap.read()[1] # Capture the first frame
22
+ if frame is not None:
23
+ # Convert original frame to RGB format
24
+ original_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
25
+ original_img = Image.fromarray(original_frame)
26
+
27
+ # Flip the frame horizontally
28
+ flipped_frame = cv2.flip(original_frame, 1)
29
+ flipped_img = Image.fromarray(flipped_frame)
30
+
31
+ # Display both original and flipped frames in their respective columns
32
+ original_placeholder.image(original_img, caption="Original Video Stream", use_column_width=True)
33
+ flipped_placeholder.image(flipped_img, caption="Flipped Video Stream", use_column_width=True)
 
 
 
 
34
 
35
  # Stop streaming if the user presses the button
36
  stop_button = st.button("Stop Streaming")
37
  if stop_button:
38
+ cap.release()
39
+ st.write("Stream stopped.")
40