当前位置:   article > 正文

YOLOV5 onnxruntime预测不用pytorch_onnxruntime替代pytorch

onnxruntime替代pytorch
  1. import numpy as np
  2. import cv2
  3. import onnxruntime
  4. import argparse
  5. def letterbox(img, new_shape=(416, 416), color=(114, 114, 114), auto=False, scaleFill=False, scaleup=True):
  6. shape = img.shape[:2] # current shape [height, width]
  7. if isinstance(new_shape, int):
  8. new_shape = (new_shape, new_shape)
  9. r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
  10. if not scaleup:
  11. r = min(r, 1.0)
  12. ratio = r, r # width, height ratios
  13. new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
  14. dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
  15. if auto: # minimum rectangle
  16. dw, dh = np.mod(dw, 64), np.mod(dh, 64) # wh padding
  17. elif scaleFill: # stretch
  18. dw, dh = 0.0, 0.0
  19. new_unpad = (new_shape[1], new_shape[0])
  20. ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
  21. dw /= 2 # divide padding into 2 sides
  22. dh /= 2
  23. if shape[::-1] != new_unpad: # resize
  24. img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
  25. top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
  26. left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
  27. img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
  28. return img, ratio, (dw, dh)
  29. def clip_coords(boxes, img_shape):
  30. # Clip bounding xyxy bounding boxes to image shape (height, width)
  31. boxes[:, 0].clip(0, img_shape[1]) # x1
  32. boxes[:, 1].clip(0, img_shape[0]) # y1
  33. boxes[:, 2].clip(0, img_shape[1]) # x2
  34. boxes[:, 3].clip(0, img_shape[0]) # y2
  35. def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):
  36. # Rescale coords (xyxy) from img1_shape to img0_shape
  37. if ratio_pad is None: # calculate from img0_shape
  38. gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1]) # gain = old / new
  39. pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2 # wh padding
  40. else:
  41. gain = ratio_pad[0][0]
  42. pad = ratio_pad[1]
  43. coords[:, [0, 2]] -= pad[0] # x padding
  44. coords[:, [1, 3]] -= pad[1] # y padding
  45. coords[:, :4] /= gain
  46. clip_coords(coords, img0_shape)
  47. return coords
  48. class Detector():
  49. def __init__(self, opt):
  50. super(Detector, self).__init__()
  51. self.img_size = opt.img_size
  52. self.threshold = opt.conf_thres
  53. self.iou_thres = opt.iou_thres
  54. self.stride = 1
  55. self.weights = opt.weights
  56. self.init_model()
  57. self.names = ["wu","yzm"]
  58. self.detected_labels = ["wu","yzm"]
  59. def init_model(self):
  60. sess = onnxruntime.InferenceSession(self.weights)
  61. self.input_name = sess.get_inputs()[0].name
  62. output_names = []
  63. for i in range(len(sess.get_outputs())):
  64. print('output shape:', sess.get_outputs()[i].name)
  65. output_names.append(sess.get_outputs()[i].name)
  66. self.output_name = sess.get_outputs()[0].name
  67. print('input name:%s, output name:%s' % (self.input_name, self.output_name))
  68. input_shape = sess.get_inputs()[0].shape
  69. print('input_shape:', input_shape)
  70. self.m = sess
  71. def preprocess(self, img):
  72. img0 = img.copy()
  73. img = letterbox(img, new_shape=self.img_size)[0]
  74. img = img[:, :, ::-1].transpose(2, 0, 1)
  75. img = np.ascontiguousarray(img).astype(np.float32)
  76. img /= 255.0 # 图像归一化
  77. img = np.expand_dims(img, axis=0)
  78. assert len(img.shape) == 4
  79. return img0, img
  80. def detect(self, im):
  81. im0, img = self.preprocess(im)
  82. W, H = img.shape[2:]
  83. pred = self.m.run(None, {self.input_name: img})[0]
  84. pred = pred.astype(np.float32)
  85. pred = np.squeeze(pred, axis=0)
  86. boxes = []
  87. classIds = []
  88. confidences = []
  89. for detection in pred:
  90. scores = detection[5:]
  91. classID = np.argmax(scores)
  92. confidence = scores[classID] * detection[4]
  93. if confidence > self.threshold:
  94. box = detection[0:4]
  95. (centerX, centerY, width, height) = box.astype("int")
  96. x = int(centerX - (width / 2))
  97. y = int(centerY - (height / 2))
  98. boxes.append([x, y, int(width), int(height)])
  99. classIds.append(classID)
  100. confidences.append(float(confidence))
  101. idxs = cv2.dnn.NMSBoxes(
  102. boxes, confidences, self.threshold, self.iou_thres)
  103. pred_boxes = []
  104. pred_confes = []
  105. pred_classes = []
  106. if len(idxs) > 0:
  107. for i in idxs.flatten():
  108. confidence = confidences[i]
  109. if confidence >= self.threshold:
  110. pred_boxes.append(boxes[i])
  111. pred_confes.append(confidence)
  112. pred_classes.append(classIds[i])
  113. return im, pred_boxes, pred_confes, pred_classes
  114. def main(opt):
  115. det = Detector(opt)
  116. image = cv2.imread(opt.source)
  117. shape = (det.img_size, det.img_size)
  118. im0, pred_boxes, pred_confes, pred_classes = det.detect(image)
  119. if len(pred_boxes) > 0:
  120. for i, _ in enumerate(pred_boxes):
  121. box = pred_boxes[i]
  122. left, top, width, height = box[0], box[1], box[2], box[3]
  123. box = (left, top, left + width, top + height)
  124. box = np.squeeze(
  125. scale_coords(shape, np.expand_dims(box, axis=0).astype("float"), im0.shape[:2]).round(),
  126. axis=0).astype("int")
  127. x0, y0, x1, y1 = box[0], box[1], box[2], box[3]
  128. cv2.rectangle(image, (x0, y0), (x1, y1), (0, 0, 255), thickness=2)
  129. cv2.putText(image, '{0}--{1:.2f}'.format(pred_classes[i], pred_confes[i]), (x0, y0 - 10),
  130. cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), thickness=1)
  131. cv2.imshow("detector", image)
  132. cv2.waitKey()
  133. cv2.destroyAllWindows()
  134. if __name__ == '__main__':
  135. parser = argparse.ArgumentParser()
  136. parser.add_argument('--weights', nargs='+', type=str, default='detect_model.onnx', help='onnx path(s)')
  137. parser.add_argument('--source', type=str, default='yzm.jpg', help='source') # file/folder, 0 for webcam
  138. parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
  139. parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
  140. parser.add_argument('--iou-thres', type=float, default=0.45, help='IOU threshold for NMS')
  141. parser.add_argument('--line-thickness', default=1, type=int, help='bounding box thickness (pixels)')
  142. parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
  143. parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
  144. opt = parser.parse_args()
  145. #print(opt)
  146. main(opt)

这个版本完全使用onnxruntime,这样可以减少环境的大小。 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号