Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,48 @@
|
|
1 |
-
|
|
|
2 |
import psutil
|
3 |
-
import json
|
4 |
-
from ultralytics import YOLO
|
5 |
import cv2
|
6 |
import numpy as np
|
7 |
-
from
|
8 |
-
import
|
9 |
-
import
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
modelName = "yolov9c-seg.pt"
|
13 |
model = YOLO(modelName)
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
@app.route("/")
|
17 |
def home():
|
18 |
return render_template("index.html")
|
19 |
|
20 |
-
|
21 |
@app.route("/sysInfo")
|
22 |
def sysInfo():
|
23 |
ram = psutil.virtual_memory()
|
@@ -26,35 +51,46 @@ def sysInfo():
|
|
26 |
data = {"ram": ram_usage, "cpu": cpu_usage}
|
27 |
return jsonify(data)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
global modelName, model
|
33 |
-
image = request.form.get("image")
|
34 |
-
modelNameForm = request.form.get("model", modelName)
|
35 |
-
if modelNameForm != modelName:
|
36 |
-
modelName = modelNameForm
|
37 |
-
model = YOLO(modelName)
|
38 |
-
image = image.split(",")[1]
|
39 |
-
image = base64.b64decode(image)
|
40 |
-
image = Image.open(io.BytesIO(image))
|
41 |
-
image = np.array(image)
|
42 |
-
results = model(
|
43 |
-
image,
|
44 |
-
show=False,
|
45 |
-
save=False,
|
46 |
-
show_boxes=True,
|
47 |
-
show_labels=True,
|
48 |
-
imgsz=640,
|
49 |
-
iou=0.1,
|
50 |
-
max_det=20,
|
51 |
-
)
|
52 |
-
image = results[0].plot()
|
53 |
-
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
54 |
-
image = cv2.imencode(".jpg", image)[1].tobytes()
|
55 |
-
image = base64.b64encode(image).decode("utf-8")
|
56 |
-
return jsonify({"image": image})
|
57 |
-
|
58 |
-
|
59 |
-
app.run(debug=True, host="0.0.0.0", port=7860)
|
60 |
-
# app.run(debug=True)
|
|
|
1 |
+
# Flask Backend (app.py)
|
2 |
+
from flask import Flask, render_template, Response, jsonify
|
3 |
import psutil
|
|
|
|
|
4 |
import cv2
|
5 |
import numpy as np
|
6 |
+
from ultralytics import YOLO
|
7 |
+
import threading
|
8 |
+
import queue
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
modelName = "yolov9c-seg.pt"
|
12 |
model = YOLO(modelName)
|
13 |
|
14 |
+
# Global variables for frame handling
|
15 |
+
frame_queue = queue.Queue(maxsize=2)
|
16 |
+
processed_frame_queue = queue.Queue(maxsize=2)
|
17 |
+
current_frame = None
|
18 |
+
processing_active = False
|
19 |
+
|
20 |
+
def process_frames():
|
21 |
+
global processing_active
|
22 |
+
while processing_active:
|
23 |
+
if not frame_queue.empty():
|
24 |
+
frame = frame_queue.get()
|
25 |
+
results = model(
|
26 |
+
frame,
|
27 |
+
show=False,
|
28 |
+
save=False,
|
29 |
+
show_boxes=True,
|
30 |
+
show_labels=True,
|
31 |
+
imgsz=640,
|
32 |
+
iou=0.1,
|
33 |
+
max_det=20,
|
34 |
+
)
|
35 |
+
processed_frame = results[0].plot()
|
36 |
+
processed_frame = cv2.cvtColor(np.array(processed_frame), cv2.COLOR_RGB2BGR)
|
37 |
+
|
38 |
+
if processed_frame_queue.full():
|
39 |
+
processed_frame_queue.get() # Remove old frame
|
40 |
+
processed_frame_queue.put(processed_frame)
|
41 |
|
42 |
@app.route("/")
|
43 |
def home():
|
44 |
return render_template("index.html")
|
45 |
|
|
|
46 |
@app.route("/sysInfo")
|
47 |
def sysInfo():
|
48 |
ram = psutil.virtual_memory()
|
|
|
51 |
data = {"ram": ram_usage, "cpu": cpu_usage}
|
52 |
return jsonify(data)
|
53 |
|
54 |
+
def generate_frames():
|
55 |
+
global processing_active
|
56 |
+
while True:
|
57 |
+
if not processed_frame_queue.empty():
|
58 |
+
frame = processed_frame_queue.get()
|
59 |
+
ret, buffer = cv2.imencode('.jpg', frame)
|
60 |
+
frame = buffer.tobytes()
|
61 |
+
yield (b'--frame\r\n'
|
62 |
+
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
|
63 |
+
|
64 |
+
@app.route('/video_feed')
|
65 |
+
def video_feed():
|
66 |
+
return Response(generate_frames(),
|
67 |
+
mimetype='multipart/x-mixed-replace; boundary=frame')
|
68 |
+
|
69 |
+
@app.route('/start_processing')
|
70 |
+
def start_processing():
|
71 |
+
global processing_active
|
72 |
+
if not processing_active:
|
73 |
+
processing_active = True
|
74 |
+
threading.Thread(target=process_frames, daemon=True).start()
|
75 |
+
return jsonify({"status": "started"})
|
76 |
+
|
77 |
+
@app.route('/stop_processing')
|
78 |
+
def stop_processing():
|
79 |
+
global processing_active
|
80 |
+
processing_active = False
|
81 |
+
return jsonify({"status": "stopped"})
|
82 |
+
|
83 |
+
@app.route('/update_frame', methods=['POST'])
|
84 |
+
def update_frame():
|
85 |
+
global current_frame
|
86 |
+
data = request.get_json()
|
87 |
+
frame_data = np.array(data['frame'])
|
88 |
+
|
89 |
+
if frame_queue.full():
|
90 |
+
frame_queue.get() # Remove old frame
|
91 |
+
frame_queue.put(frame_data)
|
92 |
+
|
93 |
+
return jsonify({"status": "success"})
|
94 |
|
95 |
+
if __name__ == "__main__":
|
96 |
+
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|