forked from timwheler/Observatory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunPipline
309 lines (258 loc) · 12.3 KB
/
runPipline
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import os
import cv2
import time
import math
import torch
import numpy as np
import os.path as osp
from tqdm import tqdm
from pathlib import Path
from PIL import ImageFont
from collections import deque
import EasyPySpin
from yolov6.utils.events import LOGpyGER, load_yaml
from yolov6.layers.common import DetectBackend
from yolov6.data.data_augment import letterbox
from yolov6.utils.nms import non_max_suppression
class LoadData:
def __init__(self, useEasyPySpin=False):
if useEasyPySpin:
self.cap = EasyPySpin.VideoCapture(0)
else:
self.cap = cv2.VideoCapture(0)
def __iter__(self):
return self
def __next__(self):
try:
ret_val, img = self.cap.read()
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
return img
except KeyboardInterrupt:
raise StopIteration
def __del__(self):
self.cap.release()
class Inferer:
def __init__(self, source, half):
self.__dict__.update(locals())
# Config
weights = "/ssd/Models/YOLOv6/weights/good/yolov6s_V3.pt"
yaml = "/ssd/Models/YOLOv6/data/vernierV3.yaml"
device = 'cuda:0' # device number or 'cpu'
self.img_size = [640, 640] # default 640x640?
self.half = False
# Init model
self.device = torch.device(device)
self.model = DetectBackend(weights, device=self.device)
self.stride = self.model.stride
self.class_names = load_yaml(yaml)['names']
self.img_size = self.check_img_size(self.img_size, s=self.stride) # check image size
# Switch model to deploy status
self.model_switch(self.model.model, self.img_size)
# Half precision
if self.half & (self.device.type != 'cpu'):
self.model.model.half()
else:
self.model.model.float()
self.half = False
if self.device.type != 'cpu':
self.model(torch.zeros(1, 3, *self.img_size).to(self.device).type_as(next(self.model.model.parameters()))) # warmup
self.files = LoadData()
def model_switch(self, model, img_size):
''' Model switch to deploy status '''
from yolov6.layers.common import RepVGGBlock
for layer in model.modules():
if isinstance(layer, RepVGGBlock):
layer.switch_to_deploy()
elif isinstance(layer, torch.nn.Upsample) and not hasattr(layer, 'recompute_scale_factor'):
layer.recompute_scale_factor = None # torch 1.11.0 compatibility
def infer(self, conf_thres, iou_thres, classes, agnostic_nms,
max_det, save_dir, save_txt, save_img, hide_labels, hide_conf, view_img=True):
''' Model Inference and results visualization '''
vid_path, vid_writer, windows = None, None, []
fps_calculator = CalcFPS()
for img_src, img_path, vid_cap in self.files:
img, img_src = self.process_image(img_src, self.img_size, self.stride, self.half)
img = img.to(self.device)
if len(img.shape) == 3:
img = img[None]
# expand for batch dim
t1 = time.time()
pred_results = self.model(img)
det = non_max_suppression(pred_results, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det)[0]
t2 = time.time()
gn = torch.tensor(img_src.shape)[[1, 0, 1, 0]] # normalization gain whwh
img_ori = img_src.copy()
# check image and font
assert img_ori.data.contiguous, 'Image needs to be contiguous. Please apply to input images with np.ascontiguousarray(im).'
self.font_check()
if len(det):
det[:, :4] = self.rescale(img.shape[2:], det[:, :4], img_src.shape).round()
for *xyxy, conf, cls in reversed(det):
if save_txt: # Write to file
xywh = (self.box_convert(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
line = (cls, *xywh, conf)
with open(txt_path + '.txt', 'a') as f:
f.write(('%g ' * len(line)).rstrip() % line + '\n')
if save_img:
class_num = int(cls) # integer class
label = None if hide_labels else (self.class_names[class_num] if hide_conf else f'{self.class_names[class_num]} {conf:.2f}')
self.plot_box_and_label(img_ori, max(round(sum(img_ori.shape) / 2 * 0.003), 2), xyxy, label, color=self.generate_colors(class_num, True))
img_src = np.asarray(img_ori)
# FPS counter
fps_calculator.update(1.0 / (t2 - t1))
avg_fps = fps_calculator.accumulate()
if self.files.type == 'video':
self.draw_text(
img_src,
f"FPS: {avg_fps:0.1f}",
pos=(20, 20),
font_scale=1.0,
text_color=(204, 85, 17),
text_color_bg=(255, 255, 255),
font_thickness=2,
)
if view_img:
if img_path not in windows:
windows.append(img_path)
cv2.namedWindow(str(img_path), cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux)
cv2.resizeWindow(str(img_path), img_src.shape[1], img_src.shape[0])
cv2.imshow(str(img_path), img_src)
cv2.waitKey(1) # 1 millisecond
# Save results (image with detections)
if save_img:
if self.files.type == 'image':
cv2.imwrite(save_path, img_src)
else: # 'video' or 'stream'
if vid_path != save_path: # new video
vid_path = save_path
if isinstance(vid_writer, cv2.VideoWriter):
vid_writer.release() # release previous video writer
if vid_cap: # video
fps = vid_cap.get(cv2.CAP_PROP_FPS)
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
else: # stream
fps, w, h = 30, img_ori.shape[1], img_ori.shape[0]
save_path = str(Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
vid_writer.write(img_src)
@staticmethod
def process_image(img_src, img_size, stride, half):
'''Process image before image inference.'''
image = letterbox(img_src, img_size, stride=stride)[0]
# Convert
# Input RGB image = image.transpose((2, 0, 1))[::-1] # HWC to CHW, BGR to RGB
image = torch.from_numpy(np.ascontiguousarray(image))
image = image.half() if half else image.float() # uint8 to fp16/32
image /= 255 # 0 - 255 to 0.0 - 1.0
return image, img_src
@staticmethod
def rescale(ori_shape, boxes, target_shape):
'''Rescale the output to the original image shape'''
ratio = min(ori_shape[0] / target_shape[0], ori_shape[1] / target_shape[1])
padding = (ori_shape[1] - target_shape[1] * ratio) / 2, (ori_shape[0] - target_shape[0] * ratio) / 2
boxes[:, [0, 2]] -= padding[0]
boxes[:, [1, 3]] -= padding[1]
boxes[:, :4] /= ratio
boxes[:, 0].clamp_(0, target_shape[1]) # x1
boxes[:, 1].clamp_(0, target_shape[0]) # y1
boxes[:, 2].clamp_(0, target_shape[1]) # x2
boxes[:, 3].clamp_(0, target_shape[0]) # y2
return boxes
def check_img_size(self, img_size, s=32, floor=0):
"""Make sure image size is a multiple of stride s in each dimension, and return a new shape list of image."""
if isinstance(img_size, int): # integer i.e. img_size=640
new_size = max(self.make_divisible(img_size, int(s)), floor)
elif isinstance(img_size, list): # list i.e. img_size=[640, 480]
new_size = [max(self.make_divisible(x, int(s)), floor) for x in img_size]
else:
raise Exception(f"Unsupported type of img_size: {type(img_size)}")
if new_size != img_size:
print(f'WARNING: --img-size {img_size} must be multiple of max stride {s}, updating to {new_size}')
return new_size if isinstance(img_size,list) else [new_size]*2
def make_divisible(self, x, divisor):
# Upward revision the value x to make it evenly divisible by the divisor.
return math.ceil(x / divisor) * divisor
@staticmethod
def draw_text(
img,
text,
font=cv2.FONT_HERSHEY_SIMPLEX,
pos=(0, 0),
font_scale=1,
font_thickness=2,
text_color=(0, 255, 0),
text_color_bg=(0, 0, 0),
):
offset = (5, 5)
x, y = pos
text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness)
text_w, text_h = text_size
rec_start = tuple(x - y for x, y in zip(pos, offset))
rec_end = tuple(x + y for x, y in zip((x + text_w, y + text_h), offset))
cv2.rectangle(img, rec_start, rec_end, text_color_bg, -1)
cv2.putText(
img,
text,
(x, int(y + text_h + font_scale - 1)),
font,
font_scale,
text_color,
font_thickness,
cv2.LINE_AA,
)
return text_size
@staticmethod
def plot_box_and_label(image, lw, box, label='', color=(128, 128, 128), txt_color=(255, 255, 255), font=cv2.FONT_HERSHEY_COMPLEX):
# Add one xyxy box to image with label
p1, p2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
cv2.rectangle(image, p1, p2, color, thickness=lw, lineType=cv2.LINE_AA)
if label:
tf = max(lw - 1, 1) # font thickness
w, h = cv2.getTextSize(label, 0, fontScale=lw / 3, thickness=tf)[0] # text width, height
outside = p1[1] - h - 3 >= 0 # label fits outside box
p2 = p1[0] + w, p1[1] - h - 3 if outside else p1[1] + h + 3
cv2.rectangle(image, p1, p2, color, -1, cv2.LINE_AA) # filled
cv2.putText(image, label, (p1[0], p1[1] - 2 if outside else p1[1] + h + 2), font, lw / 3, txt_color,
thickness=tf, lineType=cv2.LINE_AA)
@staticmethod
def font_check(font='./yolov6/utils/Arial.ttf', size=10):
# Return a PIL TrueType Font, downloading to CONFIG_DIR if necessary
assert osp.exists(font), f'font path not exists: {font}'
try:
return ImageFont.truetype(str(font) if font.exists() else font.name, size)
except Exception as e: # download if missing
return ImageFont.truetype(str(font), size)
@staticmethod
def box_convert(x):
# Convert boxes with shape [n, 4] from [x1, y1, x2, y2] to [x, y, w, h] where x1y1=top-left, x2y2=bottom-right
y = x.clone() if isinstance(x, torch.Tensor) else np.copy(x)
y[:, 0] = (x[:, 0] + x[:, 2]) / 2 # x center
y[:, 1] = (x[:, 1] + x[:, 3]) / 2 # y center
y[:, 2] = x[:, 2] - x[:, 0] # width
y[:, 3] = x[:, 3] - x[:, 1] # height
return y
@staticmethod
def generate_colors(i, bgr=False):
hex = ('FF3838', 'FF9D97', 'FF701F', 'FFB21D', 'CFD231', '48F90A', '92CC17', '3DDB86', '1A9334', '00D4BB',
'2C99A8', '00C2FF', '344593', '6473FF', '0018EC', '8438FF', '520085', 'CB38FF', 'FF95C8', 'FF37C7')
palette = []
for iter in hex:
h = '#' + iter
palette.append(tuple(int(h[1 + i:1 + i + 2], 16) for i in (0, 2, 4)))
num = len(palette)
color = palette[int(i) % num]
return (color[2], color[1], color[0]) if bgr else color
class CalcFPS:
def __init__(self, nsamples: int = 50):
self.framerate = deque(maxlen=nsamples)
def update(self, duration: float):
self.framerate.append(duration)
def accumulate(self):
if len(self.framerate) > 1:
return np.average(self.framerate)
else:
return 0.0