-
Notifications
You must be signed in to change notification settings - Fork 13
/
detect.py
144 lines (116 loc) · 4.39 KB
/
detect.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
"""Run Inference using TensorRT.
Args
--weights: onnx weights path or trt engine path; default yolov5s will download pretrained weights.
--inputs: path to input video or image file. default people.mp4 will download demo video.
--output: path to output video or image file. default out.mp4 (out.jpg if image file given in input )
"""
import argparse
import os
import time
import numpy as np
import cv2
from cvu.detector.yolov5 import Yolov5 as Yolov5Trt
from vidsz.opencv import Reader, Writer
from cvu.utils.google_utils import gdrive_download
def detect_video(weight,
input_video,
output_video=None,
classes="coco",
auto_install=True,
dtype="fp16"):
# load model
model = Yolov5Trt(classes=classes,
backend="tensorrt",
weight=weight,
auto_install=auto_install,
dtype=dtype)
# setup video reader and writer
reader = Reader(input_video)
writer = Writer(reader,
name=output_video) if output_video is not None else None
# warmup
warmup = np.random.randint(0, 255, reader.read().shape).astype("float")
for i in range(100):
model(warmup)
inference_time = 0
for frame in reader:
# inference
start = time.time()
preds = model(frame)
inference_time += time.time() - start
# draw on frame
if writer is not None:
preds.draw(frame)
writer.write(frame)
print("\nModel Inference FPS: ",
round(reader.frame_count / inference_time, 3))
print("Output Video Saved at: ", writer.name)
writer.release()
reader.release()
def detect_image(weight,
image_path,
output_image,
classes="coco",
auto_install=True,
dtype="fp16"):
# load model
model = Yolov5Trt(classes=classes,
backend="tensorrt",
weight=weight,
auto_install=auto_install,
dtype=dtype)
# read image
image = cv2.imread(image_path)
# inference
preds = model(image)
print(preds)
# draw image
preds.draw(image)
# write image
print(output_image)
cv2.imwrite(output_image, image)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--weights',
type=str,
default='yolov5s',
help='onnx weights path or trt engine path')
parser.add_argument('--input',
type=str,
default='people.mp4',
help='path to input video or image file')
parser.add_argument('--output',
type=str,
default='out.mp4',
help='name of output video or image file')
parser.add_argument('--classes',
nargs='+',
default=None,
type=str,
help=(('custom classes or filter coco classes ' +
'classes: --class car bus person')))
parser.add_argument('--no-auto-install',
action='store_true',
help="Turn off auto install feature")
parser.add_argument('--dtype',
type=str,
default='fp16',
choices=['fp16', 'fp32'],
help="set engine precision")
opt = parser.parse_args()
if opt.classes is None:
opt.classes = 'coco'
# image file
input_ext = os.path.splitext(opt.input)[-1]
output_ext = os.path.splitext(opt.output)[-1]
if input_ext in (".jpg", ".jpeg", ".png"):
if output_ext not in ((".jpg", ".jpeg", ".png")):
opt.output = opt.output.replace(output_ext, input_ext)
detect_image(opt.weights, opt.input, opt.output, opt.classes,
not opt.no_auto_install, opt.dtype.lower())
# video file
else:
if not os.path.exists(opt.input) and opt.input == 'people.mp4':
gdrive_download("1rioaBCzP9S31DYVh-tHplQ3cgvgoBpNJ", "people.mp4")
detect_video(opt.weights, opt.input, opt.output, opt.classes,
not opt.no_auto_install, opt.dtype.lower())