| 53 | |
| 54 | |
| 55 | class Yolo(SimpleOnnxInference): |
| 56 | def __init__( |
| 57 | self, |
| 58 | checkpoint, |
| 59 | device="cuda", |
| 60 | threshold_conf=0.05, |
| 61 | threshold_multi_persons=0.1, |
| 62 | input_resolution=(640, 640), |
| 63 | threshold_iou=0.5, |
| 64 | threshold_bbox_shape_ratio=0.4, |
| 65 | cat_id=[1], |
| 66 | select_type="max", |
| 67 | strict=True, |
| 68 | sorted_func=None, |
| 69 | **kwargs, |
| 70 | ): |
| 71 | super(Yolo, self).__init__(checkpoint, device=device, **kwargs) |
| 72 | |
| 73 | model_inputs = self.session.get_inputs() |
| 74 | input_shape = model_inputs[0].shape |
| 75 | |
| 76 | self.input_width = 640 |
| 77 | self.input_height = 640 |
| 78 | |
| 79 | self.threshold_multi_persons = threshold_multi_persons |
| 80 | self.threshold_conf = threshold_conf |
| 81 | self.threshold_iou = threshold_iou |
| 82 | self.threshold_bbox_shape_ratio = threshold_bbox_shape_ratio |
| 83 | self.input_resolution = input_resolution |
| 84 | self.cat_id = cat_id |
| 85 | self.select_type = select_type |
| 86 | self.strict = strict |
| 87 | self.sorted_func = sorted_func |
| 88 | |
| 89 | def preprocess(self, input_image): |
| 90 | """ |
| 91 | Preprocesses the input image before performing inference. |
| 92 | |
| 93 | Returns: |
| 94 | image_data: Preprocessed image data ready for inference. |
| 95 | """ |
| 96 | img = read_img(input_image) |
| 97 | # Get the height and width of the input image |
| 98 | img_height, img_width = img.shape[:2] |
| 99 | # Resize the image to match the input shape |
| 100 | img = cv2.resize(img, (self.input_resolution[1], self.input_resolution[0])) |
| 101 | # Normalize the image data by dividing it by 255.0 |
| 102 | image_data = np.array(img) / 255.0 |
| 103 | # Transpose the image to have the channel dimension as the first dimension |
| 104 | image_data = np.transpose(image_data, (2, 0, 1)) # Channel first |
| 105 | # Expand the dimensions of the image data to match the expected input shape |
| 106 | # image_data = np.expand_dims(image_data, axis=0).astype(np.float32) |
| 107 | image_data = image_data.astype(np.float32) |
| 108 | # Return the preprocessed image data |
| 109 | return image_data, np.array([img_height, img_width]) |
| 110 | |
| 111 | def postprocess(self, output, shape_raw, cat_id=[1]): |
| 112 | """ |