Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,127 +3,29 @@ import time
|
|
3 |
import numpy as np
|
4 |
import mediapipe as mp
|
5 |
|
6 |
-
|
7 |
import cv2
|
8 |
from flask import Flask, render_template, request, Response, session, redirect, url_for
|
9 |
-
|
10 |
from flask_socketio import SocketIO
|
11 |
import yt_dlp as youtube_dl
|
12 |
|
13 |
-
|
14 |
model_object_detection = YOLO("bisindov2.pt")
|
15 |
|
16 |
app = Flask(__name__)
|
17 |
-
|
18 |
app.config['SECRET_KEY'] = 'secret!'
|
19 |
socketio = SocketIO(app, async_mode='threading')
|
20 |
stop_flag = False
|
21 |
|
22 |
-
|
23 |
-
######################################################
|
24 |
classes_translation = {
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
"C": "ج",
|
29 |
-
"D": "د",
|
30 |
-
"F": "ف",
|
31 |
-
"H": "هـ",
|
32 |
-
"I": "أنا",
|
33 |
-
"J": "جيم",
|
34 |
-
"L": "إل",
|
35 |
-
"M": "إم",
|
36 |
-
"O": "أو",
|
37 |
-
"R": "ر",
|
38 |
-
"T": "ت",
|
39 |
-
"U": "يو",
|
40 |
-
"V": "في",
|
41 |
-
"W": "دبليو",
|
42 |
-
"Z": "زد",
|
43 |
-
"additional": "إضافي",
|
44 |
-
"alcohol": "مدرسة",
|
45 |
-
"allergy": "حساسية",
|
46 |
-
"bacon": "لحم المقدد",
|
47 |
-
"bag": "حقيبة",
|
48 |
-
"barbecue": "شواء",
|
49 |
-
"bill": "فاتورة",
|
50 |
-
"biscuit": "بسكويت",
|
51 |
-
"bitter": "مر",
|
52 |
-
"bread": "خبز",
|
53 |
-
"burger": "برغر",
|
54 |
-
"bye": "وداعاً",
|
55 |
-
"cheese": "جبن",
|
56 |
-
"chicken": "دجاج",
|
57 |
-
"coke": "كوكاكولا",
|
58 |
-
"cold": "بارد",
|
59 |
-
"cost": "تكلفة",
|
60 |
-
"coupon": "كوبون",
|
61 |
-
"cup": "كوب",
|
62 |
-
"dessert": "حلوى",
|
63 |
-
"drink": "شراب",
|
64 |
-
"drive": "قيادة",
|
65 |
-
"eat": "تناول الطعام",
|
66 |
-
"eggs": "بيض",
|
67 |
-
"enjoy": "استمتع",
|
68 |
-
"fork": "شوكة",
|
69 |
-
"french fries": "بطاطس مقلية",
|
70 |
-
"fresh": "طازج",
|
71 |
-
"hello": "مرحبا",
|
72 |
-
"hot": "ساخن",
|
73 |
-
"icecream": "آيس كريم",
|
74 |
-
"ingredients": "مكونات",
|
75 |
-
"juicy": "عصيري",
|
76 |
-
"ketchup": "كاتشب",
|
77 |
-
"lactose": "لاكتوز",
|
78 |
-
"lettuce": "خس",
|
79 |
-
"lid": "غطاء",
|
80 |
-
"manager": "مدير",
|
81 |
-
"menu": "قائمة الطعام",
|
82 |
-
"milk": "حليب",
|
83 |
-
"mustard": "خردل",
|
84 |
-
"napkin": "منديل",
|
85 |
-
"no": "لا",
|
86 |
-
"order": "طلب",
|
87 |
-
"pepper": "فلفل",
|
88 |
-
"pickle": "مخلل",
|
89 |
-
"pizza": "بيتزا",
|
90 |
-
"please": "من فضلك",
|
91 |
-
"ready": "جاهز",
|
92 |
-
"refill": "إعادة ملء",
|
93 |
-
"repeat": "كرر",
|
94 |
-
"safe": "آمن",
|
95 |
-
"salt": "ملح",
|
96 |
-
"sandwich": "ساندويتش",
|
97 |
-
"sauce": "صلصة",
|
98 |
-
"small": "صغير",
|
99 |
-
"soda": "صودا",
|
100 |
-
"sorry": "آسف",
|
101 |
-
"spicy": "حار",
|
102 |
-
"spoon": "ملعقة",
|
103 |
-
"straw": "قش",
|
104 |
-
"sugar": "سكر",
|
105 |
-
"sweet": "حلو",
|
106 |
-
"tissues": "مناديل",
|
107 |
-
"total": "مجموع",
|
108 |
-
"urgent": "عاجل",
|
109 |
-
"vegetables": "خضروات",
|
110 |
-
"warm": "دافئ",
|
111 |
-
"water": "ماء",
|
112 |
-
"what": "ماذا",
|
113 |
-
"yoghurt": "زبادي",
|
114 |
-
"your": "لك",
|
115 |
-
"ILoveYou":"أحبك",
|
116 |
-
"Halo":"مرحبًا"
|
117 |
-
}
|
118 |
-
######################################################
|
119 |
class VideoStreaming(object):
|
120 |
def __init__(self):
|
121 |
-
super(VideoStreaming, self).__init__()
|
122 |
print ("===== Video Streaming =====")
|
123 |
self._preview = False
|
124 |
self._flipH = False
|
125 |
self._detect = False
|
126 |
-
self._model = False
|
127 |
self._mediaPipe = False
|
128 |
self._confidence = 75.0
|
129 |
self.mp_hands = mp.solutions.hands
|
@@ -175,8 +77,9 @@ class VideoStreaming(object):
|
|
175 |
self._flipH = False
|
176 |
self._detect = False
|
177 |
self._mediaPipe = False
|
178 |
-
|
179 |
self._confidence = 75.0
|
|
|
|
|
180 |
ydl_opts = {
|
181 |
"quiet": True,
|
182 |
"no_warnings": True,
|
@@ -184,48 +87,37 @@ class VideoStreaming(object):
|
|
184 |
"forceurl": True,
|
185 |
}
|
186 |
|
187 |
-
if url == '0'
|
188 |
-
|
189 |
-
else:
|
190 |
-
|
191 |
-
ydl = youtube_dl.YoutubeDL(ydl_opts)
|
192 |
-
|
193 |
-
info = ydl.extract_info(url, download=False)
|
194 |
-
url = info["url"]
|
195 |
-
|
196 |
-
cap = cv2.VideoCapture(url)
|
197 |
-
|
198 |
while True:
|
199 |
if self._preview:
|
200 |
if stop_flag:
|
201 |
print("Process Stopped")
|
202 |
return
|
203 |
-
|
204 |
grabbed, frame = cap.read()
|
|
|
205 |
if not grabbed:
|
206 |
break
|
|
|
207 |
if self.flipH:
|
208 |
frame = cv2.flip(frame, 1)
|
209 |
|
|
|
210 |
if self.detect:
|
211 |
frame_yolo = frame.copy()
|
212 |
results_yolo = model_object_detection.predict(frame_yolo, conf=self._confidence / 100)
|
213 |
-
|
214 |
frame_yolo, labels = results_yolo[0].plot()
|
215 |
-
|
216 |
-
# labels_confidences
|
217 |
-
|
218 |
for label in labels:
|
219 |
confidence = label.split(" ")[-1]
|
220 |
label_name = " ".join(label.split(" ")[:-1])
|
221 |
-
# Translate the label if it exists in the translation dictionary
|
222 |
translated_label = classes_translation.get(label_name, label_name)
|
223 |
-
list_labels
|
224 |
-
list_labels.append(confidence)
|
225 |
socketio.emit('label', list_labels)
|
226 |
|
|
|
227 |
if self.mediaPipe:
|
228 |
-
# Convert the image to RGB for processing with MediaPipe
|
229 |
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
230 |
results = self.hands.process(image)
|
231 |
|
@@ -236,48 +128,36 @@ class VideoStreaming(object):
|
|
236 |
hand_landmarks,
|
237 |
self.mp_hands.HAND_CONNECTIONS,
|
238 |
landmark_drawing_spec=mp.solutions.drawing_utils.DrawingSpec(color=(255, 0, 0), thickness=4, circle_radius=3),
|
239 |
-
connection_drawing_spec=mp.solutions.drawing_utils.DrawingSpec(color=(255, 255, 255), thickness=2, circle_radius=2),
|
240 |
)
|
241 |
|
242 |
frame = cv2.imencode(".jpg", frame)[1].tobytes()
|
243 |
-
yield (
|
244 |
-
b'--frame\r\n'
|
245 |
-
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n'
|
246 |
-
)
|
247 |
else:
|
248 |
-
snap = np.zeros((
|
249 |
-
1000,
|
250 |
-
1000
|
251 |
-
), np.uint8)
|
252 |
label = "Streaming Off"
|
253 |
H, W = snap.shape
|
254 |
font = cv2.FONT_HERSHEY_PLAIN
|
255 |
color = (255, 255, 255)
|
256 |
-
cv2.putText(snap, label, (W//2 - 100, H//2),
|
257 |
-
font, 2, color, 2)
|
258 |
frame = cv2.imencode(".jpg", snap)[1].tobytes()
|
259 |
-
yield (b'--frame\r\n'
|
260 |
-
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
261 |
|
262 |
|
263 |
-
#
|
264 |
VIDEO = VideoStreaming()
|
265 |
|
266 |
-
|
267 |
@app.route('/', methods=['GET', 'POST'])
|
268 |
def homepage():
|
269 |
return render_template('hompage.html')
|
270 |
|
271 |
-
|
272 |
@app.route('/index', methods=['GET', 'POST'])
|
273 |
def index():
|
274 |
-
print("index")
|
275 |
global stop_flag
|
276 |
stop_flag = False
|
277 |
if request.method == 'POST':
|
278 |
-
print("Index post request")
|
279 |
url = request.form['url']
|
280 |
-
print("index: ", url)
|
281 |
session['url'] = url
|
282 |
return redirect(url_for('index'))
|
283 |
return render_template('index.html')
|
@@ -285,35 +165,29 @@ def index():
|
|
285 |
@app.route('/video_feed')
|
286 |
def video_feed():
|
287 |
url = session.get('url', None)
|
288 |
-
print("video feed: ", url)
|
289 |
if url is None:
|
290 |
return redirect(url_for('homepage'))
|
291 |
-
|
292 |
return Response(VIDEO.show(url), mimetype='multipart/x-mixed-replace; boundary=frame')
|
293 |
|
294 |
-
#
|
295 |
@app.route("/request_preview_switch")
|
296 |
def request_preview_switch():
|
297 |
VIDEO.preview = not VIDEO.preview
|
298 |
-
print("*"*10, VIDEO.preview)
|
299 |
return "nothing"
|
300 |
|
301 |
@app.route("/request_flipH_switch")
|
302 |
def request_flipH_switch():
|
303 |
VIDEO.flipH = not VIDEO.flipH
|
304 |
-
print("*"*10, VIDEO.flipH)
|
305 |
return "nothing"
|
306 |
|
307 |
@app.route("/request_run_model_switch")
|
308 |
def request_run_model_switch():
|
309 |
VIDEO.detect = not VIDEO.detect
|
310 |
-
print("*"*10, VIDEO.detect)
|
311 |
return "nothing"
|
312 |
|
313 |
@app.route("/request_mediapipe_switch")
|
314 |
def request_mediapipe_switch():
|
315 |
VIDEO.mediaPipe = not VIDEO.mediaPipe
|
316 |
-
print("*"*10, VIDEO.mediaPipe)
|
317 |
return "nothing"
|
318 |
|
319 |
@app.route('/update_slider_value', methods=['POST'])
|
@@ -324,7 +198,6 @@ def update_slider_value():
|
|
324 |
|
325 |
@app.route('/stop_process')
|
326 |
def stop_process():
|
327 |
-
print("Process stop Request")
|
328 |
global stop_flag
|
329 |
stop_flag = True
|
330 |
return 'Process Stop Request'
|
@@ -333,5 +206,9 @@ def stop_process():
|
|
333 |
def test_connect():
|
334 |
print('Connected')
|
335 |
|
|
|
|
|
|
|
|
|
336 |
if __name__ == '__main__':
|
337 |
socketio.run(app, host="0.0.0.0", allow_unsafe_werkzeug=True,port=7860)
|
|
|
3 |
import numpy as np
|
4 |
import mediapipe as mp
|
5 |
|
|
|
6 |
import cv2
|
7 |
from flask import Flask, render_template, request, Response, session, redirect, url_for
|
|
|
8 |
from flask_socketio import SocketIO
|
9 |
import yt_dlp as youtube_dl
|
10 |
|
|
|
11 |
model_object_detection = YOLO("bisindov2.pt")
|
12 |
|
13 |
app = Flask(__name__)
|
|
|
14 |
app.config['SECRET_KEY'] = 'secret!'
|
15 |
socketio = SocketIO(app, async_mode='threading')
|
16 |
stop_flag = False
|
17 |
|
18 |
+
# Translation mappings
|
|
|
19 |
classes_translation = {
|
20 |
+
# Add your translations here
|
21 |
+
}
|
22 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
class VideoStreaming(object):
|
24 |
def __init__(self):
|
|
|
25 |
print ("===== Video Streaming =====")
|
26 |
self._preview = False
|
27 |
self._flipH = False
|
28 |
self._detect = False
|
|
|
29 |
self._mediaPipe = False
|
30 |
self._confidence = 75.0
|
31 |
self.mp_hands = mp.solutions.hands
|
|
|
77 |
self._flipH = False
|
78 |
self._detect = False
|
79 |
self._mediaPipe = False
|
|
|
80 |
self._confidence = 75.0
|
81 |
+
|
82 |
+
# Video capture setup
|
83 |
ydl_opts = {
|
84 |
"quiet": True,
|
85 |
"no_warnings": True,
|
|
|
87 |
"forceurl": True,
|
88 |
}
|
89 |
|
90 |
+
cap = cv2.VideoCapture(0 if url == '0' else url)
|
91 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
while True:
|
93 |
if self._preview:
|
94 |
if stop_flag:
|
95 |
print("Process Stopped")
|
96 |
return
|
97 |
+
|
98 |
grabbed, frame = cap.read()
|
99 |
+
|
100 |
if not grabbed:
|
101 |
break
|
102 |
+
|
103 |
if self.flipH:
|
104 |
frame = cv2.flip(frame, 1)
|
105 |
|
106 |
+
# Perform YOLO object detection
|
107 |
if self.detect:
|
108 |
frame_yolo = frame.copy()
|
109 |
results_yolo = model_object_detection.predict(frame_yolo, conf=self._confidence / 100)
|
|
|
110 |
frame_yolo, labels = results_yolo[0].plot()
|
111 |
+
|
|
|
|
|
112 |
for label in labels:
|
113 |
confidence = label.split(" ")[-1]
|
114 |
label_name = " ".join(label.split(" ")[:-1])
|
|
|
115 |
translated_label = classes_translation.get(label_name, label_name)
|
116 |
+
list_labels = [translated_label, confidence]
|
|
|
117 |
socketio.emit('label', list_labels)
|
118 |
|
119 |
+
# Process hand landmarks with MediaPipe
|
120 |
if self.mediaPipe:
|
|
|
121 |
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
122 |
results = self.hands.process(image)
|
123 |
|
|
|
128 |
hand_landmarks,
|
129 |
self.mp_hands.HAND_CONNECTIONS,
|
130 |
landmark_drawing_spec=mp.solutions.drawing_utils.DrawingSpec(color=(255, 0, 0), thickness=4, circle_radius=3),
|
131 |
+
connection_drawing_spec=mp.solutions.drawing_utils.DrawingSpec(color=(255, 255, 255), thickness=2, circle_radius=2),
|
132 |
)
|
133 |
|
134 |
frame = cv2.imencode(".jpg", frame)[1].tobytes()
|
135 |
+
yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
|
|
|
|
|
|
136 |
else:
|
137 |
+
snap = np.zeros((1000, 1000), np.uint8)
|
|
|
|
|
|
|
138 |
label = "Streaming Off"
|
139 |
H, W = snap.shape
|
140 |
font = cv2.FONT_HERSHEY_PLAIN
|
141 |
color = (255, 255, 255)
|
142 |
+
cv2.putText(snap, label, (W//2 - 100, H//2), font, 2, color, 2)
|
|
|
143 |
frame = cv2.imencode(".jpg", snap)[1].tobytes()
|
144 |
+
yield (b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
|
|
145 |
|
146 |
|
147 |
+
# Initialize VideoStreaming object
|
148 |
VIDEO = VideoStreaming()
|
149 |
|
150 |
+
# Flask routes for the web application
|
151 |
@app.route('/', methods=['GET', 'POST'])
|
152 |
def homepage():
|
153 |
return render_template('hompage.html')
|
154 |
|
|
|
155 |
@app.route('/index', methods=['GET', 'POST'])
|
156 |
def index():
|
|
|
157 |
global stop_flag
|
158 |
stop_flag = False
|
159 |
if request.method == 'POST':
|
|
|
160 |
url = request.form['url']
|
|
|
161 |
session['url'] = url
|
162 |
return redirect(url_for('index'))
|
163 |
return render_template('index.html')
|
|
|
165 |
@app.route('/video_feed')
|
166 |
def video_feed():
|
167 |
url = session.get('url', None)
|
|
|
168 |
if url is None:
|
169 |
return redirect(url_for('homepage'))
|
|
|
170 |
return Response(VIDEO.show(url), mimetype='multipart/x-mixed-replace; boundary=frame')
|
171 |
|
172 |
+
# Button request routes for controlling settings
|
173 |
@app.route("/request_preview_switch")
|
174 |
def request_preview_switch():
|
175 |
VIDEO.preview = not VIDEO.preview
|
|
|
176 |
return "nothing"
|
177 |
|
178 |
@app.route("/request_flipH_switch")
|
179 |
def request_flipH_switch():
|
180 |
VIDEO.flipH = not VIDEO.flipH
|
|
|
181 |
return "nothing"
|
182 |
|
183 |
@app.route("/request_run_model_switch")
|
184 |
def request_run_model_switch():
|
185 |
VIDEO.detect = not VIDEO.detect
|
|
|
186 |
return "nothing"
|
187 |
|
188 |
@app.route("/request_mediapipe_switch")
|
189 |
def request_mediapipe_switch():
|
190 |
VIDEO.mediaPipe = not VIDEO.mediaPipe
|
|
|
191 |
return "nothing"
|
192 |
|
193 |
@app.route('/update_slider_value', methods=['POST'])
|
|
|
198 |
|
199 |
@app.route('/stop_process')
|
200 |
def stop_process():
|
|
|
201 |
global stop_flag
|
202 |
stop_flag = True
|
203 |
return 'Process Stop Request'
|
|
|
206 |
def test_connect():
|
207 |
print('Connected')
|
208 |
|
209 |
+
|
210 |
+
|
211 |
+
|
212 |
+
|
213 |
if __name__ == '__main__':
|
214 |
socketio.run(app, host="0.0.0.0", allow_unsafe_werkzeug=True,port=7860)
|