-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathprocess_batch.py
167 lines (138 loc) · 4.93 KB
/
process_batch.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
#!/usr/bin/python
#
# Author: Noah van der Meer
# Description: YoloV5-TensorRT example: inference on a batch of images
#
#
# Copyright (c) 2021, Noah van der Meer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
#
# note: import cv2 _before_ yolov5tensorrt; Otherwise it may lead to
# issues see https://github.com/opencv/opencv/issues/14884
import cv2
import yolov5tensorrt
import argparse
import time
import os
def main(args):
#
# Create the YoloV5 Detector object
#
detector = yolov5tensorrt.Detector()
#
# Initialize the YoloV5 Detector.
#
r = detector.init()
if r != yolov5tensorrt.Result.SUCCESS:
print("init() failed:", yolov5tensorrt.result_to_string(r))
return 1
#
# Load the engine from file.
#
r = detector.loadEngine(args.engine)
if r != yolov5tensorrt.Result.SUCCESS:
print("loadEngine() failed:", yolov5tensorrt.result_to_string(r))
return 1
#
# Load the Class names from file, and pass these on to the Detector
#
if args.classes is not None:
classes = yolov5tensorrt.Classes()
r = classes.loadFromFile(args.classes)
if r != yolov5tensorrt.Result.SUCCESS:
print("classes.loadFromFile() failed:",
yolov5tensorrt.result_to_string(r))
return 1
detector.setClasses(classes)
#
# List all files in the specified directory
#
filenames = os.listdir(args.inputs)
print("Found", len(filenames), "files in specified input directory")
images = []
for f in filenames:
image = cv2.imread(args.inputs + "/" + f)
if image is not None:
images.append(image)
else:
print("Could not load file ", f)
return 1
#
# The first one/two runs of the engine typically take significantly
# longer. To get an accurate timing for inference, first do two
# runs. These can of course also be performed on other representative
# images
#
detector.detect(image)
detector.detect(image)
ts = time.perf_counter()
#
# Detect objects in the images using the detectBatch(...) method.
#
r, detections = detector.detectBatch(images)
if r != yolov5tensorrt.Result.SUCCESS:
print("detectBatch() failed:", yolov5tensorrt.result_to_string(r))
return 1
# timing
duration = time.perf_counter() - ts
print("detectBatch() took:", duration*1000, "milliseconds")
#
# Visualize all of the detections & store to disk
#
magenta = (255, 51, 153) # BGR
visualization = []
for i in range(0, len(detections)):
img = images[i]
lst = detections[i]
for d in lst:
yolov5tensorrt.visualizeDetection(d, img, magenta, 1.0)
#
# Store the visualization to disk again.
#
outputName = args.outputs + "/" + filenames[i]
cv2.imwrite(outputName, img)
return 0
if __name__ == '__main__':
#
# Handle arguments
#
parser = argparse.ArgumentParser(add_help=True)
parser.add_argument('--engine',
required = True,
dest ='engine',
type = str,
help = '[mandatory] specify the engine file')
parser.add_argument('--inputs',
required = True,
dest ='inputs',
type = str,
help = '[mandatory] specify the input directory')
parser.add_argument('--outputs',
required = True,
dest ='outputs',
type = str,
help = '[mandatory] specify the output directory')
parser.add_argument('--classes',
dest ='classes',
type = str,
help = '[optional] specify list of class names')
args = parser.parse_args()
main(args)