Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,62 @@
|
|
1 |
import cv2
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
|
5 |
-
|
6 |
-
ret, frame = cap.read()
|
7 |
-
if not ret:
|
8 |
-
break
|
9 |
-
cv2.imshow('Camera Stream', frame)
|
10 |
-
if cv2.waitKey(1) == ord('q'):
|
11 |
-
break
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
+
import streamlit as st
|
3 |
+
from streamlit_webrtc import webrtc_streamer, VideoHTMLAttributes
|
4 |
+
import numpy as np
|
5 |
+
import av
|
6 |
|
7 |
+
st.title("OpenCV Filters on Video Stream")
|
8 |
|
9 |
+
filter = "none"
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
|
12 |
+
def transform(frame: av.VideoFrame):
|
13 |
+
img = frame.to_ndarray(format="bgr24")
|
14 |
+
|
15 |
+
if filter == "blur":
|
16 |
+
img = cv2.GaussianBlur(img, (21, 21), 0)
|
17 |
+
elif filter == "canny":
|
18 |
+
img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)
|
19 |
+
elif filter == "grayscale":
|
20 |
+
# We convert the image twice because the first conversion returns a 2D array.
|
21 |
+
# the second conversion turns it back to a 3D array.
|
22 |
+
img = cv2.cvtColor(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR)
|
23 |
+
elif filter == "sepia":
|
24 |
+
kernel = np.array(
|
25 |
+
[[0.272, 0.534, 0.131], [0.349, 0.686, 0.168], [0.393, 0.769, 0.189]]
|
26 |
+
)
|
27 |
+
img = cv2.transform(img, kernel)
|
28 |
+
elif filter == "invert":
|
29 |
+
img = cv2.bitwise_not(img)
|
30 |
+
elif filter == "none":
|
31 |
+
pass
|
32 |
+
|
33 |
+
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
34 |
+
|
35 |
+
|
36 |
+
col1, col2, col3, col4, col5, col6 = st.columns([1, 1, 1, 1, 1, 1])
|
37 |
+
|
38 |
+
with col1:
|
39 |
+
if st.button("None"):
|
40 |
+
filter = "none"
|
41 |
+
with col2:
|
42 |
+
if st.button("Blur"):
|
43 |
+
filter = "blur"
|
44 |
+
with col3:
|
45 |
+
if st.button("Grayscale"):
|
46 |
+
filter = "grayscale"
|
47 |
+
with col4:
|
48 |
+
if st.button("Sepia"):
|
49 |
+
filter = "sepia"
|
50 |
+
with col5:
|
51 |
+
if st.button("Canny"):
|
52 |
+
filter = "canny"
|
53 |
+
with col6:
|
54 |
+
if st.button("Invert"):
|
55 |
+
filter = "invert"
|
56 |
+
|
57 |
+
|
58 |
+
webrtc_streamer(
|
59 |
+
key="streamer",
|
60 |
+
video_frame_callback=transform,
|
61 |
+
sendback_audio=False
|
62 |
+
)
|