Delete pages/7_record.py
Browse files- pages/7_record.py +0 -105
pages/7_record.py
DELETED
@@ -1,105 +0,0 @@
|
|
1 |
-
import uuid
|
2 |
-
from pathlib import Path
|
3 |
-
|
4 |
-
import cv2
|
5 |
-
import streamlit as st
|
6 |
-
from aiortc.contrib.media import MediaRecorder
|
7 |
-
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
8 |
-
|
9 |
-
from sample_utils.turn import get_ice_servers
|
10 |
-
from cvzone.HandTrackingModule import HandDetector
|
11 |
-
import numpy as np
|
12 |
-
|
13 |
-
RECORD_DIR = Path("./records")
|
14 |
-
RECORD_DIR.mkdir(exist_ok=True)
|
15 |
-
|
16 |
-
# Define virtual keyboard layout
|
17 |
-
keys = [
|
18 |
-
["Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"],
|
19 |
-
["A", "S", "D", "F", "G", "H", "J", "K", "L", ";"],
|
20 |
-
["Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"],
|
21 |
-
]
|
22 |
-
|
23 |
-
# Class for keyboard buttons
|
24 |
-
class Button:
|
25 |
-
def __init__(self, pos, text, size=(100, 100)):
|
26 |
-
self.pos = pos
|
27 |
-
self.size = size
|
28 |
-
self.text = text
|
29 |
-
|
30 |
-
def draw(self, img):
|
31 |
-
x, y = self.pos
|
32 |
-
w, h = self.size
|
33 |
-
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), cv2.FILLED)
|
34 |
-
cv2.putText(img, self.text, (x + 20, y + 70), cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255), 3)
|
35 |
-
|
36 |
-
# Initialize buttons
|
37 |
-
button_list = []
|
38 |
-
for i, row in enumerate(keys):
|
39 |
-
for j, key in enumerate(row):
|
40 |
-
button_list.append(Button((j * 105 + 50, i * 120 + 50), key))
|
41 |
-
|
42 |
-
|
43 |
-
def video_frame_callback(frame):
|
44 |
-
"""
|
45 |
-
Process video frames: Overlay a virtual keyboard on the video.
|
46 |
-
"""
|
47 |
-
img = frame.to_ndarray(format="bgr24")
|
48 |
-
|
49 |
-
# Draw virtual keyboard on the frame
|
50 |
-
for button in button_list:
|
51 |
-
button.draw(img)
|
52 |
-
|
53 |
-
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
54 |
-
|
55 |
-
|
56 |
-
def virtual_keyboard_app():
|
57 |
-
"""
|
58 |
-
Streamlit application for displaying a virtual keyboard.
|
59 |
-
"""
|
60 |
-
if "prefix" not in st.session_state:
|
61 |
-
st.session_state["prefix"] = str(uuid.uuid4())
|
62 |
-
prefix = st.session_state["prefix"]
|
63 |
-
|
64 |
-
# Define input and output file paths
|
65 |
-
in_file = RECORD_DIR / f"{prefix}_input.flv"
|
66 |
-
out_file = RECORD_DIR / f"{prefix}_output.flv"
|
67 |
-
|
68 |
-
# Factory for creating MediaRecorder objects
|
69 |
-
def in_recorder_factory() -> MediaRecorder:
|
70 |
-
return MediaRecorder(
|
71 |
-
str(in_file), format="flv"
|
72 |
-
)
|
73 |
-
|
74 |
-
def out_recorder_factory() -> MediaRecorder:
|
75 |
-
return MediaRecorder(str(out_file), format="flv")
|
76 |
-
|
77 |
-
# Initialize WebRTC streamer
|
78 |
-
webrtc_streamer(
|
79 |
-
key="record",
|
80 |
-
mode=WebRtcMode.SENDRECV,
|
81 |
-
rtc_configuration={"iceServers": get_ice_servers()},
|
82 |
-
media_stream_constraints={
|
83 |
-
"video": True,
|
84 |
-
"audio": True,
|
85 |
-
},
|
86 |
-
video_frame_callback=video_frame_callback,
|
87 |
-
in_recorder_factory=in_recorder_factory,
|
88 |
-
out_recorder_factory=out_recorder_factory,
|
89 |
-
)
|
90 |
-
|
91 |
-
# Display download buttons for recorded files
|
92 |
-
if in_file.exists():
|
93 |
-
with in_file.open("rb") as f:
|
94 |
-
st.download_button(
|
95 |
-
"Download the recorded video without video filter", f, "input.flv"
|
96 |
-
)
|
97 |
-
if out_file.exists():
|
98 |
-
with out_file.open("rb") as f:
|
99 |
-
st.download_button(
|
100 |
-
"Download the recorded video with virtual keyboard", f, "output.flv"
|
101 |
-
)
|
102 |
-
|
103 |
-
|
104 |
-
if __name__ == "__main__":
|
105 |
-
virtual_keyboard_app()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|