yolov5_inference.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import cv2
  2. import numpy as np
  3. import onnxruntime as ort
  4. class YOLOV5Inference:
  5. def __init__(self, model_path, input_size=(640, 640), swap=(2, 0, 1)):
  6. """
  7. 初始化 YOLOv5 模型推理流程
  8. :param model_path: 模型 ONNX 路径
  9. :param input_size: 模型输入尺寸,默认 640x640
  10. :param swap: 图像轴变换顺序,默认为 (2,0,1) 即 HWC -> CHW
  11. """
  12. self.model_path = model_path
  13. self.input_size = input_size
  14. self.swap = swap
  15. # 初始化 ONNX 推理会话
  16. self.session = ort.InferenceSession(self.model_path)
  17. self.input_name = self.session.get_inputs()[0].name
  18. def input_processing(self, image_path):
  19. """
  20. 图像预处理:读取图像、Letterbox 缩放、归一化、CHW 转换
  21. :param image_path: 图像路径
  22. :return: 模型输入张量, 原始图像, ratio 比例
  23. """
  24. img = cv2.imread(image_path)
  25. h0, w0 = img.shape[:2]
  26. r = min(self.input_size[0] / h0, self.input_size[1] / w0)
  27. resized_img = cv2.resize(img, (int(w0 * r), int(h0 * r)), interpolation=cv2.INTER_LINEAR)
  28. padded_img = np.full((self.input_size[0], self.input_size[1], 3), 114, dtype=np.uint8)
  29. padded_img[:resized_img.shape[0], :resized_img.shape[1]] = resized_img
  30. # BGR -> RGB, HWC -> CHW
  31. img_tensor = padded_img[:, :, ::-1].transpose(self.swap).astype(np.float32)
  32. img_tensor /= 255.0 # 归一化到 [0,1]
  33. img_tensor = np.expand_dims(img_tensor, axis=0) # 添加 batch 维度
  34. return img_tensor, img, r
  35. def predict(self, image_path, conf_thres=0.25, iou_thres=0.45):
  36. """
  37. 对单张图像进行 YOLOv5 推理并返回处理后的检测结果
  38. :param image_path: 图像路径
  39. :param conf_thres: 置信度阈值
  40. :param iou_thres: NMS IOU 阈值
  41. :return: Numpy 数组,每行 [x1, y1, x2, y2, conf, cls]
  42. """
  43. input_tensor, raw_img, ratio = self.input_processing(image_path)
  44. outputs = self.session.run(None, {self.input_name: input_tensor})[0] # [1, N, 6/85]
  45. outputs = self.output_processing(outputs, ratio, conf_thres, iou_thres)
  46. return outputs
  47. def output_processing(self, outputs, ratio, conf_thres, iou_thres):
  48. """
  49. 解析 ONNX 输出并进行后处理(包含 NMS)
  50. :param outputs: 原始模型输出
  51. :param ratio: 输入图像缩放比例
  52. :return: NMS 后的结果 [x1, y1, x2, y2, conf, cls]
  53. """
  54. preds = outputs[0] # [N, 6] 或 [N, 85]
  55. if preds.shape[1] == 6:
  56. # already in [x1, y1, x2, y2, conf, cls]
  57. boxes = preds[:, :4]
  58. scores = preds[:, 4]
  59. classes = preds[:, 5]
  60. else:
  61. boxes = preds[:, :4]
  62. scores_all = preds[:, 5:]
  63. classes = np.argmax(scores_all, axis=1)
  64. scores = scores_all[np.arange(len(classes)), classes]
  65. # 置信度筛选
  66. mask = scores > conf_thres
  67. boxes = boxes[mask]
  68. scores = scores[mask]
  69. classes = classes[mask]
  70. if boxes.shape[0] == 0:
  71. return np.array([])
  72. # 还原坐标
  73. boxes /= ratio
  74. # 执行 NMS
  75. indices = nms(boxes, scores, iou_thres)
  76. dets = np.concatenate([
  77. boxes[indices],
  78. scores[indices, None],
  79. classes[indices, None].astype(np.float32)
  80. ], axis=1)
  81. return dets
  82. def nms(boxes, scores, iou_threshold):
  83. """
  84. 单类 NMS
  85. :param boxes: [N, 4] => x1, y1, x2, y2
  86. :param scores: [N,]
  87. :param iou_threshold: 阈值
  88. :return: 保留索引
  89. """
  90. x1 = boxes[:, 0]
  91. y1 = boxes[:, 1]
  92. x2 = boxes[:, 2]
  93. y2 = boxes[:, 3]
  94. areas = (x2 - x1) * (y2 - y1)
  95. order = scores.argsort()[::-1]
  96. keep = []
  97. while order.size > 0:
  98. i = order[0]
  99. keep.append(i)
  100. xx1 = np.maximum(x1[i], x1[order[1:]])
  101. yy1 = np.maximum(y1[i], y1[order[1:]])
  102. xx2 = np.minimum(x2[i], x2[order[1:]])
  103. yy2 = np.minimum(y2[i], y2[order[1:]])
  104. w = np.maximum(0.0, xx2 - xx1)
  105. h = np.maximum(0.0, yy2 - yy1)
  106. inter = w * h
  107. iou = inter / (areas[i] + areas[order[1:]] - inter)
  108. inds = np.where(iou <= iou_threshold)[0]
  109. order = order[inds + 1]
  110. return keep