Preprocesses the input image before performing inference. Returns: image_data: Preprocessed image data ready for inference.
(self, input_image)
| 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 | """ |