osheina commited on
Commit
ead5f0d
·
verified ·
1 Parent(s): 35b42a4

Update pages/Video Upload.py

Browse files
Files changed (1) hide show
  1. pages/Video Upload.py +184 -126
pages/Video Upload.py CHANGED
@@ -1,129 +1,187 @@
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
- DEFAULT_WIDTH = 50
12
-
13
- import openai
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
-
32
- corrected_text = response.choices[0].message['content'].strip()
33
- return corrected_text
34
-
35
- #st.set_page_config(layout="wide")
36
-
37
-
38
-
39
- width = 50
40
- side = max((100 - width) / 1.2, 0.01)
41
-
42
- _, container, _ = st.columns([side, width, side])
43
-
44
- logger = logging.getLogger(__name__)
45
-
46
- class SLInference:
47
- def __init__(self, config_path):
48
- self.config = self.load_config(config_path)
49
- self.predictor = Predictor(self.config)
50
- self.input_queue = deque(maxlen=32) # Queue to store 32 frames
51
- self.pred = ''
52
-
53
- def load_config(self, config_path):
54
- import json
55
- with open(config_path, 'r') as f:
56
- return json.load(f)
57
-
58
- def start(self):
59
- pass # This method can be left empty or add initialization logic
60
-
61
- def predict(self, frames):
62
- frames_resized = [cv2.resize(frame, (224, 224)) for frame in frames]
63
- while len(frames_resized) < 32:
64
- frames_resized.append(frames_resized[-1])
65
- result = self.predictor.predict(frames_resized)
66
- if result:
67
- return result["labels"][0]
68
- return 'no'
69
-
70
- def process_batch(inference_thread, frames, gestures):
71
- gesture = inference_thread.predict(frames)
72
- if gesture not in ['no', ''] and gesture not in gestures:
73
- gestures.append(gesture)
74
-
75
- def main(config_path):
76
- #st.set_page_config(layout="wide")
77
- st.title("Sign Language Recognition Demo")
78
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  st.warning("Please upload a video file for prediction.")
80
 
81
- uploaded_file = st.file_uploader("Upload Video", type=["mp4", "avi", "mov", "gif"])
82
-
83
- if uploaded_file is not None:
84
- video_bytes = uploaded_file.read()
85
- container.video(data=video_bytes)
86
- #st.video(video_bytes)
87
-
88
- inference_thread = SLInference(config_path)
89
- inference_thread.start()
90
-
91
- text_output = st.empty()
92
-
93
- if st.button("Predict"):
94
- import tempfile
95
- tfile = tempfile.NamedTemporaryFile(delete=False)
96
- tfile.write(video_bytes)
97
- cap = cv2.VideoCapture(tfile.name)
98
-
99
- gestures = []
100
- frames = []
101
- batch_size = 32
102
-
103
- def process_frames(batch):
104
- process_batch(inference_thread, batch, gestures)
105
-
106
- with ThreadPoolExecutor() as executor:
107
- while cap.isOpened():
108
- ret, frame = cap.read()
109
- if not ret:
110
- break
111
- frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
112
- frames.append(frame)
113
- if len(frames) == batch_size:
114
- executor.submit(process_frames, frames)
115
- frames = []
116
-
117
- if frames:
118
- executor.submit(process_frames, frames)
119
-
120
- cap.release()
121
- text_output.markdown(f'<p style="font-size:20px"> Gestures in video: {" ".join(gestures)}</p>',
122
- unsafe_allow_html=True)
123
- st.text(correct_text_gpt3(" ".join(gestures)))
124
-
125
- print(gestures)
126
-
127
- if __name__ == "__main__":
128
- logging.basicConfig(level=logging.INFO)
129
- main("configs/config.json")
 
 
 
 
 
 
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)