-
Notifications
You must be signed in to change notification settings - Fork 1
/
old-app.py
43 lines (29 loc) · 1.12 KB
/
old-app.py
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
import argparse
parser = argparse.ArgumentParser(description='Start the Flask app with a YOLO model and video input.')
parser.add_argument('model_path', type=str, help='Path to the YOLO model file')
parser.add_argument('video_path', type=str, help='Path to the video file')
args = parser.parse_args()
import cv2
from flask import Flask, Response
from ultralytics import YOLO
app = Flask(__name__)
model = YOLO(args.model_path, task="detect")
if args.video_path.isdigit():
cap = cv2.VideoCapture(int(args.video_path))
else:
cap = cv2.VideoCapture(args.video_path)
def generate_frames():
while cap.isOpened():
success, frame = cap.read()
if not success:
break
results = model(frame)
annotated_frame = results[0].plot()
_, buffer = cv2.imencode('.jpg', annotated_frame)
frame_bytes = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')
@app.route('/')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
app.run(host='0.0.0.0', port=5000)