Delete pages/5_fork_multi_outputs.py
Browse files- pages/5_fork_multi_outputs.py +0 -107
pages/5_fork_multi_outputs.py
DELETED
@@ -1,107 +0,0 @@
|
|
1 |
-
try:
|
2 |
-
from typing import Literal
|
3 |
-
except ImportError:
|
4 |
-
from typing_extensions import Literal # type: ignore
|
5 |
-
|
6 |
-
from typing import cast
|
7 |
-
|
8 |
-
import av
|
9 |
-
import cv2
|
10 |
-
import streamlit as st
|
11 |
-
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
12 |
-
|
13 |
-
from sample_utils.turn import get_ice_servers
|
14 |
-
|
15 |
-
st.markdown(
|
16 |
-
"""
|
17 |
-
Fork one input to multiple outputs with different video filters.
|
18 |
-
"""
|
19 |
-
)
|
20 |
-
|
21 |
-
VideoFilterType = Literal["noop", "cartoon", "edges", "rotate"]
|
22 |
-
|
23 |
-
|
24 |
-
def make_video_frame_callback(_type: VideoFilterType):
|
25 |
-
def callback(frame: av.VideoFrame) -> av.VideoFrame:
|
26 |
-
img = frame.to_ndarray(format="bgr24")
|
27 |
-
|
28 |
-
if _type == "noop":
|
29 |
-
pass
|
30 |
-
elif _type == "cartoon":
|
31 |
-
# prepare color
|
32 |
-
img_color = cv2.pyrDown(cv2.pyrDown(img))
|
33 |
-
for _ in range(6):
|
34 |
-
img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
|
35 |
-
img_color = cv2.pyrUp(cv2.pyrUp(img_color))
|
36 |
-
|
37 |
-
# prepare edges
|
38 |
-
img_edges = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
39 |
-
img_edges = cv2.adaptiveThreshold(
|
40 |
-
cv2.medianBlur(img_edges, 7),
|
41 |
-
255,
|
42 |
-
cv2.ADAPTIVE_THRESH_MEAN_C,
|
43 |
-
cv2.THRESH_BINARY,
|
44 |
-
9,
|
45 |
-
2,
|
46 |
-
)
|
47 |
-
img_edges = cv2.cvtColor(img_edges, cv2.COLOR_GRAY2RGB)
|
48 |
-
|
49 |
-
# combine color and edges
|
50 |
-
img = cv2.bitwise_and(img_color, img_edges)
|
51 |
-
elif _type == "edges":
|
52 |
-
# perform edge detection
|
53 |
-
img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)
|
54 |
-
elif _type == "rotate":
|
55 |
-
# rotate image
|
56 |
-
rows, cols, _ = img.shape
|
57 |
-
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), frame.time * 45, 1)
|
58 |
-
img = cv2.warpAffine(img, M, (cols, rows))
|
59 |
-
|
60 |
-
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
61 |
-
|
62 |
-
return callback
|
63 |
-
|
64 |
-
|
65 |
-
COMMON_RTC_CONFIG = {"iceServers": get_ice_servers()}
|
66 |
-
|
67 |
-
st.header("Input")
|
68 |
-
ctx = webrtc_streamer(
|
69 |
-
key="loopback",
|
70 |
-
mode=WebRtcMode.SENDRECV,
|
71 |
-
rtc_configuration=COMMON_RTC_CONFIG,
|
72 |
-
media_stream_constraints={"video": True, "audio": False},
|
73 |
-
)
|
74 |
-
|
75 |
-
st.header("Forked output 1")
|
76 |
-
filter1_type = st.radio(
|
77 |
-
"Select transform type",
|
78 |
-
("noop", "cartoon", "edges", "rotate"),
|
79 |
-
key="fork-filter1-type",
|
80 |
-
)
|
81 |
-
callback = make_video_frame_callback(cast(VideoFilterType, filter1_type))
|
82 |
-
webrtc_streamer(
|
83 |
-
key="filter1",
|
84 |
-
mode=WebRtcMode.RECVONLY,
|
85 |
-
video_frame_callback=callback,
|
86 |
-
source_video_track=ctx.output_video_track,
|
87 |
-
desired_playing_state=ctx.state.playing,
|
88 |
-
rtc_configuration=COMMON_RTC_CONFIG,
|
89 |
-
media_stream_constraints={"video": True, "audio": False},
|
90 |
-
)
|
91 |
-
|
92 |
-
st.header("Forked output 2")
|
93 |
-
filter2_type = st.radio(
|
94 |
-
"Select transform type",
|
95 |
-
("noop", "cartoon", "edges", "rotate"),
|
96 |
-
key="fork-filter2-type",
|
97 |
-
)
|
98 |
-
callback = make_video_frame_callback(cast(VideoFilterType, filter2_type))
|
99 |
-
webrtc_streamer(
|
100 |
-
key="filter2",
|
101 |
-
mode=WebRtcMode.RECVONLY,
|
102 |
-
video_frame_callback=callback,
|
103 |
-
source_video_track=ctx.output_video_track,
|
104 |
-
desired_playing_state=ctx.state.playing,
|
105 |
-
rtc_configuration=COMMON_RTC_CONFIG,
|
106 |
-
media_stream_constraints={"video": True, "audio": False},
|
107 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|