Raumkommander commited on
Commit
dbd9537
·
verified ·
1 Parent(s): 32cff9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -38
app.py CHANGED
@@ -1,44 +1,20 @@
1
- import cv2
2
  import streamlit as st
 
3
  import numpy as np
4
- from PIL import Image
5
-
6
- st.title("Live Webcam Stream - Original and Flipped")
7
-
8
- # Try accessing the webcam (with different indexes)
9
- cap = cv2.VideoCapture(0) # First try index 0
10
- if not cap.isOpened():
11
- st.warning("Webcam not found. Trying with index 1.")
12
- cap = cv2.VideoCapture(1) # Try index 1 if index 0 fails
13
- if not cap.isOpened():
14
- st.error("Failed to access the webcam. Please check your device or permissions.")
15
- else:
16
- st.write("Webcam accessed using index 1.")
17
-
18
- # If camera is available, proceed with the processing
19
- if cap.isOpened():
20
- # Create two columns to display the original and flipped video streams
21
- col1, col2 = st.columns(2)
22
- original_placeholder = col1.empty()
23
- flipped_placeholder = col2.empty()
24
 
25
- # Capture and display the webcam feed frame by frame
26
- frame = cap.read()[1] # Capture the first frame
27
- if frame is not None:
28
- # Convert original frame to RGB format
29
- original_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
30
- original_img = Image.fromarray(original_frame)
31
 
32
- # Flip the frame horizontally
33
- flipped_frame = cv2.flip(original_frame, 1)
34
- flipped_img = Image.fromarray(flipped_frame)
35
 
36
- # Display both original and flipped frames in their respective columns
37
- original_placeholder.image(original_img, caption="Original Video Stream", use_column_width=True)
38
- flipped_placeholder.image(flipped_img, caption="Flipped Video Stream", use_column_width=True)
39
 
40
- # Stop streaming if the user presses the button
41
- stop_button = st.button("Stop Streaming")
42
- if stop_button:
43
- cap.release()
44
- st.write("Stream stopped.")
 
 
1
  import streamlit as st
2
+ import cv2
3
  import numpy as np
4
+ from streamlit_webrtc import webrtc_streamer, VideoTransformerBase
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ class RotatedVideoTransformer(VideoTransformerBase):
7
+ def transform(self, frame):
8
+ img = frame.to_ndarray(format="bgr24") # Convert frame to NumPy array
9
+ rotated_img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) # Rotate 90 degrees
10
+ return rotated_img # Return the rotated frame
 
11
 
12
+ st.title("Live Webcam Streaming with Rotation")
 
 
13
 
14
+ # Original Stream
15
+ st.subheader("Original Video Stream")
16
+ webrtc_streamer(key="original")
17
 
18
+ # Rotated Stream
19
+ st.subheader("Rotated Video Stream")
20
+ webrtc_streamer(key="rotated", video_transformer_factory=RotatedVideoTransformer)