osheina commited on
Commit
5d28b08
·
verified ·
1 Parent(s): ead5f0d

Update pages/Video Upload.py

Browse files
Files changed (1) hide show
  1. pages/Video Upload.py +125 -186
pages/Video Upload.py CHANGED
@@ -1,187 +1,126 @@
 
 
 
 
 
1
  import streamlit as st
2
- from PIL import Image
3
- import numpy as np
4
-
5
- # Page config
6
- st.set_page_config(
7
- page_title="GestureGuru",
8
- page_icon="🤖",
9
- layout="wide",
10
- initial_sidebar_state="expanded"
11
- )
12
-
13
- # --- Custom Style ---
14
- st.markdown("""
15
- <style>
16
- body {
17
- background-image: url('https://images.unsplash.com/photo-1498050108023-c5249f4df085?auto=format&fit=crop&w=1920&q=80');
18
- background-size: cover;
19
- background-attachment: fixed;
20
- background-position: center;
21
- font-family: 'Segoe UI', sans-serif;
22
- color: #222;
23
- animation: fade-in 2s ease-out;
24
- }
25
- .sidebar .sidebar-content {
26
- background-color: rgba(255, 255, 255, 0.8);
27
- padding: 1rem;
28
- border-radius: 16px;
29
- box-shadow: 0 4px 12px rgba(0,0,0,0.1);
30
- backdrop-filter: blur(8px);
31
- }
32
- .hero {
33
- text-align: center;
34
- padding: 5rem 2rem;
35
- background: linear-gradient(135deg, #8e2de2, #4a00e0);
36
- border-radius: 24px;
37
- color: white;
38
- margin-bottom: 3rem;
39
- box-shadow: 0 20px 40px rgba(0,0,0,0.1);
40
- animation: fade-in 2s ease-out;
41
- position: relative;
42
- }
43
- .hero h1 {
44
- font-size: 3.75em;
45
- font-weight: 700;
46
- margin-bottom: 0.3em;
47
- animation: glow-text 2s infinite alternate;
48
- }
49
- .hero p {
50
- font-size: 1.3em;
51
- font-weight: 300;
52
- margin-top: 0.5em;
53
- animation: slide-in-bottom 1.2s ease-out;
54
- }
55
- .hero img.logo {
56
- width: 120px;
57
- position: absolute;
58
- top: 20px;
59
- left: 20px;
60
- border-radius: 50%;
61
- border: 2px solid white;
62
- box-shadow: 0 4px 12px rgba(0,0,0,0.4);
63
- }
64
- .section {
65
- background: rgba(255, 255, 255, 0.95);
66
- border-radius: 20px;
67
- padding: 2.5rem;
68
- margin-bottom: 2.5rem;
69
- box-shadow: 0 10px 30px rgba(0, 0, 0, 0.06);
70
- backdrop-filter: blur(4px);
71
- animation: fade-in-up 1s ease-out;
72
- }
73
- .upload-section {
74
- background: linear-gradient(to right, #00c6ff, #0072ff);
75
- color: white;
76
- padding: 2rem;
77
- border-radius: 20px;
78
- margin-bottom: 2rem;
79
- box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
80
- animation: fade-in-up 1s ease-out;
81
- }
82
- .upload-section h3 {
83
- color: white;
84
- }
85
- h3 {
86
- font-size: 1.8rem;
87
- color: #2e2e2e;
88
- margin-bottom: 1rem;
89
- }
90
- ul, ol {
91
- font-size: 1.1em;
92
- line-height: 1.6;
93
- color: #444;
94
- }
95
- .contact-block {
96
- text-align: center;
97
- padding: 3rem;
98
- border-radius: 20px;
99
- background: linear-gradient(to right, #ff6a00, #ee0979);
100
- color: white;
101
- margin-top: 3rem;
102
- box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
103
- animation: fade-in 2s ease-out;
104
- }
105
- .contact-block a {
106
- color: #fff;
107
- text-decoration: underline;
108
- }
109
- @keyframes fade-in {
110
- from { opacity: 0; }
111
- to { opacity: 1; }
112
- }
113
- @keyframes fade-in-up {
114
- from { opacity: 0; transform: translateY(30px); }
115
- to { opacity: 1; transform: translateY(0); }
116
- }
117
- @keyframes slide-in-top {
118
- 0% { transform: translateY(-100px); opacity: 0; }
119
- 100% { transform: translateY(0); opacity: 1; }
120
- }
121
- @keyframes slide-in-bottom {
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")