-
Notifications
You must be signed in to change notification settings - Fork 233
/
demo.py
171 lines (148 loc) · 5.41 KB
/
demo.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
import argparse
import os
from typing import List, Optional, Union
import numpy as np
import torch
import torchvision.ops.boxes as bops
import norfair
from norfair import Detection, Paths, Tracker, Video
from norfair.distances import frobenius, iou
DISTANCE_THRESHOLD_BBOX: float = 0.7
DISTANCE_THRESHOLD_CENTROID: int = 30
MAX_DISTANCE: int = 10000
class YOLO:
def __init__(self, model_path: str, device: Optional[str] = None):
if device is not None and "cuda" in device and not torch.cuda.is_available():
raise Exception(
"Selected device='cuda', but cuda is not available to Pytorch."
)
# automatically set device if its None
elif device is None:
device = "cuda:0" if torch.cuda.is_available() else "cpu"
if not os.path.exists(model_path):
os.system(
f"wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/{os.path.basename(model_path)} -O {model_path}"
)
# load model
try:
self.model = torch.hub.load("WongKinYiu/yolov7", "custom", model_path)
except:
raise Exception("Failed to load model from {}".format(model_path))
def __call__(
self,
img: Union[str, np.ndarray],
conf_threshold: float = 0.25,
iou_threshold: float = 0.45,
image_size: int = 720,
classes: Optional[List[int]] = None,
) -> torch.tensor:
self.model.conf = conf_threshold
self.model.iou = iou_threshold
if classes is not None:
self.model.classes = classes
detections = self.model(img, size=image_size)
return detections
def center(points):
return [np.mean(np.array(points), axis=0)]
def yolo_detections_to_norfair_detections(
yolo_detections: torch.tensor, track_points: str = "centroid" # bbox or centroid
) -> List[Detection]:
"""convert detections_as_xywh to norfair detections"""
norfair_detections: List[Detection] = []
if track_points == "centroid":
detections_as_xywh = yolo_detections.xywh[0]
for detection_as_xywh in detections_as_xywh:
centroid = np.array(
[detection_as_xywh[0].item(), detection_as_xywh[1].item()]
)
scores = np.array([detection_as_xywh[4].item()])
norfair_detections.append(
Detection(
points=centroid,
scores=scores,
label=int(detection_as_xywh[-1].item()),
)
)
elif track_points == "bbox":
detections_as_xyxy = yolo_detections.xyxy[0]
for detection_as_xyxy in detections_as_xyxy:
bbox = np.array(
[
[detection_as_xyxy[0].item(), detection_as_xyxy[1].item()],
[detection_as_xyxy[2].item(), detection_as_xyxy[3].item()],
]
)
scores = np.array(
[detection_as_xyxy[4].item(), detection_as_xyxy[4].item()]
)
norfair_detections.append(
Detection(
points=bbox, scores=scores, label=int(detection_as_xyxy[-1].item())
)
)
return norfair_detections
parser = argparse.ArgumentParser(description="Track objects in a video.")
parser.add_argument("files", type=str, nargs="+", help="Video files to process")
parser.add_argument(
"--detector-path", type=str, default="/yolov7.pt", help="YOLOv7 model path"
)
parser.add_argument(
"--img-size", type=int, default="720", help="YOLOv7 inference size (pixels)"
)
parser.add_argument(
"--conf-threshold",
type=float,
default="0.25",
help="YOLOv7 object confidence threshold",
)
parser.add_argument(
"--iou-threshold", type=float, default="0.45", help="YOLOv7 IOU threshold for NMS"
)
parser.add_argument(
"--classes",
nargs="+",
type=int,
help="Filter by class: --classes 0, or --classes 0 2 3",
)
parser.add_argument(
"--device", type=str, default=None, help="Inference device: 'cpu' or 'cuda'"
)
parser.add_argument(
"--track-points",
type=str,
default="bbox",
help="Track points: 'centroid' or 'bbox'",
)
args = parser.parse_args()
model = YOLO(args.detector_path, device=args.device)
for input_path in args.files:
video = Video(input_path=input_path)
distance_function = iou if args.track_points == "bbox" else frobenius
distance_threshold = (
DISTANCE_THRESHOLD_BBOX
if args.track_points == "bbox"
else DISTANCE_THRESHOLD_CENTROID
)
tracker = Tracker(
distance_function=distance_function,
distance_threshold=distance_threshold,
)
for frame in video:
yolo_detections = model(
frame,
conf_threshold=args.conf_threshold,
iou_threshold=args.iou_threshold,
image_size=args.img_size,
classes=args.classes,
)
detections = yolo_detections_to_norfair_detections(
yolo_detections, track_points=args.track_points
)
tracked_objects = tracker.update(detections=detections)
if args.track_points == "centroid":
norfair.draw_points(frame, detections)
norfair.draw_tracked_objects(frame, tracked_objects)
elif args.track_points == "bbox":
norfair.draw_boxes(frame, detections)
norfair.draw_tracked_boxes(frame, tracked_objects)
video.write(frame)