-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract_tracking_sum_txt.py
246 lines (203 loc) · 9.98 KB
/
extract_tracking_sum_txt.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
# python interpreter searches these subdirectories for modules
import sys
sys.path.insert(0, './yolov5')
sys.path.insert(0, './sort')
import argparse
import os
import shutil
import time
from pathlib import Path
import cv2
import torch
import torch.backends.cudnn as cudnn
import numpy as np
# yolov5
from yolov5.models.common import DetectMultiBackend
from yolov5.utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages, LoadStreams
from yolov5.utils.general import check_img_size, non_max_suppression, scale_coords, check_file, \
check_requirements, print_args, check_imshow, increment_path, LOGGER, colorstr, strip_optimizer
from yolov5.utils.torch_utils import select_device, time_sync
from yolov5.utils.plots import Annotator
# SORT
import skimage
from sort import *
torch.set_printoptions(precision=3)
palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1)
FILE = Path(__file__).resolve()
ROOT = FILE.parents[0] # MoodangE_tracking root directory
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # add ROOT to PATH
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
def bbox_rel(*xyxy):
"""" Calculates the relative bounding box from absolute pixel values. """
bbox_left = min([xyxy[0].item(), xyxy[2].item()])
bbox_top = min([xyxy[1].item(), xyxy[3].item()])
bbox_w = abs(xyxy[0].item() - xyxy[2].item())
bbox_h = abs(xyxy[1].item() - xyxy[3].item())
x_c = (bbox_left + bbox_w / 2)
y_c = (bbox_top + bbox_h / 2)
w = bbox_w
h = bbox_h
return x_c, y_c, w, h
def compute_color_for_labels(label):
"""
Simple function that adds fixed color depending on the class
"""
color = [int((p * (label ** 2 - label + 1)) % 255) for p in palette]
return tuple(color)
def scene_boxes_data_txt(bbox, categories=None, names=None, offset=(0, 0)):
label_data = ''
for i, box in enumerate(bbox):
x1, y1, x2, y2 = [int(i) for i in box]
x1 += offset[0]
x2 += offset[0]
y1 += offset[1]
y2 += offset[1]
# box text and bar
cat = int(categories[i]) if categories is not None else 0
label_data += names[cat] + '\n'
return label_data
@torch.no_grad()
def run(
weights=ROOT / 'yolov5/yolov5s.pt', # model.pt path(s)
source=ROOT / 'yolov5/data/images', # file/dir/URL/glob, 0 for webcam
data=ROOT / 'yolov5/models/yolov5s.yaml', # customDataset.yaml path
imgsz=(640, 640), # inference size (height, width)
conf_thres=0.25, # confidence threshold
iou_thres=0.45, # NMS IOU threshold
max_det=1000, # maximum detections per image
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
nosave=False, # do not save images/videos
classes=None, # filter by class: --class 0, or --class 0 2 3
agnostic_nms=False, # class-agnostic NMS
augment=False, # augmented inference
visualize=False, # visualize features
project=ROOT / 'inference_extract_sum_txt', # save results to project/name
name='exp', # save results to project/name
exist_ok=False, # existing project/name ok, do not increment
line_thickness=3, # bounding box thickness (pixels)
dnn=False, # use OpenCV DNN for ONNX inference
sort_max_age=5,
sort_min_hits=2,
sort_iou_thresh=0.2
):
source = str(source)
is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
is_url = source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
webcam = source.isnumeric() or source.endswith(
'.txt') or (is_url and not is_file)
if is_url and is_file:
source = check_file(source) # download
# Directory and CUDA settings for YOLOv5
device = select_device(device)
save_dir = increment_path(Path(project) / name,
exist_ok=exist_ok) # increment run
if os.path.exists(save_dir):
shutil.rmtree(save_dir) # delete output folder
os.makedirs(save_dir) # make new output folder
half = device.type != 'cpu' # half precision only supported on CUDA
# Load YOLOv5 model
model = DetectMultiBackend(
weights, device=device, dnn=dnn, data=data, fp16=half)
stride, names, pt = model.stride, model.names, model.pt
imgsz = check_img_size(imgsz, s=stride) # check image size
# Set Dataloader
if webcam:
cudnn.benchmark = True # set True to speed up constant image size inference
dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt)
bs = len(dataset) # batch_size
else:
dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt)
bs = 1 # batch_size
# Run inference
model.warmup(imgsz=(1 if pt else bs, 3, *imgsz)) # warmup
seen, windows, dt = 0, [], [0.0, 0.0, 0.0]
for frame_idx, (path, im, im0s, vid_cap, s) in enumerate(dataset):
split_s = s.split()
filename = split_s[-1]
filename = ((filename.split('/'))[-1]).split('.')
filename = filename[0].split('_')
filename_parts = filename[0]
t1 = time_sync()
im = torch.from_numpy(im).to(device)
im = im.half() if model.fp16 else im.float() # uint8 to fp16/32
im /= 255 # 0 - 255 to 0.0 - 1.0
if len(im.shape) == 3:
im = im[None] # expand for batch dim
# Inference
visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False
pred = model(im, augment=augment, visualize=visualize)
# Apply NMS
pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)
# Process predictions
for i, det in enumerate(pred): # per image
seen += 1
if webcam: # batch_size >= 1
p, im0, frame = path[i], im0s[i].copy(), dataset.count
s += f'{i}: '
else:
p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
p = Path(p) # to Path
txt_path = str(save_dir / filename_parts) + '.txt' # im.txt
s += '%gx%g ' % im.shape[2:] # print string
# Rescale boxes from img_size (temporarily downscaled size) to im0 (native) size
det[:, :4] = scale_coords(
im.shape[2:], det[:, :4], im0.shape).round()
for c in det[:, -1].unique(): # for each unique object category
n = (det[:, -1] == c).sum() # number of detections per class
s += f' - {n} {names[int(c)]}'
dets_to_sort = np.empty((0, 6))
# Pass detections to SORT
# NOTE: We send in detected object class too
for x1, y1, x2, y2, conf, detclass in det.cpu().detach().numpy():
dets_to_sort = np.vstack((dets_to_sort, np.array([x1, y1, x2, y2, conf, detclass])))
# Save detect Data
if len(dets_to_sort) != 0:
label_datas = scene_boxes_data_txt(bbox=dets_to_sort[:, :4], categories=dets_to_sort[:, 5], names=names)
with open(txt_path, 'a') as f:
f.write(f'{label_datas}')
# Progress
print(f'{s} Done.')
# Time taken per frame
total_duration = time_sync() - t1
print('\tTime taken per frame: {:.4f}'.format(total_duration))
def parse_opt():
parser = argparse.ArgumentParser()
# YOLOv5 params
parser.add_argument('--weights', nargs='+', type=str, default='best_1107.pt', help='model path(s)')
parser.add_argument('--source', type=str, default='yolov5/data/images', help='file/dir/URL/glob, 0 for webcam')
parser.add_argument('--data', type=str, default='yolov5/customDataset/gachon_road.yaml',
help='(optional) customDataset.yaml path')
parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
parser.add_argument('--conf-thres', type=float, default=0.3, help='confidence threshold')
parser.add_argument('--iou-thres', type=float, default=0.4, help='NMS IoU threshold')
parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
parser.add_argument('--augment', action='store_true', help='augmented inference')
parser.add_argument('--visualize', action='store_true', help='visualize features')
parser.add_argument('--project', default=ROOT /
'inference_extract_sum_txt', help='save results to project/name')
parser.add_argument('--name', default='exp', help='save results to project/name')
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
# SORT params
parser.add_argument('--sort-max-age', type=int, default=5,
help='keep track of object even if object is occluded or not detected in n frames')
parser.add_argument('--sort-min-hits', type=int, default=2,
help='start tracking only after n number of objects detected')
parser.add_argument('--sort-iou-thresh', type=float, default=0.1,
help='intersection-over-union threshold between two frames for association')
opt = parser.parse_args()
opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand
print_args(vars(opt))
return opt
def main(opt):
check_requirements(exclude=('tensorboard', 'thop'))
run(**vars(opt))
if __name__ == "__main__":
opt = parse_opt()
main(opt)