Delete pages/8_media_files_streaming.py
Browse files- pages/8_media_files_streaming.py +0 -129
pages/8_media_files_streaming.py
DELETED
@@ -1,129 +0,0 @@
|
|
1 |
-
"""Media streamings"""
|
2 |
-
import logging
|
3 |
-
from pathlib import Path
|
4 |
-
from typing import Dict, Optional, cast
|
5 |
-
|
6 |
-
import av
|
7 |
-
import cv2
|
8 |
-
import streamlit as st
|
9 |
-
from aiortc.contrib.media import MediaPlayer
|
10 |
-
from streamlit_webrtc import WebRtcMode, WebRtcStreamerContext, webrtc_streamer
|
11 |
-
|
12 |
-
from sample_utils.download import download_file
|
13 |
-
from sample_utils.turn import get_ice_servers
|
14 |
-
|
15 |
-
HERE = Path(__file__).parent
|
16 |
-
ROOT = HERE.parent
|
17 |
-
|
18 |
-
logger = logging.getLogger(__name__)
|
19 |
-
|
20 |
-
|
21 |
-
MEDIAFILES: Dict[str, Dict] = {
|
22 |
-
"big_buck_bunny_720p_2mb.mp4 (local)": {
|
23 |
-
"url": "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_2mb.mp4", # noqa: E501
|
24 |
-
"local_file_path": ROOT / "data/big_buck_bunny_720p_2mb.mp4",
|
25 |
-
"type": "video",
|
26 |
-
},
|
27 |
-
"big_buck_bunny_720p_10mb.mp4 (local)": {
|
28 |
-
"url": "https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_10mb.mp4", # noqa: E501
|
29 |
-
"local_file_path": ROOT / "data/big_buck_bunny_720p_10mb.mp4",
|
30 |
-
"type": "video",
|
31 |
-
},
|
32 |
-
"file_example_MP3_700KB.mp3 (local)": {
|
33 |
-
"url": "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_700KB.mp3", # noqa: E501
|
34 |
-
"local_file_path": ROOT / "data/file_example_MP3_700KB.mp3",
|
35 |
-
"type": "audio",
|
36 |
-
},
|
37 |
-
"file_example_MP3_5MG.mp3 (local)": {
|
38 |
-
"url": "https://file-examples-com.github.io/uploads/2017/11/file_example_MP3_5MG.mp3", # noqa: E501
|
39 |
-
"local_file_path": ROOT / "data/file_example_MP3_5MG.mp3",
|
40 |
-
"type": "audio",
|
41 |
-
},
|
42 |
-
"rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov": {
|
43 |
-
"url": "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mov",
|
44 |
-
"type": "video",
|
45 |
-
},
|
46 |
-
}
|
47 |
-
media_file_label = st.radio("Select a media source to stream", tuple(MEDIAFILES.keys()))
|
48 |
-
media_file_info = MEDIAFILES[cast(str, media_file_label)]
|
49 |
-
if "local_file_path" in media_file_info:
|
50 |
-
download_file(media_file_info["url"], media_file_info["local_file_path"])
|
51 |
-
|
52 |
-
|
53 |
-
def create_player():
|
54 |
-
if "local_file_path" in media_file_info:
|
55 |
-
return MediaPlayer(str(media_file_info["local_file_path"]))
|
56 |
-
else:
|
57 |
-
return MediaPlayer(media_file_info["url"])
|
58 |
-
|
59 |
-
# NOTE: To stream the video from webcam, use the code below.
|
60 |
-
# return MediaPlayer(
|
61 |
-
# "1:none",
|
62 |
-
# format="avfoundation",
|
63 |
-
# options={"framerate": "30", "video_size": "1280x720"},
|
64 |
-
# )
|
65 |
-
|
66 |
-
|
67 |
-
key = f"media-streaming-{media_file_label}"
|
68 |
-
ctx: Optional[WebRtcStreamerContext] = st.session_state.get(key)
|
69 |
-
if media_file_info["type"] == "video" and ctx and ctx.state.playing:
|
70 |
-
_type = st.radio("Select transform type", ("noop", "cartoon", "edges", "rotate"))
|
71 |
-
else:
|
72 |
-
_type = "noop"
|
73 |
-
|
74 |
-
|
75 |
-
def video_frame_callback(frame: av.VideoFrame) -> av.VideoFrame:
|
76 |
-
img = frame.to_ndarray(format="bgr24")
|
77 |
-
|
78 |
-
if _type == "noop":
|
79 |
-
pass
|
80 |
-
elif _type == "cartoon":
|
81 |
-
# prepare color
|
82 |
-
img_color = cv2.pyrDown(cv2.pyrDown(img))
|
83 |
-
for _ in range(6):
|
84 |
-
img_color = cv2.bilateralFilter(img_color, 9, 9, 7)
|
85 |
-
img_color = cv2.pyrUp(cv2.pyrUp(img_color))
|
86 |
-
|
87 |
-
# prepare edges
|
88 |
-
img_edges = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
|
89 |
-
img_edges = cv2.adaptiveThreshold(
|
90 |
-
cv2.medianBlur(img_edges, 7),
|
91 |
-
255,
|
92 |
-
cv2.ADAPTIVE_THRESH_MEAN_C,
|
93 |
-
cv2.THRESH_BINARY,
|
94 |
-
9,
|
95 |
-
2,
|
96 |
-
)
|
97 |
-
img_edges = cv2.cvtColor(img_edges, cv2.COLOR_GRAY2RGB)
|
98 |
-
|
99 |
-
# combine color and edges
|
100 |
-
img = cv2.bitwise_and(img_color, img_edges)
|
101 |
-
elif _type == "edges":
|
102 |
-
# perform edge detection
|
103 |
-
img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)
|
104 |
-
elif _type == "rotate":
|
105 |
-
# rotate image
|
106 |
-
rows, cols, _ = img.shape
|
107 |
-
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), frame.time * 45, 1)
|
108 |
-
img = cv2.warpAffine(img, M, (cols, rows))
|
109 |
-
|
110 |
-
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
111 |
-
|
112 |
-
|
113 |
-
webrtc_streamer(
|
114 |
-
key=key,
|
115 |
-
mode=WebRtcMode.RECVONLY,
|
116 |
-
rtc_configuration={"iceServers": get_ice_servers()},
|
117 |
-
media_stream_constraints={
|
118 |
-
"video": media_file_info["type"] == "video",
|
119 |
-
"audio": media_file_info["type"] == "audio",
|
120 |
-
},
|
121 |
-
player_factory=create_player,
|
122 |
-
video_frame_callback=video_frame_callback,
|
123 |
-
)
|
124 |
-
|
125 |
-
st.markdown(
|
126 |
-
"The video filter in this demo is based on "
|
127 |
-
"https://github.com/aiortc/aiortc/blob/2362e6d1f0c730a0f8c387bbea76546775ad2fe8/examples/server/server.py#L34. " # noqa: E501
|
128 |
-
"Many thanks to the project."
|
129 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|