forked from iwanggp/yolov5_onnxruntime_deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyolov5_ort.py
187 lines (169 loc) · 6.48 KB
/
yolov5_ort.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
import cv2
import onnxruntime
import numpy as np
def letterbox(img, new_shape=(640, 640), auto=False, scaleFill=False, scaleUp=True):
"""
python的信封图片缩放
:param img: 原图
:param new_shape: 缩放后的图片
:param color: 填充的颜色
:param auto: 是否为自动
:param scaleFill: 填充
:param scaleUp: 向上填充
:return:
"""
shape = img.shape[:2] # current shape[height,width]
if isinstance(new_shape, int):
new_shape = (new_shape, new_shape)
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
if not scaleUp:
r = min(r, 1.0) # 确保不超过1
ration = r, r # width,height 缩放比例
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]
if auto:
dw, dh = np.mod(dw, 64), np.mod(dh, 64)
elif scaleFill:
dw, dh = 0.0, 0.0
new_unpad = (new_shape[1], new_shape[0])
ration = new_shape[1] / shape[1], new_shape[0] / shape[0]
# 均分处理
dw /= 2
dh /= 2
if shape[::-1] != new_unpad:
img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=(114, 114, 114)) # 添加边界
return img, ration, (dw, dh)
def clip_coords(boxes, img_shape):
"""
图片的边界处理
:param boxes: 检测框
:param img_shape: 图片的尺寸
:return:
"""
boxes[:, 0].clip(0, img_shape[1]) # x1
boxes[:, 1].clip(0, img_shape[0]) # y1
boxes[:, 2].clip(0, img_shape[1]) # x2
boxes[:, 3].clip(0, img_shape[0]) # x2
def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
"""
坐标还原
:param img1_shape: 旧图像的尺寸
:param coords: 坐标
:param img0_shape:新图像的尺寸
:param ratio_pad: 填充率
:return:
"""
if ratio_pad is None: # 从img0_shape中计算
gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain=old/new
pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2
else:
gain = ratio_pad[0][0]
pad = ratio_pad[1]
coords[:, [0, 2]] -= pad[0] # x padding
coords[:, [1, 3]] -= pad[1] # y padding
coords[:, :4] /= gain
clip_coords(coords, img0_shape)
return coords
class Detector():
"""
检测类
"""
def __init__(self):
super(Detector, self).__init__()
self.img_size = 640
self.threshold = 0.25
self.iou_thres = 0.45
self.stride = 1
self.weights = 'yolov5s.onnx'
self.init_model()
self.names = ["person"]
def init_model(self):
"""
模型初始化这一步比较固定写法
:return:
"""
sess = onnxruntime.InferenceSession(self.weights) # 加载模型权重
self.input_name = sess.get_inputs()[0].name # 获得输入节点
output_names = []
for i in range(len(sess.get_outputs())):
print("output node:", sess.get_outputs()[i].name)
output_names.append(sess.get_outputs()[i].name) # 所有的输出节点
print(output_names)
self.output_name = sess.get_outputs()[0].name # 获得输出节点的名称
print(f"input name {self.input_name}-----output_name{self.output_name}")
input_shape = sess.get_inputs()[0].shape # 输入节点形状
print("input_shape:", input_shape)
self.m = sess
def preprocess(self, img):
"""
图片预处理过程
:param img:
:return:
"""
img0 = img.copy()
img = letterbox(img, new_shape=self.img_size)[0] # 图片预处理
img = img[:, :, ::-1].transpose(2, 0, 1)
img = np.ascontiguousarray(img).astype(np.float32)
img /= 255.0
img = np.expand_dims(img, axis=0)
assert len(img.shape) == 4
return img0, img
def detect(self, im):
"""
:param img:
:return:
"""
img0, img = self.preprocess(im)
pred = self.m.run(None, {self.input_name: img})[0] # 执行推理
pred = pred.astype(np.float32)
pred = np.squeeze(pred, axis=0)
boxes = []
classIds = []
confidences = []
for detection in pred:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID] * detection[4] # 置信度为类别的概率和目标框概率值得乘积
if confidence > self.threshold and classID == 0:
box = detection[0:4]
(centerX, centerY, width, height) = box.astype("int")
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
boxes.append([x, y, int(width), int(height)])
classIds.append(classID)
confidences.append(float(confidence))
idxs = cv2.dnn.NMSBoxes(boxes, confidences, self.threshold, self.iou_thres) # 执行nms算法
pred_boxes = []
pred_confes = []
pred_classes = []
if len(idxs) > 0:
for i in idxs.flatten():
confidence = confidences[i]
if confidence >= self.threshold:
pred_boxes.append(boxes[i])
pred_confes.append(confidence)
pred_classes.append(classIds[i])
return im, pred_boxes, pred_confes, pred_classes
def main():
det = Detector()
image = cv2.imread('img.png')
shape = (det.img_size, det.img_size)
img, pred_boxes, pred_confes, pred_classes = det.detect(image)
if len(pred_boxes) > 0:
for i, _ in enumerate(pred_boxes):
box = pred_boxes[i]
left, top, width, height = box[0], box[1], box[2], box[3]
box = (left, top, left + width, top + height)
box = np.squeeze(
scale_coords(shape, np.expand_dims(box, axis=0).astype("float"), img.shape[:2]).round(), axis=0).astype(
"int") # 进行坐标还原
x0, y0, x1, y1 = box[0], box[1], box[2], box[3]
# 执行画图函数
cv2.rectangle(image, (x0, y0), (x1, y1), (0, 0, 255), thickness=2)
cv2.rectangle(image, (x0, y0), (x1, y1), (0, 0, 255), thickness=2)
cv2.imwrite("result.jpg", image)
if __name__ == '__main__':
main()