-
Notifications
You must be signed in to change notification settings - Fork 1
/
YOLOv7_bytetrack.py
321 lines (264 loc) · 14.1 KB
/
YOLOv7_bytetrack.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
309
310
311
312
313
314
315
316
317
318
319
320
321
import argparse
import time
import cv2
import numpy as np
from openvino.runtime import Core # pip install openvino -i https://pypi.tuna.tsinghua.edu.cn/simple
import onnxruntime as ort # 使用onnxruntime推理用上,pip install onnxruntime,默认安装CPU
import copy
from bytetrack.byte_tracker import BYTETracker
# COCO默认的80类
CLASSES = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow',
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee',
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich',
'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven',
'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
class OpenvinoInference(object):
def __init__(self, onnx_path):
self.onnx_path = onnx_path
ie = Core()
self.model_onnx = ie.read_model(model=self.onnx_path)
self.compiled_model_onnx = ie.compile_model(model=self.model_onnx, device_name="CPU")
self.output_layer_onnx = self.compiled_model_onnx.output(0)
def predict(self, datas):
predict_data = self.compiled_model_onnx([datas])[self.output_layer_onnx]
return predict_data
class YOLOv7:
"""YOLOv7 object detection model class for handling inference and visualization."""
def __init__(self, onnx_model, imgsz=(640, 640), infer_tool='openvino'):
"""
Initialization.
Args:
onnx_model (str): Path to the ONNX model.
"""
self.infer_tool = infer_tool
if self.infer_tool == 'openvino':
# 构建openvino推理引擎
self.openvino = OpenvinoInference(onnx_model)
self.ndtype = np.single
else:
# 构建onnxruntime推理引擎
self.ort_session = ort.InferenceSession(onnx_model,
providers=['CUDAExecutionProvider', 'CPUExecutionProvider']
if ort.get_device() == 'GPU' else ['CPUExecutionProvider'])
# Numpy dtype: support both FP32 and FP16 onnx model
self.ndtype = np.half if self.ort_session.get_inputs()[0].type == 'tensor(float16)' else np.single
self.classes = CLASSES # 加载模型类别
self.model_height, self.model_width = imgsz[0], imgsz[1] # 图像resize大小
self.color_palette = np.random.uniform(0, 255, size=(len(self.classes), 3)) # 为每个类别生成调色板
def __call__(self, im0, conf_threshold=0.4, iou_threshold=0.45):
"""
The whole pipeline: pre-process -> inference -> post-process.
Args:
im0 (Numpy.ndarray): original input image.
conf_threshold (float): confidence threshold for filtering predictions.
iou_threshold (float): iou threshold for NMS.
Returns:
boxes (List): list of bounding boxes.
"""
# 前处理Pre-process
t1 = time.time()
im, ratio, (pad_w, pad_h) = self.preprocess(im0)
print('预处理时间:{:.3f}s'.format(time.time() - t1))
# 推理 inference
t2 = time.time()
if self.infer_tool == 'openvino':
preds = self.openvino.predict(im)
else:
preds = self.ort_session.run(None, {self.ort_session.get_inputs()[0].name: im})[0]
print('推理时间:{:.3f}s'.format(time.time() - t2))
# 后处理Post-process
t3 = time.time()
boxes = self.postprocess(preds,
im0=im0,
ratio=ratio,
pad_w=pad_w,
pad_h=pad_h,
conf_threshold=conf_threshold,
iou_threshold=iou_threshold,
)
print('后处理时间:{:.3f}s'.format(time.time() - t3))
return boxes
# 前处理,包括:resize, pad, HWC to CHW,BGR to RGB,归一化,增加维度CHW -> BCHW
def preprocess(self, img):
"""
Pre-processes the input image.
Args:
img (Numpy.ndarray): image about to be processed.
Returns:
img_process (Numpy.ndarray): image preprocessed for inference.
ratio (tuple): width, height ratios in letterbox.
pad_w (float): width padding in letterbox.
pad_h (float): height padding in letterbox.
"""
# Resize and pad input image using letterbox() (Borrowed from Ultralytics)
shape = img.shape[:2] # original image shape
new_shape = (self.model_height, self.model_width)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
ratio = r, r
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
pad_w, pad_h = (new_shape[1] - new_unpad[0]) / 2, (new_shape[0] - new_unpad[1]) / 2 # wh padding
if shape[::-1] != new_unpad: # resize
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(pad_h - 0.1)), int(round(pad_h + 0.1))
left, right = int(round(pad_w - 0.1)), int(round(pad_w + 0.1))
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=(114, 114, 114)) # 填充
# Transforms: HWC to CHW -> BGR to RGB -> div(255) -> contiguous -> add axis(optional)
img = np.ascontiguousarray(np.einsum('HWC->CHW', img)[::-1], dtype=self.ndtype) / 255.0
img_process = img[None] if len(img.shape) == 3 else img
return img_process, ratio, (pad_w, pad_h)
# 后处理,包括:阈值过滤与NMS
def postprocess(self, preds, im0, ratio, pad_w, pad_h, conf_threshold, iou_threshold):
"""
Post-process the prediction.
Args:
preds (Numpy.ndarray): predictions come from ort.session.run().
im0 (Numpy.ndarray): [h, w, c] original input image.
ratio (tuple): width, height ratios in letterbox.
pad_w (float): width padding in letterbox.
pad_h (float): height padding in letterbox.
conf_threshold (float): conf threshold.
iou_threshold (float): iou threshold.
Returns:
boxes (List): list of bounding boxes.
"""
# (Batch_size, Num_anchors, xywh_score_conf_cls), v5和v6_1.0的[..., 4]是置信度分数,v8v9采用类别里面最大的概率作为置信度score
x = preds # outputs: predictions (1, 8400*3, 85)
# Predictions filtering by conf-threshold
x = x[x[..., 4] > conf_threshold]
# Create a new matrix which merge these(box, score, cls) into one
# For more details about `numpy.c_()`: https://numpy.org/doc/1.26/reference/generated/numpy.c_.html
x = np.c_[x[..., :4], x[..., 4], np.argmax(x[..., 5:], axis=-1)]
# NMS filtering
# 经过NMS后的值, np.array([[x, y, w, h, conf, cls], ...]), shape=(-1, 4 + 1 + 1)
x = x[cv2.dnn.NMSBoxes(x[:, :4], x[:, 4], conf_threshold, iou_threshold)]
# 重新缩放边界框,为画图做准备
if len(x) > 0:
# Bounding boxes format change: cxcywh -> xyxy
x[..., [0, 1]] -= x[..., [2, 3]] / 2
x[..., [2, 3]] += x[..., [0, 1]]
# Rescales bounding boxes from model shape(model_height, model_width) to the shape of original image
x[..., :4] -= [pad_w, pad_h, pad_w, pad_h]
x[..., :4] /= min(ratio)
# Bounding boxes boundary clamp
x[..., [0, 2]] = x[:, [0, 2]].clip(0, im0.shape[1])
x[..., [1, 3]] = x[:, [1, 3]].clip(0, im0.shape[0])
return x[..., :6] # boxes
else:
return []
# 绘框
def draw_and_visualize(self, im, bboxes, video_writer, vis=False, save=False, is_track=False):
"""
Draw and visualize results.
Args:
im (np.ndarray): original image, shape [h, w, c].
bboxes (numpy.ndarray): [n, 6], n is number of bboxes.
vis (bool): imshow using OpenCV.
save (bool): save image annotated.
Returns:
None
"""
# Draw rectangles
if not is_track:
for (*box, conf, cls_) in bboxes:
# draw bbox rectangle
cv2.rectangle(im, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])),
self.color_palette[int(cls_)], 1, cv2.LINE_AA)
cv2.putText(im, f'{self.classes[int(cls_)]}: {conf:.3f}', (int(box[0]), int(box[1] - 9)),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, self.color_palette[int(cls_)], 2, cv2.LINE_AA)
else:
for (*box, conf, id_) in bboxes:
# draw bbox rectangle
cv2.rectangle(im, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])),
(0, 0, 255), 1, cv2.LINE_AA)
cv2.putText(im, f'{id_}: {conf:.3f}', (int(box[0]), int(box[1] - 9)),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2, cv2.LINE_AA)
# Show image
if vis:
cv2.imshow('demo', im)
cv2.waitKey(1)
# Save video
if save:
video_writer.write(im)
class ByteTrackerONNX(object):
def __init__(self, args):
self.args = args
self.tracker = BYTETracker(args, frame_rate=30)
def _tracker_update(self, dets, image):
online_targets = []
if dets is not None:
online_targets = self.tracker.update(
dets[:, :5],
[image.shape[0], image.shape[1]],
[image.shape[0], image.shape[1]],
)
online_tlwhs = []
online_ids = []
online_scores = []
for online_target in online_targets:
tlwh = online_target.tlwh
track_id = online_target.track_id
vertical = tlwh[2] / tlwh[3] > 1.6
if tlwh[2] * tlwh[3] > self.args.min_box_area and not vertical:
online_tlwhs.append(tlwh)
online_ids.append(track_id)
online_scores.append(online_target.score)
return online_tlwhs, online_ids, online_scores
def inference(self, image, dets):
"""
Args: dets: 检测结果, [x1, y1, x2, y2, conf, cls]
Returns: np.array([[x1, y1, x2, y2, conf, ids], ...])
"""
bboxes, ids, scores = self._tracker_update(dets, image)
if len(bboxes) == 0:
return []
# Bounding boxes format change: tlwh -> xyxy
bboxes = np.array(bboxes)
bboxes[..., [2, 3]] += bboxes[..., [0, 1]]
bboxes = np.c_[bboxes, np.array(scores), np.array(ids)]
return bboxes
if __name__ == '__main__':
# Create an argument parser to handle command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default='weights/yolov7.onnx', help='Path to ONNX model')
parser.add_argument('--source', type=str, default=str('test.mp4'), help='Path to input image')
parser.add_argument('--imgsz', type=tuple, default=(640, 640), help='Image input size')
parser.add_argument('--conf', type=float, default=0.25, help='Confidence threshold')
parser.add_argument('--iou', type=float, default=0.45, help='NMS IoU threshold')
parser.add_argument('--infer_tool', type=str, default='openvino', choices=("openvino", "onnxruntime"), help='选择推理引擎')
parser.add_argument('--is_track', type=bool, default=True, help='是否启用跟踪')
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, usually as same with FPS')
parser.add_argument('--match_thresh', type=float, default=0.8, help='matching threshold for tracking')
parser.add_argument('--min_box_area', type=float, default=10, help='filter out tiny boxes',)
parser.add_argument('--mot20', dest='mot20', default=False, action='store_true', help='test mot20.',)
args = parser.parse_args()
# Build model
model = YOLOv7(args.model, args.imgsz, args.infer_tool)
bytetrack = ByteTrackerONNX(args)
# 读取视频,解析帧数宽高,保存视频
cap = cv2.VideoCapture(args.source)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
video_writer = cv2.VideoWriter('demo.mp4', cv2.VideoWriter_fourcc(*"mp4v"), fps, (int(width), int(height)))
frame_id = 1
while True:
start_time = time.time()
ret, img = cap.read()
if not ret:
break
# Inference
boxes = model(img, conf_threshold=args.conf, iou_threshold=args.iou)
# track
if args.is_track:
boxes = bytetrack.inference(img, boxes)
# Visualize
if len(boxes) > 0:
model.draw_and_visualize(copy.deepcopy(img), boxes, video_writer, vis=True, save=False, is_track=args.is_track)
end_time = time.time() - start_time
print('frame {}/{} (Total time: {:.2f} ms)'.format(frame_id, int(frame_count), end_time * 1000))
frame_id += 1