-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.py
169 lines (143 loc) · 5.08 KB
/
stream.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import supervision as sv
import cv2
import time
import argparse
from ultralytics import YOLO
import matplotlib.pyplot as plt
import os
import PyQt5
from pathlib import Path
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = os.fspath(
Path(PyQt5.__file__).resolve().parent / "Qt5" / "plugins"
)
class ObjectDetection:
def __init__(self, source: str, target: str, confidence: float, weights: str, width: 1280, height: 960):
self.target = target
self.confidence = confidence
self.annotator = sv.BoundingBoxAnnotator()
self.frame_count = 0
if source.isnumeric():
self.source = int(source)
self.width = width
self.height = height
self.fps = 20
self.video_info = sv.VideoInfo(width=self.width, height=self.height, fps=self.fps)
else:
self.source = source
self.video_info = sv.VideoInfo.from_video_path(self.source)
self.width = self.video_info.width
self.height = self.video_info.height
self.fps = self.video_info.fps
self.tracker = sv.ByteTrack()
self.model = YOLO(weights)
self.label_annotator = sv.LabelAnnotator()
def display_fps(self, frame):
self.end_time = time.time()
fps = self.frame_count / (self.end_time - self.start_time)
text = f'FPS: {int(fps)}'
cv2.putText(frame, text, (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
self.start_time = time.time()
self.frame_count = 0
def display_time(self, frame):
current_time = time.time()
text = f'TIME: {int(current_time)}'
cv2.putText(frame, text, (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2)
def get_minimum_sized_detections(self, detections: sv.Detections) -> sv.Detections:
w = detections.xyxy[:, 2] - detections.xyxy[:, 0]
h = detections.xyxy[:, 3] - detections.xyxy[:, 1]
detections = detections[
(w / self.width > 0.05) & # > 5% width
(h / self.height > 0.05) # > 5% height
]
return detections
def predict(self, frame):
results = self.model(
frame, conf=self.confidence
)[0]
detections = sv.Detections.from_ultralytics(results)
#detections = self.get_minimum_sized_detections(detections)
detections = self.tracker.update_with_detections(detections)
labels = [
f"#{tracker_id} {results.names[class_id]}"
for class_id, tracker_id
in zip(detections.class_id, detections.tracker_id)
]
annotated_frame = self.annotator.annotate(
scene=frame, detections=detections
)
annotated_frame = self.label_annotator.annotate(
scene=frame, detections=detections, labels=labels
)
return annotated_frame
def run(self):
with sv.VideoSink(target_path=self.target, video_info=self.video_info) as sink:
cap = cv2.VideoCapture(self.source)
assert cap.isOpened()
cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.height)
self.start_time = time.time()
while True:
ret, frame = cap.read()
frame = self.predict(frame)
self.frame_count += 1
self.display_fps(frame)
#self.display_time(frame)
sink.write_frame(frame)
cv2.imshow('Object Detection', frame)
if cv2.waitKey(5) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()
def get_arguments():
parser = argparse.ArgumentParser(
description="Video Processing with YOLO and ByteTrack"
)
parser.add_argument(
"--weights",
required=True,
help="Path to the source weights file",
type=str,
)
parser.add_argument(
"--source",
required=True,
help="Path to the source video file",
type=str,
)
parser.add_argument(
"--target",
help="Path to the target video file (output)",
type=str,
default="result.mp4"
)
parser.add_argument(
"--confidence",
default=0.3,
help="Confidence threshold for the model",
type=float,
)
parser.add_argument(
"--width",
default=1280,
help="Resolution of camera",
type=int,
)
parser.add_argument(
"--height",
default=960,
help="Resolution of camera",
type=int,
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = get_arguments()
detection = ObjectDetection(
source=args.source,
target=args.target,
weights=args.weights,
confidence=args.confidence,
width=args.width,
height=args.height
)
detection.run()