Spaces:
Running
Running
Update pages/Video Upload.py
Browse files- pages/Video Upload.py +125 -186
pages/Video Upload.py
CHANGED
@@ -1,187 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
|
4 |
-
|
5 |
-
#
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
.
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
0% { transform: translateY(100px); opacity: 0; }
|
123 |
-
100% { transform: translateY(0); opacity: 1; }
|
124 |
-
}
|
125 |
-
@keyframes glow-text {
|
126 |
-
from { text-shadow: 0 0 5px #fff, 0 0 10px #bca7ff, 0 0 20px #7e57ff; }
|
127 |
-
to { text-shadow: 0 0 10px #fff, 0 0 20px #b388ff, 0 0 30px #7e57ff; }
|
128 |
-
}
|
129 |
-
</style>
|
130 |
-
""", unsafe_allow_html=True)
|
131 |
-
|
132 |
-
# --- Hero Section ---
|
133 |
-
st.markdown("""
|
134 |
-
<div class="hero">
|
135 |
-
<img src="https://cdn-icons-png.flaticon.com/512/3062/3062634.png" class="logo" alt="logo">
|
136 |
-
<h1>GestureGuru</h1>
|
137 |
-
<p>AI for Sign Language Understanding & Accessibility</p>
|
138 |
-
</div>
|
139 |
-
""", unsafe_allow_html=True)
|
140 |
-
|
141 |
-
# --- Features ---
|
142 |
-
st.markdown("""
|
143 |
-
<div class="section">
|
144 |
-
<h3>✨ Key Features</h3>
|
145 |
-
<ul>
|
146 |
-
<li>⚡ Real-time AI gesture recognition</li>
|
147 |
-
<li>🧠 Optimized for Russian Sign Language</li>
|
148 |
-
<li>🖥️ Lightweight and browser-based interface</li>
|
149 |
-
<li>🌍 Promoting accessibility and inclusive communication</li>
|
150 |
-
</ul>
|
151 |
-
</div>
|
152 |
-
""", unsafe_allow_html=True)
|
153 |
-
|
154 |
-
# --- How it works ---
|
155 |
-
st.markdown("""
|
156 |
-
<div class="section">
|
157 |
-
<h3>🛠 How it works</h3>
|
158 |
-
<ol>
|
159 |
-
<li>Camera captures hand gestures</li>
|
160 |
-
<li>Frame is processed via ONNX deep learning model</li>
|
161 |
-
<li>Text prediction appears instantly on screen</li>
|
162 |
-
</ol>
|
163 |
-
</div>
|
164 |
-
""", unsafe_allow_html=True)
|
165 |
-
|
166 |
-
# --- Upload Section ---
|
167 |
-
st.markdown("""
|
168 |
-
<div class="upload-section">
|
169 |
-
<h3>🎥 Sign Language Recognition Demo</h3>
|
170 |
-
<p>Upload a short video clip to detect sign gestures:</p>
|
171 |
-
</div>
|
172 |
-
""", unsafe_allow_html=True)
|
173 |
-
|
174 |
-
uploaded_file = st.file_uploader("Upload Video", type=["mp4", "avi", "mov", "gif"])
|
175 |
-
if uploaded_file:
|
176 |
-
st.video(uploaded_file)
|
177 |
-
else:
|
178 |
-
st.warning("Please upload a video file for prediction.")
|
179 |
-
|
180 |
-
# --- Contact block ---
|
181 |
-
st.markdown("""
|
182 |
-
<div class="contact-block">
|
183 |
-
<h3>📬 Let's collaborate!</h3>
|
184 |
-
<p>For demos, partnerships, or research opportunities:</p>
|
185 |
-
<p>Email: <a href="mailto:your@email.com">your@email.com</a></p>
|
186 |
-
</div>
|
187 |
-
""", unsafe_allow_html=True)
|
|
|
1 |
+
import logging
|
2 |
+
import queue
|
3 |
+
from collections import deque
|
4 |
+
from concurrent.futures import ThreadPoolExecutor
|
5 |
+
|
6 |
import streamlit as st
|
7 |
+
from streamlit_webrtc import WebRtcMode, webrtc_streamer
|
8 |
+
|
9 |
+
import cv2
|
10 |
+
from model import Predictor # Import Predictor from your model file
|
11 |
+
import openai
|
12 |
+
|
13 |
+
DEFAULT_WIDTH = 50
|
14 |
+
|
15 |
+
# Initialize the OpenAI client
|
16 |
+
openai.api_key = 'sk-proj-GDxupB1DFvTTWBg38VyST3BlbkFJ7MdcACLwu3u0U1QvWeMb'
|
17 |
+
|
18 |
+
def correct_text_gpt3(input_text):
|
19 |
+
prompt = f"Исправь грамматические ошибки в тексте: '{input_text}'"
|
20 |
+
response = openai.ChatCompletion.create(
|
21 |
+
model="gpt-3.5-turbo",
|
22 |
+
messages=[
|
23 |
+
{"role": "system", "content": "You are a helpful assistant that corrects grammatical errors."},
|
24 |
+
{"role": "user", "content": prompt}
|
25 |
+
],
|
26 |
+
max_tokens=50,
|
27 |
+
n=1,
|
28 |
+
stop=None,
|
29 |
+
temperature=0.5,
|
30 |
+
)
|
31 |
+
corrected_text = response.choices[0].message['content'].strip()
|
32 |
+
return corrected_text
|
33 |
+
|
34 |
+
# Center content layout
|
35 |
+
width = 50
|
36 |
+
side = max((100 - width) / 1.2, 0.01)
|
37 |
+
_, container, _ = st.columns([side, width, side])
|
38 |
+
|
39 |
+
logger = logging.getLogger(__name__)
|
40 |
+
|
41 |
+
class SLInference:
|
42 |
+
def __init__(self, config_path):
|
43 |
+
self.config = self.load_config(config_path)
|
44 |
+
self.predictor = Predictor(self.config)
|
45 |
+
self.input_queue = deque(maxlen=32)
|
46 |
+
self.pred = ''
|
47 |
+
|
48 |
+
def load_config(self, config_path):
|
49 |
+
import json
|
50 |
+
with open(config_path, 'r') as f:
|
51 |
+
return json.load(f)
|
52 |
+
|
53 |
+
def start(self):
|
54 |
+
pass
|
55 |
+
|
56 |
+
def predict(self, frames):
|
57 |
+
frames_resized = [cv2.resize(frame, (224, 224)) for frame in frames]
|
58 |
+
while len(frames_resized) < 32:
|
59 |
+
frames_resized.append(frames_resized[-1])
|
60 |
+
result = self.predictor.predict(frames_resized)
|
61 |
+
if result:
|
62 |
+
return result["labels"][0]
|
63 |
+
return 'no'
|
64 |
+
|
65 |
+
def process_batch(inference_thread, frames, gestures):
|
66 |
+
gesture = inference_thread.predict(frames)
|
67 |
+
if gesture not in ['no', ''] and gesture not in gestures:
|
68 |
+
gestures.append(gesture)
|
69 |
+
|
70 |
+
def main(config_path):
|
71 |
+
# --- Styled Upload Section ---
|
72 |
+
st.markdown("""
|
73 |
+
<div class="upload-section">
|
74 |
+
<h3>🎥 Sign Language Recognition Demo</h3>
|
75 |
+
<p>Upload a short video clip to detect sign gestures:</p>
|
76 |
+
</div>
|
77 |
+
""", unsafe_allow_html=True)
|
78 |
+
|
79 |
+
uploaded_file = st.file_uploader("Upload Video", type=["mp4", "avi", "mov", "gif"])
|
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 |
+
text_output.markdown(
|
119 |
+
f'<div class="section"><p style="font-size:20px">🖐️ Detected gestures: <b>{" ".join(gestures)}</b></p></div>',
|
120 |
+
unsafe_allow_html=True
|
121 |
+
)
|
122 |
+
st.text(correct_text_gpt3(" ".join(gestures)))
|
123 |
+
|
124 |
+
if __name__ == "__main__":
|
125 |
+
logging.basicConfig(level=logging.INFO)
|
126 |
+
main("configs/config.json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|