-
Notifications
You must be signed in to change notification settings - Fork 95
/
demo.py
284 lines (246 loc) · 11.8 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
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
# ------------------------------------------------------------------------
# Copyright (c) 2021 megvii-model. All Rights Reserved.
# ------------------------------------------------------------------------
# Modified from Deformable DETR (https://github.com/fundamentalvision/Deformable-DETR)
# Copyright (c) 2020 SenseTime. All Rights Reserved.
# ------------------------------------------------------------------------
# Modified from DETR (https://github.com/facebookresearch/detr)
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
# ------------------------------------------------------------------------
from __future__ import print_function
import glob
import math
import os
import os.path as osp
import random
import time
from collections import OrderedDict
import torchvision.transforms.functional as F
import cv2
import numpy as np
import torch
import argparse
import torchvision.transforms.functional as F
from tqdm import tqdm
from pathlib import Path
from PIL import Image, ImageDraw
from models import build_model
from util.tool import load_model
from main import get_args_parser
from torch.nn.functional import interpolate
from typing import List
import shutil
from models.structures import Instances
np.random.seed(2020)
COLORS_10 = [(144, 238, 144), (178, 34, 34), (221, 160, 221), (0, 255, 0), (0, 128, 0), (210, 105, 30), (220, 20, 60),
(192, 192, 192), (255, 228, 196), (50, 205, 50), (139, 0, 139), (100, 149, 237), (138, 43, 226),
(238, 130, 238),
(255, 0, 255), (0, 100, 0), (127, 255, 0), (255, 0, 255), (0, 0, 205), (255, 140, 0), (255, 239, 213),
(199, 21, 133), (124, 252, 0), (147, 112, 219), (106, 90, 205), (176, 196, 222), (65, 105, 225),
(173, 255, 47),
(255, 20, 147), (219, 112, 147), (186, 85, 211), (199, 21, 133), (148, 0, 211), (255, 99, 71),
(144, 238, 144),
(255, 255, 0), (230, 230, 250), (0, 0, 255), (128, 128, 0), (189, 183, 107), (255, 255, 224),
(128, 128, 128),
(105, 105, 105), (64, 224, 208), (205, 133, 63), (0, 128, 128), (72, 209, 204), (139, 69, 19),
(255, 245, 238),
(250, 240, 230), (152, 251, 152), (0, 255, 255), (135, 206, 235), (0, 191, 255), (176, 224, 230),
(0, 250, 154),
(245, 255, 250), (240, 230, 140), (245, 222, 179), (0, 139, 139), (143, 188, 143), (255, 0, 0),
(240, 128, 128),
(102, 205, 170), (60, 179, 113), (46, 139, 87), (165, 42, 42), (178, 34, 34), (175, 238, 238),
(255, 248, 220),
(218, 165, 32), (255, 250, 240), (253, 245, 230), (244, 164, 96), (210, 105, 30)]
def plot_one_box(x, img, color=None, label=None, score=None, line_thickness=None):
# Plots one bounding box on image img
# tl = line_thickness or round(
# 0.002 * max(img.shape[0:2])) + 1 # line thickness
tl = 2
color = color or [random.randint(0, 255) for _ in range(3)]
c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
cv2.rectangle(img, c1, c2, color, thickness=tl)
if label:
tf = max(tl - 1, 1) # font thickness
t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
cv2.rectangle(img, c1, c2, color, -1) # filled
cv2.putText(img,
label, (c1[0], c1[1] - 2),
0,
tl / 3, [225, 255, 255],
thickness=tf,
lineType=cv2.LINE_AA)
if score is not None:
cv2.putText(img, score, (c1[0], c1[1] + 30), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
return img
def draw_bboxes(ori_img, bbox, identities=None, offset=(0, 0), cvt_color=False):
if cvt_color:
ori_img = cv2.cvtColor(np.asarray(ori_img), cv2.COLOR_RGB2BGR)
img = ori_img
for i, box in enumerate(bbox):
x1, y1, x2, y2 = [int(i) for i in box[:4]]
x1 += offset[0]
x2 += offset[0]
y1 += offset[1]
y2 += offset[1]
if len(box) > 4:
score = '{:.2f}'.format(box[4])
else:
score = None
# box text and bar
id = int(identities[i]) if identities is not None else 0
color = COLORS_10[id % len(COLORS_10)]
label = '{:d}'.format(id)
# t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 2 , 2)[0]
img = plot_one_box([x1, y1, x2, y2], img, color, label, score=score)
return img
def draw_points(img: np.ndarray, points: np.ndarray, color=(255, 255, 255)) -> np.ndarray:
assert len(points.shape) == 2 and points.shape[1] == 2, 'invalid points shape: {}'.format(points.shape)
for i, (x, y) in enumerate(points):
if i >= 300:
color = (0, 255, 0)
cv2.circle(img, (int(x), int(y)), 2, color=color, thickness=2)
return img
class LoadVideo: # for inference
def __init__(self, path, img_size=(1536, 800)):
if not os.path.isfile(path):
raise FileExistsError
self.cap = cv2.VideoCapture(path)
self.frame_rate = int(round(self.cap.get(cv2.CAP_PROP_FPS)))
self.seq_w = int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH))
self.seq_h = int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
self.vn = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
self.width = img_size[0]
self.height = img_size[1]
self.count = 0
self.mean = [0.485, 0.456, 0.406]
self.std = [0.229, 0.224, 0.225]
print('Lenth of the video: {:d} frames'.format(self.vn))
def __iter__(self):
self.count = -1
return self
def __next__(self):
self.count += 1
if self.count == len(self):
raise StopIteration
# Read image
res, img = self.cap.read() # BGR
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # RGB
assert img is not None, 'Failed to load frame {:d}'.format(self.count)
cur_img, ori_img = self.init_img(img)
return self.count, cur_img, ori_img
def init_img(self, img):
ori_img = img.copy()
self.seq_h, self.seq_w = img.shape[:2]
scale = self.height / min(self.seq_h, self.seq_w)
if max(self.seq_h, self.seq_w) * scale > self.width:
scale = self.width / max(self.seq_h, self.seq_w)
target_h = int(self.seq_h * scale)
target_w = int(self.seq_w * scale)
img = cv2.resize(img, (target_w, target_h))
img = F.normalize(F.to_tensor(img), self.mean, self.std)
img = img.unsqueeze(0)
return img, ori_img
def __len__(self):
return self.vn # number of files
class MOTR(object):
def update(self, dt_instances: Instances):
ret = []
for i in range(len(dt_instances)):
label = dt_instances.labels[i]
if label == 0:
id = dt_instances.obj_idxes[i]
box_with_score = np.concatenate([dt_instances.boxes[i], dt_instances.scores[i:i+1]], axis=-1)
ret.append(np.concatenate((box_with_score, [id + 1])).reshape(1, -1)) # +1 as MOT benchmark requires positive
if len(ret) > 0:
return np.concatenate(ret)
return np.empty((0, 6))
class Detector(object):
def __init__(self, args):
self.args = args
# build model and load weights
self.model, _, _ = build_model(args)
checkpoint = torch.load(args.resume, map_location='cpu')
self.model = load_model(self.model, args.resume)
self.model = self.model.cuda()
self.model.eval()
# mkidr save_dir
vid_name, prefix = args.input_video.split('/')[-1].split('.')
self.save_root = os.path.join(args.output_dir, 'results', vid_name)
Path(self.save_root).mkdir(parents=True, exist_ok=True)
self.save_img_root = os.path.join(self.save_root, 'imgs')
Path(self.save_img_root).mkdir(parents=True, exist_ok=True)
self.txt_root = os.path.join(self.save_root, f'{vid_name}.txt')
self.vid_root = os.path.join(self.save_root, args.input_video.split('/')[-1])
# build dataloader and tracker
self.dataloader = LoadVideo(args.input_video)
self.tr_tracker = MOTR()
@staticmethod
def filter_dt_by_score(dt_instances: Instances, prob_threshold: float) -> Instances:
keep = dt_instances.scores > prob_threshold
return dt_instances[keep]
@staticmethod
def filter_dt_by_area(dt_instances: Instances, area_threshold: float) -> Instances:
wh = dt_instances.boxes[:, 2:4] - dt_instances.boxes[:, 0:2]
areas = wh[:, 0] * wh[:, 1]
keep = areas > area_threshold
return dt_instances[keep]
@staticmethod
def write_results(txt_path, frame_id, bbox_xyxy, identities):
save_format = '{frame},{id},{x1},{y1},{w},{h},1,-1,-1,-1\n'
with open(txt_path, 'a') as f:
for xyxy, track_id in zip(bbox_xyxy, identities):
if track_id < 0 or track_id is None:
continue
x1, y1, x2, y2 = xyxy
w, h = x2 - x1, y2 - y1
line = save_format.format(frame=int(frame_id), id=int(track_id), x1=x1, y1=y1, w=w, h=h)
f.write(line)
@staticmethod
def visualize_img_with_bbox(img_path, img, dt_instances: Instances, ref_pts=None, gt_boxes=None):
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
if dt_instances.has('scores'):
img_show = draw_bboxes(img, np.concatenate([dt_instances.boxes, dt_instances.scores.reshape(-1, 1)], axis=-1), dt_instances.obj_idxes)
else:
img_show = draw_bboxes(img, dt_instances.boxes, dt_instances.obj_idxes)
if ref_pts is not None:
img_show = draw_points(img_show, ref_pts)
if gt_boxes is not None:
img_show = draw_bboxes(img_show, gt_boxes, identities=np.ones((len(gt_boxes), )) * -1)
cv2.imwrite(img_path, img_show)
return img_show
def run(self, prob_threshold=0.7, area_threshold=100, vis=True, dump=True):
# save as video
fps = self.dataloader.frame_rate
videowriter = cv2.VideoWriter(self.vid_root, cv2.VideoWriter_fourcc('M','J','P','G'), fps, (self.dataloader.seq_w, self.dataloader.seq_h))
track_instances = None
fid = 0
for _, cur_img, ori_img in tqdm(self.dataloader):
if track_instances is not None:
track_instances.remove('boxes')
track_instances.remove('labels')
res = self.model.inference_single_image(cur_img.cuda().float(), (self.dataloader.seq_h, self.dataloader.seq_w), track_instances)
track_instances = res['track_instances']
dt_instances = track_instances.to(torch.device('cpu'))
# filter det instances by score.
dt_instances = self.filter_dt_by_score(dt_instances, prob_threshold)
dt_instances = self.filter_dt_by_area(dt_instances, area_threshold)
if vis:
vis_img_path = os.path.join(self.save_img_root, '{:06d}.jpg'.format(fid))
vis_img = self.visualize_img_with_bbox(vis_img_path, ori_img, dt_instances)
videowriter.write(vis_img)
if dump:
tracker_outputs = self.tr_tracker.update(dt_instances)
self.write_results(txt_path=self.txt_root,
frame_id=(fid+1),
bbox_xyxy=tracker_outputs[:, :4],
identities=tracker_outputs[:, 5])
fid += 1
videowriter.release()
if __name__ == '__main__':
parser = argparse.ArgumentParser('DETR training and evaluation script', parents=[get_args_parser()])
args = parser.parse_args()
if args.output_dir:
Path(args.output_dir).mkdir(parents=True, exist_ok=True)
detector = Detector(args)
detector.run()