yolo_model_nms_export.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import time
  4. import cv2
  5. import numpy as np
  6. import onnxruntime
  7. class YOLOv8:
  8. def __init__(self, path, conf_thres=0.7, iou_thres=0.7):
  9. self.conf_threshold = conf_thres
  10. self.iou_threshold = iou_thres
  11. # Initialize model
  12. self.initialize_model(path)
  13. def __call__(self, image):
  14. return self.detect_objects(image)
  15. def initialize_model(self, path):
  16. # 'CUDAExecutionProvider',
  17. self.session = onnxruntime.InferenceSession(path, providers=['CPUExecutionProvider'])
  18. # Get model info
  19. self.get_input_details()
  20. self.get_output_details()
  21. def detect_objects(self, image):
  22. input_tensor,ratio = self.prepare_input(image)
  23. # Perform inference on the image
  24. outputs = self.inference(input_tensor)
  25. self.boxes, self.scores, self.class_ids = self.process_output(outputs,ratio)
  26. return self.boxes, self.scores, self.class_ids
  27. def prepare_input(self, image):
  28. self.img_height, self.img_width = image.shape[:2]
  29. input_img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  30. # Resize图片不要直接使用resize,需要按比例缩放,空白区域填空纯色即可
  31. input_img,ratio = self.ratioresize(input_img)
  32. # Scale input pixel values to 0 to 1
  33. input_img = input_img / 255.0
  34. input_img = input_img.transpose(2, 0, 1)
  35. input_tensor = input_img[np.newaxis, :, :, :].astype(np.float32)
  36. return input_tensor,ratio
  37. def inference(self, input_tensor):
  38. start = time.perf_counter()
  39. outputs = self.session.run(self.output_names, {self.input_names[0]: input_tensor})
  40. # print(f"Inference time: {(time.perf_counter() - start)*1000:.2f} ms")
  41. return outputs
  42. def process_output(self, output,ratio):
  43. predictions = np.squeeze(output[0]).T
  44. # Filter out object confidence scores below threshold
  45. scores = np.max(predictions[:, 4:], axis=1)
  46. predictions = predictions[scores > self.conf_threshold, :]
  47. scores = scores[scores > self.conf_threshold]
  48. if len(scores) == 0:
  49. return [], [], []
  50. # Get the class with the highest confidence
  51. class_ids = np.argmax(predictions[:, 4:], axis=1)
  52. # Get bounding boxes for each object
  53. boxes = self.extract_boxes(predictions,ratio)
  54. # Apply non-maxima suppression to suppress weak, overlapping bounding boxes
  55. indices = self.nms(boxes, scores, self.iou_threshold)
  56. return boxes[indices], scores[indices], class_ids[indices]
  57. def extract_boxes(self, predictions,ratio):
  58. # Extract boxes from predictions
  59. boxes = predictions[:, :4]
  60. # Scale boxes to original image dimensions
  61. # boxes = self.rescale_boxes(boxes)
  62. boxes *= ratio
  63. # Convert boxes to xyxy format
  64. boxes = self.xywh2xyxy(boxes)
  65. return boxes
  66. def rescale_boxes(self, boxes):
  67. # Rescale boxes to original image dimensions
  68. input_shape = np.array([self.input_width, self.input_height, self.input_width, self.input_height])
  69. boxes = np.divide(boxes, input_shape, dtype=np.float32)
  70. boxes *= np.array([self.img_width, self.img_height, self.img_width, self.img_height])
  71. return boxes
  72. def get_input_details(self):
  73. model_inputs = self.session.get_inputs()
  74. self.input_names = [model_inputs[i].name for i in range(len(model_inputs))]
  75. self.input_shape = model_inputs[0].shape
  76. self.input_height = self.input_shape[2]
  77. self.input_width = self.input_shape[3]
  78. def get_output_details(self):
  79. model_outputs = self.session.get_outputs()
  80. self.output_names = [model_outputs[i].name for i in range(len(model_outputs))]
  81. #等比例缩放图片
  82. def ratioresize(self,im, color=114):
  83. shape = im.shape[:2]
  84. new_h, new_w = self.input_height, self.input_width
  85. padded_img = np.ones((new_h, new_w, 3), dtype=np.uint8) * color
  86. # Scale ratio (new / old)
  87. r = min(new_h / shape[0], new_w / shape[1])
  88. # Compute padding
  89. new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
  90. if shape[::-1] != new_unpad:
  91. im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
  92. padded_img[: new_unpad[1], : new_unpad[0]] = im
  93. padded_img = np.ascontiguousarray(padded_img)
  94. return padded_img, 1 / r
  95. def nms(self, boxes, scores, iou_threshold):
  96. # Sort by score
  97. sorted_indices = np.argsort(scores)[::-1]
  98. keep_boxes = []
  99. while sorted_indices.size > 0:
  100. # Pick the last box
  101. box_id = sorted_indices[0]
  102. keep_boxes.append(box_id)
  103. # Compute IoU of the picked box with the rest
  104. ious = self.compute_iou(boxes[box_id, :], boxes[sorted_indices[1:], :])
  105. # Remove boxes with IoU over the threshold
  106. keep_indices = np.where(ious < iou_threshold)[0]
  107. # print(keep_indices.shape, sorted_indices.shape)
  108. sorted_indices = sorted_indices[keep_indices + 1]
  109. return keep_boxes
  110. def compute_iou(self, box, boxes):
  111. # Compute xmin, ymin, xmax, ymax for both boxes
  112. xmin = np.maximum(box[0], boxes[:, 0])
  113. ymin = np.maximum(box[1], boxes[:, 1])
  114. xmax = np.minimum(box[2], boxes[:, 2])
  115. ymax = np.minimum(box[3], boxes[:, 3])
  116. # Compute intersection area
  117. intersection_area = np.maximum(0, xmax - xmin) * np.maximum(0, ymax - ymin)
  118. # Compute union area
  119. box_area = (box[2] - box[0]) * (box[3] - box[1])
  120. boxes_area = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
  121. union_area = box_area + boxes_area - intersection_area
  122. # Compute IoU
  123. iou = intersection_area / union_area
  124. return iou
  125. def xywh2xyxy(self, x):
  126. # Convert bounding box (x, y, w, h) to bounding box (x1, y1, x2, y2)
  127. y = np.copy(x)
  128. y[..., 0] = x[..., 0] - x[..., 2] / 2
  129. y[..., 1] = x[..., 1] - x[..., 3] / 2
  130. y[..., 2] = x[..., 0] + x[..., 2] / 2
  131. y[..., 3] = x[..., 1] + x[..., 3] / 2
  132. return y
  133. if __name__ == "__main__":
  134. model_path = "/Volumes/Media/WorkDoc/Beizhi/CODE/hyd-yolo/models/water_strean_model.pt"
  135. yolov8_detector = YOLOv8(model_path, conf_thres=0.7, iou_thres=0.7)
  136. image = cv2.imread("/Users/zhenqin/temp/yolo_demo/datasets/hyd-action/images/Frame2000128.png")
  137. boxes, scores, class_ids = yolov8_detector(image)
  138. print(boxes, scores, class_ids)