osheina commited on
Commit
149bbfe
·
verified ·
1 Parent(s): b98d63e

Update pages/Video Upload.py

Browse files
Files changed (1) hide show
  1. pages/Video Upload.py +0 -116
pages/Video Upload.py CHANGED
@@ -1,73 +1,3 @@
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">
@@ -78,50 +8,4 @@ def main(config_path):
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")
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # --- Styled Upload Section ---
2
  st.markdown("""
3
  <div class="upload-section">
 
8
 
9
  uploaded_file = st.file_uploader("Upload Video", type=["mp4", "avi", "mov", "gif"])
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11