Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,363 @@
|
|
1 |
-
|
2 |
-
from flask import Flask, render_template, Response, request, jsonify, redirect, url_for
|
3 |
-
from aiortc import RTCPeerConnection, RTCSessionDescription
|
4 |
-
import cv2
|
5 |
-
import json
|
6 |
-
import uuid
|
7 |
-
import asyncio
|
8 |
-
import logging
|
9 |
import time
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
|
15 |
-
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
break
|
25 |
-
else:
|
26 |
-
ret, buffer = cv2.imencode('.jpg', frame)
|
27 |
-
frame = buffer.tobytes()
|
28 |
-
# Concatenate frame and yield for streaming
|
29 |
-
yield (b'--frame\r\n'
|
30 |
-
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
31 |
-
elapsed_time = time.time() - start_time
|
32 |
-
logging.debug(f"Frame generation time: {elapsed_time} seconds")
|
33 |
-
|
34 |
-
# Route to render the HTML template
|
35 |
-
@app.route('/')
|
36 |
-
def index():
|
37 |
-
return render_template('index.html')
|
38 |
-
# return redirect(url_for('video_feed')) #to render live stream directly
|
39 |
|
40 |
-
|
41 |
-
async def offer_async():
|
42 |
-
params = await request.json
|
43 |
-
offer = RTCSessionDescription(sdp=params["sdp"], type=params["type"])
|
44 |
|
45 |
-
# Create an RTCPeerConnection instance
|
46 |
-
pc = RTCPeerConnection()
|
47 |
|
48 |
-
# Generate a unique ID for the RTCPeerConnection
|
49 |
-
pc_id = "PeerConnection(%s)" % uuid.uuid4()
|
50 |
-
pc_id = pc_id[:8]
|
51 |
|
52 |
-
# Create a data channel named "chat"
|
53 |
-
# pc.createDataChannel("chat")
|
54 |
|
55 |
-
|
56 |
-
await pc.createOffer(offer)
|
57 |
-
await pc.setLocalDescription(offer)
|
58 |
|
59 |
-
|
60 |
-
response_data = {"s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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
# Route to stream video frames
|
78 |
@app.route('/video_feed')
|
79 |
def video_feed():
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
app.run(debug=True, host='0.0.0.0')
|
|
|
1 |
+
from ultralytics import YOLO
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import time
|
3 |
+
import numpy as np
|
4 |
+
import mediapipe as mp
|
5 |
|
6 |
+
import uvicorn
|
7 |
+
from socketio import ASGIApp
|
8 |
|
9 |
+
import cv2
|
10 |
+
from flask import Flask, render_template, request, Response, session, redirect, url_for
|
11 |
+
from flask import Flask, render_template
|
12 |
|
13 |
+
from flask_socketio import SocketIO
|
14 |
+
from flask_socketio import emit
|
15 |
+
from flask_cors import CORS
|
16 |
+
|
17 |
+
from flask_socketio import SocketIO
|
18 |
+
import yt_dlp as youtube_dl
|
19 |
+
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
+
model_object_detection = YOLO("bisindov2.pt")
|
|
|
|
|
|
|
22 |
|
|
|
|
|
23 |
|
|
|
|
|
|
|
24 |
|
|
|
|
|
25 |
|
26 |
+
app = Flask(__name__)
|
|
|
|
|
27 |
|
28 |
+
socketio = SocketIO(app, cors_allowed_origins="*")
|
|
|
29 |
|
30 |
+
CORS(app)
|
31 |
|
32 |
+
app.secret_key = 'flask-sockets-builds'
|
33 |
+
|
34 |
+
|
35 |
+
@socketio.on('image')
|
36 |
+
def handle_image(image_data):
|
37 |
+
# Process the received image data
|
38 |
+
# Here, you can save the image, perform analysis, etc.
|
39 |
+
# For demonstration purposes, let's just print the length of the image data
|
40 |
+
print("Received image data length:", len(image_data))
|
41 |
+
|
42 |
+
# Send a response back to the client
|
43 |
+
# You can send any data you want back to the client
|
44 |
+
response_data = {'status': 'success'}
|
45 |
+
socketio.emit('response_back', response_data)
|
46 |
|
47 |
+
######################################################
|
48 |
+
classes_translation = {
|
49 |
+
"all": "الكل",
|
50 |
+
"A": "أ",
|
51 |
+
"B": "ب",
|
52 |
+
"C": "ج",
|
53 |
+
"D": "د",
|
54 |
+
"F": "ف",
|
55 |
+
"H": "هـ",
|
56 |
+
"I": "أنا",
|
57 |
+
"J": "جيم",
|
58 |
+
"L": "إل",
|
59 |
+
"M": "إم",
|
60 |
+
"O": "أو",
|
61 |
+
"R": "ر",
|
62 |
+
"T": "ت",
|
63 |
+
"U": "يو",
|
64 |
+
"V": "في",
|
65 |
+
"W": "دبليو",
|
66 |
+
"Z": "زد",
|
67 |
+
"additional": "إضافي",
|
68 |
+
"alcohol": "مدرسة",
|
69 |
+
"allergy": "حساسية",
|
70 |
+
"bacon": "لحم المقدد",
|
71 |
+
"bag": "حقيبة",
|
72 |
+
"barbecue": "شواء",
|
73 |
+
"bill": "فاتورة",
|
74 |
+
"biscuit": "بسكويت",
|
75 |
+
"bitter": "مر",
|
76 |
+
"bread": "خبز",
|
77 |
+
"burger": "برغر",
|
78 |
+
"bye": "وداعاً",
|
79 |
+
"cheese": "جبن",
|
80 |
+
"chicken": "دجاج",
|
81 |
+
"coke": "كوكاكولا",
|
82 |
+
"cold": "بارد",
|
83 |
+
"cost": "تكلفة",
|
84 |
+
"coupon": "كوبون",
|
85 |
+
"cup": "كوب",
|
86 |
+
"dessert": "حلوى",
|
87 |
+
"drink": "شراب",
|
88 |
+
"drive": "قيادة",
|
89 |
+
"eat": "تناول الطعام",
|
90 |
+
"eggs": "بيض",
|
91 |
+
"enjoy": "استمتع",
|
92 |
+
"fork": "شوكة",
|
93 |
+
"french fries": "بطاطس مقلية",
|
94 |
+
"fresh": "طازج",
|
95 |
+
"hello": "مرحبا",
|
96 |
+
"hot": "ساخن",
|
97 |
+
"icecream": "آيس كريم",
|
98 |
+
"ingredients": "مكونات",
|
99 |
+
"juicy": "عصيري",
|
100 |
+
"ketchup": "كاتشب",
|
101 |
+
"lactose": "لاكتوز",
|
102 |
+
"lettuce": "خس",
|
103 |
+
"lid": "غطاء",
|
104 |
+
"manager": "مدير",
|
105 |
+
"menu": "قائمة الطعام",
|
106 |
+
"milk": "حليب",
|
107 |
+
"mustard": "خردل",
|
108 |
+
"napkin": "منديل",
|
109 |
+
"no": "لا",
|
110 |
+
"order": "طلب",
|
111 |
+
"pepper": "فلفل",
|
112 |
+
"pickle": "مخلل",
|
113 |
+
"pizza": "بيتزا",
|
114 |
+
"please": "من فضلك",
|
115 |
+
"ready": "جاهز",
|
116 |
+
"refill": "إعادة ملء",
|
117 |
+
"repeat": "كرر",
|
118 |
+
"safe": "آمن",
|
119 |
+
"salt": "ملح",
|
120 |
+
"sandwich": "ساندويتش",
|
121 |
+
"sauce": "صلصة",
|
122 |
+
"small": "صغير",
|
123 |
+
"soda": "صودا",
|
124 |
+
"sorry": "آسف",
|
125 |
+
"spicy": "حار",
|
126 |
+
"spoon": "ملعقة",
|
127 |
+
"straw": "قش",
|
128 |
+
"sugar": "سكر",
|
129 |
+
"sweet": "حلو",
|
130 |
+
"tissues": "مناديل",
|
131 |
+
"total": "مجموع",
|
132 |
+
"urgent": "عاجل",
|
133 |
+
"vegetables": "خضروات",
|
134 |
+
"warm": "دافئ",
|
135 |
+
"water": "ماء",
|
136 |
+
"what": "ماذا",
|
137 |
+
"yoghurt": "زبادي",
|
138 |
+
"your": "لك",
|
139 |
+
"ILoveYou":"أحبك",
|
140 |
+
"Halo":"مرحبًا"
|
141 |
+
}
|
142 |
+
######################################################
|
143 |
+
class VideoStreaming(object):
|
144 |
+
def __init__(self):
|
145 |
+
super(VideoStreaming, self).__init__()
|
146 |
+
print ("===== Video Streaming =====")
|
147 |
+
self._preview = False
|
148 |
+
self._flipH = False
|
149 |
+
self._detect = False
|
150 |
+
self._model = False
|
151 |
+
self._mediaPipe = False
|
152 |
+
self._confidence = 75.0
|
153 |
+
self.mp_hands = mp.solutions.hands
|
154 |
+
self.hands = self.mp_hands.Hands()
|
155 |
+
|
156 |
+
@property
|
157 |
+
def confidence(self):
|
158 |
+
return self._confidence
|
159 |
+
|
160 |
+
@confidence.setter
|
161 |
+
def confidence(self, value):
|
162 |
+
self._confidence = int(value)
|
163 |
+
|
164 |
+
@property
|
165 |
+
def preview(self):
|
166 |
+
return self._preview
|
167 |
+
|
168 |
+
@preview.setter
|
169 |
+
def preview(self, value):
|
170 |
+
self._preview = bool(value)
|
171 |
+
|
172 |
+
@property
|
173 |
+
def flipH(self):
|
174 |
+
return self._flipH
|
175 |
+
|
176 |
+
@flipH.setter
|
177 |
+
def flipH(self, value):
|
178 |
+
self._flipH = bool(value)
|
179 |
+
|
180 |
+
@property
|
181 |
+
def detect(self):
|
182 |
+
return self._detect
|
183 |
+
|
184 |
+
@detect.setter
|
185 |
+
def detect(self, value):
|
186 |
+
self._detect = bool(value)
|
187 |
+
|
188 |
+
@property
|
189 |
+
def mediaPipe(self):
|
190 |
+
return self._mediaPipe
|
191 |
+
|
192 |
+
@mediaPipe.setter
|
193 |
+
def mediaPipe(self, value):
|
194 |
+
self._mediaPipe = bool(value)
|
195 |
+
|
196 |
+
def show(self, url):
|
197 |
+
print(url)
|
198 |
+
self._preview = False
|
199 |
+
self._flipH = False
|
200 |
+
self._detect = False
|
201 |
+
self._mediaPipe = False
|
202 |
+
|
203 |
+
self._confidence = 75.0
|
204 |
+
ydl_opts = {
|
205 |
+
"quiet": True,
|
206 |
+
"no_warnings": True,
|
207 |
+
"format": "best",
|
208 |
+
"forceurl": True,
|
209 |
+
}
|
210 |
+
|
211 |
+
if url == '0':
|
212 |
+
cap = cv2.VideoCapture(0)
|
213 |
+
else:
|
214 |
+
|
215 |
+
ydl = youtube_dl.YoutubeDL(ydl_opts)
|
216 |
+
|
217 |
+
info = ydl.extract_info(url, download=False)
|
218 |
+
url = info["url"]
|
219 |
|
220 |
+
cap = cv2.VideoCapture(url)
|
221 |
+
|
222 |
+
while True:
|
223 |
+
if self._preview:
|
224 |
+
if stop_flag:
|
225 |
+
print("Process Stopped")
|
226 |
+
return
|
227 |
+
|
228 |
+
grabbed, frame = cap.read()
|
229 |
+
if not grabbed:
|
230 |
+
break
|
231 |
+
if self.flipH:
|
232 |
+
frame = cv2.flip(frame, 1)
|
233 |
+
|
234 |
+
if self.detect:
|
235 |
+
frame_yolo = frame.copy()
|
236 |
+
results_yolo = model_object_detection.predict(frame_yolo, conf=self._confidence / 100)
|
237 |
+
|
238 |
+
frame_yolo, labels = results_yolo[0].plot()
|
239 |
+
list_labels = []
|
240 |
+
# labels_confidences
|
241 |
+
|
242 |
+
for label in labels:
|
243 |
+
confidence = label.split(" ")[-1]
|
244 |
+
label_name = " ".join(label.split(" ")[:-1])
|
245 |
+
# Translate the label if it exists in the translation dictionary
|
246 |
+
translated_label = classes_translation.get(label_name, label_name)
|
247 |
+
list_labels.append(translated_label)
|
248 |
+
list_labels.append(confidence)
|
249 |
+
socketio.emit('label', list_labels)
|
250 |
+
|
251 |
+
if self.mediaPipe:
|
252 |
+
# Convert the image to RGB for processing with MediaPipe
|
253 |
+
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
254 |
+
results = self.hands.process(image)
|
255 |
+
|
256 |
+
if results.multi_hand_landmarks:
|
257 |
+
for hand_landmarks in results.multi_hand_landmarks:
|
258 |
+
mp.solutions.drawing_utils.draw_landmarks(
|
259 |
+
frame,
|
260 |
+
hand_landmarks,
|
261 |
+
self.mp_hands.HAND_CONNECTIONS,
|
262 |
+
landmark_drawing_spec=mp.solutions.drawing_utils.DrawingSpec(color=(255, 0, 0), thickness=4, circle_radius=3),
|
263 |
+
connection_drawing_spec=mp.solutions.drawing_utils.DrawingSpec(color=(255, 255, 255), thickness=2, circle_radius=2),
|
264 |
+
)
|
265 |
+
|
266 |
+
frame = cv2.imencode(".jpg", frame)[1].tobytes()
|
267 |
+
yield (
|
268 |
+
b'--frame\r\n'
|
269 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n'
|
270 |
+
)
|
271 |
+
else:
|
272 |
+
snap = np.zeros((
|
273 |
+
1000,
|
274 |
+
1000
|
275 |
+
), np.uint8)
|
276 |
+
label = "Streaming Off"
|
277 |
+
H, W = snap.shape
|
278 |
+
font = cv2.FONT_HERSHEY_PLAIN
|
279 |
+
color = (255, 255, 255)
|
280 |
+
cv2.putText(snap, label, (W//2 - 100, H//2),
|
281 |
+
font, 2, color, 2)
|
282 |
+
frame = cv2.imencode(".jpg", snap)[1].tobytes()
|
283 |
+
yield (b'--frame\r\n'
|
284 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
285 |
+
|
286 |
+
|
287 |
+
# check_settings()
|
288 |
+
VIDEO = VideoStreaming()
|
289 |
+
|
290 |
+
|
291 |
+
@app.route('/', methods=['GET', 'POST'])
|
292 |
+
def homepage():
|
293 |
+
return render_template('hompage.html')
|
294 |
+
|
295 |
+
|
296 |
+
@app.route('/index', methods=['GET', 'POST'])
|
297 |
+
def index():
|
298 |
+
print("index")
|
299 |
+
global stop_flag
|
300 |
+
stop_flag = False
|
301 |
+
if request.method == 'POST':
|
302 |
+
print("Index post request")
|
303 |
+
url = request.form['url']
|
304 |
+
print("index: ", url)
|
305 |
+
session['url'] = url
|
306 |
+
return redirect(url_for('index'))
|
307 |
+
return render_template('index.html')
|
308 |
|
|
|
309 |
@app.route('/video_feed')
|
310 |
def video_feed():
|
311 |
+
url = session.get('url', None)
|
312 |
+
print("video feed: ", url)
|
313 |
+
if url is None:
|
314 |
+
return redirect(url_for('homepage'))
|
315 |
+
print("video feed: ", url)
|
316 |
+
return Response(VIDEO.show(url), mimetype='multipart/x-mixed-replace; boundary=frame')
|
317 |
+
|
318 |
+
# * Button requests
|
319 |
+
@app.route("/request_preview_switch")
|
320 |
+
def request_preview_switch():
|
321 |
+
VIDEO.preview = not VIDEO.preview
|
322 |
+
print("*"*10, VIDEO.preview)
|
323 |
+
return "nothing"
|
324 |
+
|
325 |
+
@app.route("/request_flipH_switch")
|
326 |
+
def request_flipH_switch():
|
327 |
+
VIDEO.flipH = not VIDEO.flipH
|
328 |
+
print("*"*10, VIDEO.flipH)
|
329 |
+
return "nothing"
|
330 |
+
|
331 |
+
@app.route("/request_run_model_switch")
|
332 |
+
def request_run_model_switch():
|
333 |
+
VIDEO.detect = not VIDEO.detect
|
334 |
+
print("*"*10, VIDEO.detect)
|
335 |
+
return "nothing"
|
336 |
+
|
337 |
+
@app.route("/request_mediapipe_switch")
|
338 |
+
def request_mediapipe_switch():
|
339 |
+
VIDEO.mediaPipe = not VIDEO.mediaPipe
|
340 |
+
print("*"*10, VIDEO.mediaPipe)
|
341 |
+
return "nothing"
|
342 |
+
|
343 |
+
@app.route('/update_slider_value', methods=['POST'])
|
344 |
+
def update_slider_value():
|
345 |
+
slider_value = request.form['sliderValue']
|
346 |
+
VIDEO.confidence = slider_value
|
347 |
+
return 'OK'
|
348 |
+
|
349 |
+
@app.route('/stop_process')
|
350 |
+
def stop_process():
|
351 |
+
print("Process stop Request")
|
352 |
+
global stop_flag
|
353 |
+
stop_flag = True
|
354 |
+
return 'Process Stop Request'
|
355 |
+
|
356 |
+
@socketio.on('connect')
|
357 |
+
def test_connect():
|
358 |
+
print('Connected')
|
359 |
+
#emit('message', data, broadcast=True)
|
360 |
+
|
361 |
|
362 |
+
if __name__ == '__main__':
|
363 |
+
socketio.run(app, host="0.0.0.0", allow_unsafe_werkzeug=True,port=7860)
|
|