-
Notifications
You must be signed in to change notification settings - Fork 23
/
yolo.py
308 lines (263 loc) · 12.2 KB
/
yolo.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import os, random, yaml, argparse
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"
random.seed(0)
import cv2
import torch
import numpy as np
from yolo_utils.utils import non_max_suppression, letterbox, scale_coords, plot_one_box
from ultralytics import YOLO, RTDETR
def ByteTrack_opt():
parser = argparse.ArgumentParser("ByteTrack Param.")
parser.add_argument("--track_thresh", type=float, default=0.5, help="tracking confidence threshold")
parser.add_argument("--track_buffer", type=int, default=30, help="the frames for keep lost tracks")
parser.add_argument("--match_thresh", type=float, default=0.8, help="matching threshold for tracking")
parser.add_argument(
"--aspect_ratio_thresh", type=float, default=1.6,
help="threshold for filtering out boxes of which aspect ratio are above the given value."
)
parser.add_argument('--min_box_area', type=float, default=10, help='filter out tiny boxes')
parser.add_argument("--fps", default=25, type=int, help="frame rate (fps)")
parser.add_argument("--mot20", dest="mot20", default=False, action="store_true", help="test mot20.")
return parser.parse_args()
class base_model:
def __init__(self, model_path, iou_thres, conf_thres, device, names, imgsz, **kwargs):
device = self.select_device(device)
print(device)
if model_path.endswith('pt'):
model = torch.jit.load(model_path).to(device)
elif model_path.endswith('onnx'):
try:
import onnxruntime as ort
except:
raise 'please install onnxruntime.'
providers = ['CUDAExecutionProvider'] if device.type != 'cpu' else ['CPUExecutionProvider']
model = ort.InferenceSession(model_path, providers=providers)
colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
self.__dict__.update(locals())
def __call__(self, data):
if type(data) is str:
image = cv2.imdecode(np.fromfile(data, np.uint8), cv2.IMREAD_COLOR)
else:
image = data
im = self.processing(image)
if self.model_path.endswith('pt'):
result = self.model(im)[0]
elif self.model_path.endswith('onnx'):
result = self.model.run([i.name for i in self.model.get_outputs()], {'images':im})[0]
return self.post_processing(result, im, image)
def processing(self, img):
image = letterbox(img, new_shape=tuple(self.imgsz), auto=False)[0]
image = image.transpose((2, 0, 1))[::-1]
image = np.expand_dims(image, 0)
image = np.ascontiguousarray(image)
image = np.array(image, dtype=np.float32)
image /= 255
if self.model_path.endswith('pt'):
im = torch.from_numpy(image).float().to(self.device)
elif self.model_path.endswith('onnx'):
im = image
return im
def post_processing(self, result, im=None, img=None):
pass
def select_device(self, device):
if device == -1:
return torch.device('cpu')
else:
os.environ['CUDA_VISIBLE_DEVICES'] = str(device)
assert torch.cuda.is_available(), f'CUDA unavailable, invalid device {device} requested' # check availability
device = torch.device('cuda:0')
return device
def track_init(self, track_type):
from track_utils.byte_tracker import BYTETracker, BaseTrack
if track_type == 'ByteTrack':
self.track_opt = ByteTrack_opt()
self.tracker = BYTETracker(self.track_opt, frame_rate=self.track_opt.fps)
BaseTrack._count = 0
def track_processing(self, frame, det_result):
if type(det_result) is torch.Tensor:
det_result = det_result.cpu().detach().numpy()
online_targets = self.tracker.update(det_result[:, :5], frame.shape[:2], [640, 640])
for t in online_targets:
tlwh = t.tlwh
tid = t.track_id
vertical = tlwh[2] / tlwh[3] > self.track_opt.aspect_ratio_thresh
if tlwh[2] * tlwh[3] > self.track_opt.min_box_area and not vertical:
plot_one_box([tlwh[0], tlwh[1], tlwh[0] + tlwh[2], tlwh[1] + tlwh[3]], frame, (0, 0, 255), str(tid))
return frame
class yolov7(base_model):
def __init__(self, model_path, iou_thres, conf_thres, device, names, imgsz, **kwargs):
super().__init__(model_path, iou_thres, conf_thres, device, names, imgsz, **kwargs)
def post_processing(self, result, im=None, img=None):
if self.model_path.endswith('pt'):
result = non_max_suppression(result, conf_thres=self.conf_thres, iou_thres=self.iou_thres)[0]
result[:, :4] = scale_coords(im.shape[2:], result[:, :4], img.shape)
for *xyxy, conf, cls in result:
label = f'{self.names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, img, label=label, color=self.colors[int(cls)])
elif self.model_path.endswith('onnx'):
result = result[:, 1:]
ratio, dwdh = letterbox(img, new_shape=tuple(self.imgsz), auto=False)[1:]
result[:, :4] -= np.array(dwdh * 2)
result[:, :4] /= ratio
result[:, [4, 5]] = result[:, [5, 4]] # xyxy, cls, conf => xyxy, conf, cls
for *xyxy, conf, cls in result:
label = f'{self.names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, img, label=label, color=self.colors[int(cls)])
return img, result
class yolov5(base_model):
def __init__(self, model_path, iou_thres, conf_thres, device, names, imgsz, **kwargs):
super().__init__(model_path, iou_thres, conf_thres, device, names, imgsz, **kwargs)
def post_processing(self, result, im=None, img=None):
if self.model_path.endswith('pt'):
result = non_max_suppression(result, conf_thres=self.conf_thres, iou_thres=self.iou_thres)[0]
result[:, :4] = scale_coords(im.shape[2:], result[:, :4], img.shape)
for *xyxy, conf, cls in result:
label = f'{self.names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, img, label=label, color=self.colors[int(cls)])
elif self.model_path.endswith('onnx'):
result = non_max_suppression(torch.from_numpy(result), conf_thres=self.conf_thres, iou_thres=self.iou_thres)[0]
result[:, :4] = scale_coords(im.shape[2:], result[:, :4], img.shape)
for *xyxy, conf, cls in result:
label = f'{self.names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, img, label=label, color=self.colors[int(cls)])
return img, result
class yolov8:
def __init__(self, model_path, iou_thres, conf_thres, device, names, imgsz, **kwargs) -> None:
print(model_path)
model = YOLO(model_path)
model.info()
colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
self.__dict__.update(locals())
def __call__(self, data):
if type(data) is str:
image = cv2.imdecode(np.fromfile(data, np.uint8), cv2.IMREAD_COLOR)
else:
image = data
result = next(self.model.predict(source=image, stream=True, iou=self.iou_thres, conf=self.conf_thres, imgsz=self.imgsz, save=False, device=self.device))
result = result.boxes.data.cpu().detach().numpy()
for *xyxy, conf, cls in result:
label = f'{self.names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, image, label=label, color=self.colors[int(cls)])
return image, result
def track_init(self, track_type):
from track_utils.byte_tracker import BYTETracker, BaseTrack
if track_type == 'ByteTrack':
self.track_opt = ByteTrack_opt()
self.tracker = BYTETracker(self.track_opt, frame_rate=self.track_opt.fps)
BaseTrack._count = 0
def track_processing(self, frame, det_result):
if type(det_result) is torch.Tensor:
det_result = det_result.cpu().detach().numpy()
online_targets = self.tracker.update(det_result[:, :5], frame.shape[:2], [640, 640])
for t in online_targets:
tlwh = t.tlwh
tid = t.track_id
vertical = tlwh[2] / tlwh[3] > self.track_opt.aspect_ratio_thresh
if tlwh[2] * tlwh[3] > self.track_opt.min_box_area and not vertical:
plot_one_box([tlwh[0], tlwh[1], tlwh[0] + tlwh[2], tlwh[1] + tlwh[3]], frame, (0, 0, 255), str(tid))
return frame
class rtdetr:
def __init__(self, model_path, iou_thres, conf_thres, device, names, imgsz, **kwargs) -> None:
model = RTDETR(model_path)
model.info()
colors = [[random.randint(0, 255) for _ in range(3)] for _ in names]
self.__dict__.update(locals())
def __call__(self, data):
if type(data) is str:
image = cv2.imdecode(np.fromfile(data, np.uint8), cv2.IMREAD_COLOR)
else:
image = data
result = next(self.model.predict(source=image, stream=True, iou=self.iou_thres, conf=self.conf_thres, imgsz=self.imgsz, save=False, device=self.device))
result = result.boxes.data.cpu().detach().numpy()
for *xyxy, conf, cls in result:
label = f'{self.names[int(cls)]} {conf:.2f}'
plot_one_box(xyxy, image, label=label, color=self.colors[int(cls)])
return image, result
def track_init(self, track_type):
from track_utils.byte_tracker import BYTETracker, BaseTrack
if track_type == 'ByteTrack':
self.track_opt = ByteTrack_opt()
self.tracker = BYTETracker(self.track_opt, frame_rate=self.track_opt.fps)
BaseTrack._count = 0
def track_processing(self, frame, det_result):
if type(det_result) is torch.Tensor:
det_result = det_result.cpu().detach().numpy()
online_targets = self.tracker.update(det_result[:, :5], frame.shape[:2], [640, 640])
for t in online_targets:
tlwh = t.tlwh
tid = t.track_id
vertical = tlwh[2] / tlwh[3] > self.track_opt.aspect_ratio_thresh
if tlwh[2] * tlwh[3] > self.track_opt.min_box_area and not vertical:
plot_one_box([tlwh[0], tlwh[1], tlwh[0] + tlwh[2], tlwh[1] + tlwh[3]], frame, (0, 0, 255), str(tid))
return frame
def test_yolov7():
# read cfg
with open('yolov7-tiny.yaml') as f:
cfg = yaml.load(f, Loader=yaml.SafeLoader)
# print cfg
print(cfg)
# init
yolo = yolov7(**cfg)
image_path = '1.jpg'
# inference
image, _ = yolo(image_path)
cv2.imshow('pic', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def test_yolov5():
# read cfg
with open('yolov5s.yaml') as f:
cfg = yaml.load(f, Loader=yaml.SafeLoader)
# print cfg
print(cfg)
# init
yolo = yolov5(**cfg)
image_path = '2.jpg'
# inference
image, _ = yolo(image_path)
cv2.imshow('pic', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
def test_yolov5_track():
from track_utils.byte_tracker import BYTETracker
# read cfg
with open('yolov5s.yaml') as f:
cfg = yaml.load(f, Loader=yaml.SafeLoader)
# print cfg
print(cfg)
# init
yolo = yolov5(**cfg)
yolo.track_init('ByteTrack')
cap = cv2.VideoCapture('2.mp4')
while True:
ret, frame = cap.read()
if frame is None:
break
image, result = yolo(frame.copy())
image = yolo.track_processing(frame.copy(), result)
cv2.imshow('pic', image)
cv2.waitKey(20)
def test_yolov7_track():
from track_utils.byte_tracker import BYTETracker
# read cfg
with open('yolov7-tiny.yaml') as f:
cfg = yaml.load(f, Loader=yaml.SafeLoader)
# print cfg
print(cfg)
# init
yolo = yolov7(**cfg)
yolo.track_init('ByteTrack')
cap = cv2.VideoCapture('1.mp4')
while True:
ret, frame = cap.read()
if frame is None:
break
image, result = yolo(frame.copy())
image = yolo.track_processing(frame.copy(), result)
cv2.imshow('pic', image)
cv2.waitKey(20)
if __name__ == '__main__':
# test_yolov5()
# test_yolov7()
test_yolov5_track()
test_yolov7_track()