Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,64 @@
|
|
1 |
-
"""
|
2 |
-
from the browser to the server and to render frames via `st.image`."""
|
3 |
-
|
4 |
-
import logging
|
5 |
-
import queue
|
6 |
|
|
|
|
|
7 |
import streamlit as st
|
8 |
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
9 |
|
10 |
from sample_utils.turn import get_ice_servers
|
11 |
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
-
|
16 |
-
key="
|
17 |
-
mode=WebRtcMode.
|
18 |
rtc_configuration={"iceServers": get_ice_servers()},
|
19 |
-
|
|
|
|
|
20 |
)
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
video_frame = webrtc_ctx.video_receiver.get_frame(timeout=1)
|
28 |
-
except queue.Empty:
|
29 |
-
logger.warning("Queue is empty. Abort.")
|
30 |
-
break
|
31 |
-
|
32 |
-
img_rgb = video_frame.to_ndarray(format="rgb24")
|
33 |
-
image_place.image(img_rgb)
|
34 |
-
else:
|
35 |
-
logger.warning("AudioReciver is not set. Abort.")
|
36 |
-
break
|
|
|
1 |
+
"""Video transforms with OpenCV"""
|
|
|
|
|
|
|
|
|
2 |
|
3 |
+
import av
|
4 |
+
import cv2
|
5 |
import streamlit as st
|
6 |
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
7 |
|
8 |
from sample_utils.turn import get_ice_servers
|
9 |
|
10 |
+
_type = st.radio("Select transform type", ("noop", "cartoon", "edges", "rotate"))
|
11 |
+
|
12 |
+
|
13 |
+
def callback(frame: av.VideoFrame) -> av.VideoFrame:
|
14 |
+
img = frame.to_ndarray(format="bgr24")
|
15 |
+
|
16 |
+
if _type == "noop":
|
17 |
+
pass
|
18 |
+
elif _type == "cartoon":
|
19 |
+
# prepare color
|
20 |
+
img_color = cv2.pyrDown(cv2.pyrDown(img))
|
21 |
+
for _ in range(6):
|
22 |
+
img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
|
23 |
+
img_color = cv2.pyrUp(cv2.pyrUp(img_color))
|
24 |
+
|
25 |
+
# prepare edges
|
26 |
+
img_edges = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
27 |
+
img_edges = cv2.adaptiveThreshold(
|
28 |
+
cv2.medianBlur(img_edges, 7),
|
29 |
+
255,
|
30 |
+
cv2.ADAPTIVE_THRESH_MEAN_C,
|
31 |
+
cv2.THRESH_BINARY,
|
32 |
+
9,
|
33 |
+
2,
|
34 |
+
)
|
35 |
+
img_edges = cv2.cvtColor(img_edges, cv2.COLOR_GRAY2RGB)
|
36 |
+
|
37 |
+
# combine color and edges
|
38 |
+
img = cv2.bitwise_and(img_color, img_edges)
|
39 |
+
elif _type == "edges":
|
40 |
+
# perform edge detection
|
41 |
+
img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)
|
42 |
+
elif _type == "rotate":
|
43 |
+
# rotate image
|
44 |
+
rows, cols, _ = img.shape
|
45 |
+
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), frame.time * 45, 1)
|
46 |
+
img = cv2.warpAffine(img, M, (cols, rows))
|
47 |
+
|
48 |
+
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
49 |
|
50 |
|
51 |
+
webrtc_streamer(
|
52 |
+
key="opencv-filter",
|
53 |
+
mode=WebRtcMode.SENDRECV,
|
54 |
rtc_configuration={"iceServers": get_ice_servers()},
|
55 |
+
video_frame_callback=callback,
|
56 |
+
media_stream_constraints={"video": True, "audio": False},
|
57 |
+
async_processing=True,
|
58 |
)
|
59 |
|
60 |
+
st.markdown(
|
61 |
+
"This demo is based on "
|
62 |
+
"https://github.com/aiortc/aiortc/blob/2362e6d1f0c730a0f8c387bbea76546775ad2fe8/examples/server/server.py#L34. " # noqa: E501
|
63 |
+
"Many thanks to the project."
|
64 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|