Update pages/7_record.py
Browse files- pages/7_record.py +39 -9
pages/7_record.py
CHANGED
@@ -1,31 +1,61 @@
|
|
1 |
import uuid
|
2 |
from pathlib import Path
|
3 |
|
4 |
-
import av
|
5 |
import cv2
|
6 |
import streamlit as st
|
7 |
from aiortc.contrib.media import MediaRecorder
|
8 |
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
9 |
|
10 |
from sample_utils.turn import get_ice_servers
|
|
|
|
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
|
|
14 |
"""
|
15 |
-
Process video frames:
|
16 |
"""
|
17 |
img = frame.to_ndarray(format="bgr24")
|
18 |
-
img = cv2.cvtColor(cv2.Canny(img, 100, 200), cv2.COLOR_GRAY2BGR)
|
19 |
-
return av.VideoFrame.from_ndarray(img, format="bgr24")
|
20 |
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
RECORD_DIR.mkdir(exist_ok=True)
|
24 |
|
25 |
|
26 |
def virtual_keyboard_app():
|
27 |
"""
|
28 |
-
Streamlit application for
|
29 |
"""
|
30 |
if "prefix" not in st.session_state:
|
31 |
st.session_state["prefix"] = str(uuid.uuid4())
|
@@ -67,7 +97,7 @@ def virtual_keyboard_app():
|
|
67 |
if out_file.exists():
|
68 |
with out_file.open("rb") as f:
|
69 |
st.download_button(
|
70 |
-
"Download the recorded video with
|
71 |
)
|
72 |
|
73 |
|
|
|
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())
|
|
|
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 |
|