-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp.py
More file actions
91 lines (73 loc) · 2.64 KB
/
app.py
File metadata and controls
91 lines (73 loc) · 2.64 KB
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
from flask import Flask, render_template, Response, jsonify
import cv2
import supervision as sv
from ultralytics import YOLO
import time
import pyresearch
# Flask App Initialization
app = Flask(__name__)
# PyResearch Configuration Constants
PR_MODEL_PATH = "best.pt"
PR_DISPLAY_CONFIG = {
'window_title': "PyResearch - Pothole Computer Vision Project",
'window_size': (1280, 720),
'color_scheme': "PR_DARK_BLUE",
'fps_display': True
}
# Global variable to store detection count
detection_count = 0
class PyResearchVisualizer:
"""PyResearch Standard Visualization Engine"""
def __init__(self):
self.model = YOLO(PR_MODEL_PATH)
self.box_annotator = sv.BoundingBoxAnnotator(
thickness=2,
color=sv.Color.from_hex("#0055FF")
)
self.label_annotator = sv.LabelAnnotator(
text_scale=0.7,
text_thickness=1,
text_color=sv.Color.WHITE,
text_padding=10
)
def process_frame(self, frame):
"""PyResearch Standard Processing Pipeline"""
global detection_count
results = self.model(frame)[0]
detections = sv.Detections.from_ultralytics(results)
# Update detection count
detection_count = len(detections) # Count the number of detections in the current frame
# Apply PyResearch Visualization Standards
annotated_frame = self.box_annotator.annotate(
scene=frame,
detections=detections
)
annotated_frame = self.label_annotator.annotate(
scene=annotated_frame,
detections=detections
)
return annotated_frame
def generate_frames():
visualizer = PyResearchVisualizer()
cap = cv2.VideoCapture("demo.mp4")
while cap.isOpened():
success, frame = cap.read()
if not success:
break
output_frame = visualizer.process_frame(frame)
_, buffer = cv2.imencode('.jpg', output_frame)
frame_bytes = buffer.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame_bytes + b'\r\n')
cap.release()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/video_feed')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
@app.route('/detection_count')
def get_detection_count():
return jsonify({'detections': detection_count})
if __name__ == "__main__":
app.run(debug=True, host='0.0.0.0')