-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
app_videochat.py
203 lines (159 loc) · 6.31 KB
/
app_videochat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import logging
import math
from typing import List
try:
from typing import Literal
except ImportError:
from typing_extensions import Literal # type: ignore
import av
import cv2
import numpy as np
import streamlit as st
from streamlit_server_state import server_state, server_state_lock
from streamlit_webrtc import (
VideoProcessorBase,
WebRtcMode,
WebRtcStreamerContext,
create_mix_track,
create_process_track,
webrtc_streamer,
)
logger = logging.getLogger(__name__)
class OpenCVVideoProcessor(VideoProcessorBase):
type: Literal["noop", "cartoon", "edges", "rotate"]
def __init__(self) -> None:
self.type = "noop"
def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
img = frame.to_ndarray(format="bgr24")
if self.type == "noop":
pass
elif self.type == "cartoon":
# prepare color
img_color = cv2.pyrDown(cv2.pyrDown(img))
for _ in range(6):
img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
img_color = cv2.pyrUp(cv2.pyrUp(img_color))
# prepare edges
img_edges = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img_edges = cv2.adaptiveThreshold(
cv2.medianBlur(img_edges, 7),
255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
9,
2,
)
img_edges = cv2.cvtColor(img_edges, cv2.COLOR_GRAY2RGB)
# combine color and edges
img = cv2.bitwise_and(img_color, img_edges)
elif self.type == "edges":
# perform edge detection
img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)
elif self.type == "rotate":
# rotate image
rows, cols, _ = img.shape
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), frame.time * 45, 1)
img = cv2.warpAffine(img, M, (cols, rows))
return av.VideoFrame.from_ndarray(img, format="bgr24")
def mixer_callback(frames: List[av.VideoFrame]) -> av.VideoFrame:
buf_w = 640
buf_h = 480
buffer = np.zeros((buf_h, buf_w, 3), dtype=np.uint8)
n_inputs = len(frames)
n_cols = math.ceil(math.sqrt(n_inputs))
n_rows = math.ceil(n_inputs / n_cols)
grid_w = buf_w // n_cols
grid_h = buf_h // n_rows
for i in range(n_inputs):
frame = frames[i]
if frame is None:
continue
grid_x = (i % n_cols) * grid_w
grid_y = (i // n_cols) * grid_h
img = frame.to_ndarray(format="bgr24")
src_h, src_w = img.shape[0:2]
aspect_ratio = src_w / src_h
window_w = min(grid_w, int(grid_h * aspect_ratio))
window_h = min(grid_h, int(window_w / aspect_ratio))
window_offset_x = (grid_w - window_w) // 2
window_offset_y = (grid_h - window_h) // 2
window_x0 = grid_x + window_offset_x
window_y0 = grid_y + window_offset_y
window_x1 = window_x0 + window_w
window_y1 = window_y0 + window_h
buffer[window_y0:window_y1, window_x0:window_x1, :] = cv2.resize(
img, (window_w, window_h)
)
new_frame = av.VideoFrame.from_ndarray(buffer, format="bgr24")
return new_frame
def main():
with server_state_lock["webrtc_contexts"]:
if "webrtc_contexts" not in server_state:
server_state["webrtc_contexts"] = []
with server_state_lock["mix_track"]:
if "mix_track" not in server_state:
server_state["mix_track"] = create_mix_track(
kind="video", mixer_callback=mixer_callback, key="mix"
)
mix_track = server_state["mix_track"]
self_ctx = webrtc_streamer(
key="self",
mode=WebRtcMode.SENDRECV,
rtc_configuration={"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]},
media_stream_constraints={"video": True, "audio": True},
source_video_track=mix_track,
sendback_audio=False,
)
self_process_track = None
if self_ctx.input_video_track:
self_process_track = create_process_track(
input_track=self_ctx.input_video_track,
processor_factory=OpenCVVideoProcessor,
)
mix_track.add_input_track(self_process_track)
self_process_track.processor.type = st.radio(
"Select transform type",
("noop", "cartoon", "edges", "rotate"),
key="filter1-type",
)
with server_state_lock["webrtc_contexts"]:
webrtc_contexts: List[WebRtcStreamerContext] = server_state["webrtc_contexts"]
self_is_playing = self_ctx.state.playing and self_process_track
if self_is_playing and self_ctx not in webrtc_contexts:
webrtc_contexts.append(self_ctx)
server_state["webrtc_contexts"] = webrtc_contexts
elif not self_is_playing and self_ctx in webrtc_contexts:
webrtc_contexts.remove(self_ctx)
server_state["webrtc_contexts"] = webrtc_contexts
if self_ctx.state.playing:
# Audio streams are transferred in SFU manner
# TODO: Create MCU to mix audio streams
for ctx in webrtc_contexts:
if ctx == self_ctx or not ctx.state.playing:
continue
webrtc_streamer(
key=f"sound-{id(ctx)}",
mode=WebRtcMode.RECVONLY,
rtc_configuration={
"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]
},
media_stream_constraints={"video": False, "audio": True},
source_audio_track=ctx.input_audio_track,
desired_playing_state=ctx.state.playing,
)
if __name__ == "__main__":
import os
DEBUG = os.environ.get("DEBUG", "false").lower() not in ["false", "no", "0"]
logging.basicConfig(
format="[%(asctime)s] %(levelname)7s from %(name)s in %(pathname)s:%(lineno)d: "
"%(message)s",
force=True,
)
logger.setLevel(level=logging.DEBUG if DEBUG else logging.INFO)
st_webrtc_logger = logging.getLogger("streamlit_webrtc")
st_webrtc_logger.setLevel(logging.DEBUG if DEBUG else logging.INFO)
aioice_logger = logging.getLogger("aioice")
aioice_logger.setLevel(logging.WARNING)
fsevents_logger = logging.getLogger("fsevents")
fsevents_logger.setLevel(logging.WARNING)
main()