File size: 2,797 Bytes
36505bb
 
a8ea06f
 
 
36505bb
 
 
a8ea06f
 
28bf7eb
 
a8ea06f
36505bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8ea06f
 
 
 
 
 
 
 
 
 
 
 
 
36505bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a8ea06f
36505bb
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Flask Backend (app.py)
from flask import Flask, render_template, Response, jsonify
import psutil
import cv2
import numpy as np
from ultralytics import YOLO
import threading
import queue

app = Flask(__name__)
modelName = "yolov9c-seg.pt"
model = YOLO(modelName)

# Global variables for frame handling
frame_queue = queue.Queue(maxsize=2)
processed_frame_queue = queue.Queue(maxsize=2)
current_frame = None
processing_active = False

def process_frames():
    global processing_active
    while processing_active:
        if not frame_queue.empty():
            frame = frame_queue.get()
            results = model(
                frame,
                show=False,
                save=False,
                show_boxes=True,
                show_labels=True,
                imgsz=640,
                iou=0.1,
                max_det=20,
            )
            processed_frame = results[0].plot()
            processed_frame = cv2.cvtColor(np.array(processed_frame), cv2.COLOR_RGB2BGR)
            
            if processed_frame_queue.full():
                processed_frame_queue.get()  # Remove old frame
            processed_frame_queue.put(processed_frame)

@app.route("/")
def home():
    return render_template("index.html")

@app.route("/sysInfo")
def sysInfo():
    ram = psutil.virtual_memory()
    ram_usage = ram.percent
    cpu_usage = psutil.cpu_percent(interval=1)
    data = {"ram": ram_usage, "cpu": cpu_usage}
    return jsonify(data)

def generate_frames():
    global processing_active
    while True:
        if not processed_frame_queue.empty():
            frame = processed_frame_queue.get()
            ret, buffer = cv2.imencode('.jpg', frame)
            frame = buffer.tobytes()
            yield (b'--frame\r\n'
                   b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(generate_frames(),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/start_processing')
def start_processing():
    global processing_active
    if not processing_active:
        processing_active = True
        threading.Thread(target=process_frames, daemon=True).start()
    return jsonify({"status": "started"})

@app.route('/stop_processing')
def stop_processing():
    global processing_active
    processing_active = False
    return jsonify({"status": "stopped"})

@app.route('/update_frame', methods=['POST'])
def update_frame():
    global current_frame
    data = request.get_json()
    frame_data = np.array(data['frame'])
    
    if frame_queue.full():
        frame_queue.get()  # Remove old frame
    frame_queue.put(frame_data)
    
    return jsonify({"status": "success"})

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=7860)