Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,20 @@
|
|
1 |
-
import cv2
|
2 |
import streamlit as st
|
|
|
3 |
import numpy as np
|
4 |
-
from
|
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 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
original_img = Image.fromarray(original_frame)
|
31 |
|
32 |
-
|
33 |
-
flipped_frame = cv2.flip(original_frame, 1)
|
34 |
-
flipped_img = Image.fromarray(flipped_frame)
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
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)
|
|
|
|