Add pages/14_programmable_source.py
Browse files
pages/14_programmable_source.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fractions
|
2 |
+
import time
|
3 |
+
|
4 |
+
import av
|
5 |
+
import cv2
|
6 |
+
import numpy as np
|
7 |
+
import streamlit as st
|
8 |
+
from streamlit_webrtc import WebRtcMode, create_video_source_track, webrtc_streamer
|
9 |
+
|
10 |
+
thickness = st.slider("thickness", 1, 10, 3, 1)
|
11 |
+
|
12 |
+
|
13 |
+
def video_source_callback(pts: int, time_base: fractions.Fraction) -> av.VideoFrame:
|
14 |
+
pts_sec = pts * time_base
|
15 |
+
|
16 |
+
buffer = np.zeros((480, 640, 3), dtype=np.uint8)
|
17 |
+
buffer = cv2.putText(
|
18 |
+
buffer,
|
19 |
+
text=f"time: {time.time():.2f}",
|
20 |
+
org=(0, 32),
|
21 |
+
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
|
22 |
+
fontScale=1.0,
|
23 |
+
color=(255, 255, 0),
|
24 |
+
thickness=thickness,
|
25 |
+
lineType=cv2.LINE_4,
|
26 |
+
)
|
27 |
+
buffer = cv2.putText(
|
28 |
+
buffer,
|
29 |
+
text=f"pts: {pts} ({float(pts_sec):.2f} sec)",
|
30 |
+
org=(0, 64),
|
31 |
+
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
|
32 |
+
fontScale=1.0,
|
33 |
+
color=(255, 255, 0),
|
34 |
+
thickness=thickness,
|
35 |
+
lineType=cv2.LINE_4,
|
36 |
+
)
|
37 |
+
return av.VideoFrame.from_ndarray(buffer, format="bgr24")
|
38 |
+
|
39 |
+
|
40 |
+
fps = st.slider("fps", 1, 30, 30, 1)
|
41 |
+
|
42 |
+
|
43 |
+
video_source_track = create_video_source_track(
|
44 |
+
video_source_callback, key="video_source_track", fps=fps
|
45 |
+
)
|
46 |
+
|
47 |
+
|
48 |
+
def on_change():
|
49 |
+
ctx = st.session_state["player"]
|
50 |
+
stopped = not ctx.state.playing and not ctx.state.signalling
|
51 |
+
if stopped:
|
52 |
+
video_source_track.stop() # Manually stop the track.
|
53 |
+
|
54 |
+
|
55 |
+
webrtc_streamer(
|
56 |
+
key="player",
|
57 |
+
mode=WebRtcMode.RECVONLY,
|
58 |
+
source_video_track=video_source_track,
|
59 |
+
media_stream_constraints={"video": True, "audio": False},
|
60 |
+
on_change=on_change,
|
61 |
+
)
|