Spaces:
Running
Running
Update pages/Video Upload.py
Browse files- pages/Video Upload.py +124 -2
pages/Video Upload.py
CHANGED
@@ -1,4 +1,72 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
st.markdown("""
|
3 |
<div class="upload-section">
|
4 |
<h3>🎥 Sign Language Recognition Demo</h3>
|
@@ -6,6 +74,60 @@
|
|
6 |
</div>
|
7 |
""", unsafe_allow_html=True)
|
8 |
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
|
|
|
1 |
+
import logging
|
2 |
+
import queue
|
3 |
+
from collections import deque
|
4 |
+
from concurrent.futures import ThreadPoolExecutor
|
5 |
+
|
6 |
+
import streamlit as st
|
7 |
+
import cv2
|
8 |
+
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
9 |
+
from model import Predictor
|
10 |
+
import openai
|
11 |
+
|
12 |
+
# Настройки
|
13 |
+
DEFAULT_WIDTH = 50
|
14 |
+
openai.api_key = 'sk-proj-GDxupB1DFvTTWBg38VyST3BlbkFJ7MdcACLwu3u0U1QvWeMb'
|
15 |
+
logger = logging.getLogger(__name__)
|
16 |
+
|
17 |
+
def correct_text_gpt3(input_text):
|
18 |
+
prompt = f"Исправь грамматические ошибки в тексте: '{input_text}'"
|
19 |
+
response = openai.ChatCompletion.create(
|
20 |
+
model="gpt-3.5-turbo",
|
21 |
+
messages=[
|
22 |
+
{"role": "system", "content": "You are a helpful assistant that corrects grammatical errors."},
|
23 |
+
{"role": "user", "content": prompt}
|
24 |
+
],
|
25 |
+
max_tokens=50,
|
26 |
+
n=1,
|
27 |
+
stop=None,
|
28 |
+
temperature=0.5,
|
29 |
+
)
|
30 |
+
return response.choices[0].message['content'].strip()
|
31 |
+
|
32 |
+
# Центрируем контент
|
33 |
+
width = 50
|
34 |
+
side = max((100 - width) / 1.2, 0.01)
|
35 |
+
_, container, _ = st.columns([side, width, side])
|
36 |
+
|
37 |
+
# Модель инференса
|
38 |
+
class SLInference:
|
39 |
+
def __init__(self, config_path):
|
40 |
+
self.config = self.load_config(config_path)
|
41 |
+
self.predictor = Predictor(self.config)
|
42 |
+
self.input_queue = deque(maxlen=32)
|
43 |
+
self.pred = ''
|
44 |
+
|
45 |
+
def load_config(self, config_path):
|
46 |
+
import json
|
47 |
+
with open(config_path, 'r') as f:
|
48 |
+
return json.load(f)
|
49 |
+
|
50 |
+
def start(self):
|
51 |
+
pass
|
52 |
+
|
53 |
+
def predict(self, frames):
|
54 |
+
frames_resized = [cv2.resize(frame, (224, 224)) for frame in frames]
|
55 |
+
while len(frames_resized) < 32:
|
56 |
+
frames_resized.append(frames_resized[-1])
|
57 |
+
result = self.predictor.predict(frames_resized)
|
58 |
+
if result:
|
59 |
+
return result["labels"][0]
|
60 |
+
return 'no'
|
61 |
+
|
62 |
+
def process_batch(inference_thread, frames, gestures):
|
63 |
+
gesture = inference_thread.predict(frames)
|
64 |
+
if gesture not in ['no', ''] and gesture not in gestures:
|
65 |
+
gestures.append(gesture)
|
66 |
+
|
67 |
+
# Основной интерфейс
|
68 |
+
def main(config_path):
|
69 |
+
# --- Заголовок блока ---
|
70 |
st.markdown("""
|
71 |
<div class="upload-section">
|
72 |
<h3>🎥 Sign Language Recognition Demo</h3>
|
|
|
74 |
</div>
|
75 |
""", unsafe_allow_html=True)
|
76 |
|
77 |
+
# --- Скрытый лейбл uploader'а в стилизованной обёртке ---
|
78 |
+
with st.container():
|
79 |
+
uploaded_file = st.file_uploader(" ", type=["mp4", "avi", "mov", "gif"], label_visibility="collapsed")
|
80 |
+
|
81 |
+
if uploaded_file is not None:
|
82 |
+
video_bytes = uploaded_file.read()
|
83 |
+
container.video(data=video_bytes)
|
84 |
+
|
85 |
+
inference_thread = SLInference(config_path)
|
86 |
+
inference_thread.start()
|
87 |
+
|
88 |
+
text_output = st.empty()
|
89 |
+
|
90 |
+
if st.button("🔍 Predict Gestures"):
|
91 |
+
import tempfile
|
92 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
93 |
+
tfile.write(video_bytes)
|
94 |
+
cap = cv2.VideoCapture(tfile.name)
|
95 |
+
|
96 |
+
gestures = []
|
97 |
+
frames = []
|
98 |
+
batch_size = 32
|
99 |
+
|
100 |
+
def process_frames(batch):
|
101 |
+
process_batch(inference_thread, batch, gestures)
|
102 |
+
|
103 |
+
with ThreadPoolExecutor() as executor:
|
104 |
+
while cap.isOpened():
|
105 |
+
ret, frame = cap.read()
|
106 |
+
if not ret:
|
107 |
+
break
|
108 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
109 |
+
frames.append(frame)
|
110 |
+
if len(frames) == batch_size:
|
111 |
+
executor.submit(process_frames, frames)
|
112 |
+
frames = []
|
113 |
+
|
114 |
+
if frames:
|
115 |
+
executor.submit(process_frames, frames)
|
116 |
+
|
117 |
+
cap.release()
|
118 |
+
|
119 |
+
# Вывод результата
|
120 |
+
text_output.markdown(
|
121 |
+
f'<div class="section"><p style="font-size:20px">🖐️ Detected gestures: <b>{" ".join(gestures)}</b></p></div>',
|
122 |
+
unsafe_allow_html=True
|
123 |
+
)
|
124 |
+
|
125 |
+
# Исправление текста
|
126 |
+
st.text(correct_text_gpt3(" ".join(gestures)))
|
127 |
+
|
128 |
+
if __name__ == "__main__":
|
129 |
+
logging.basicConfig(level=logging.INFO)
|
130 |
+
main("configs/config.json")
|
131 |
+
|
132 |
|
133 |
|