Preprocess: scale, normalize, HWC->CHW. img: RGB numpy. Returns: (preprocessed_image, original_size)
(self, img: np.ndarray)
| 121 | self._is_loaded = True |
| 122 | |
| 123 | def _preprocess(self, img: np.ndarray) -> tuple: |
| 124 | """Preprocess: scale, normalize, HWC->CHW. img: RGB numpy. |
| 125 | |
| 126 | Returns: |
| 127 | (preprocessed_image, original_size) |
| 128 | """ |
| 129 | # RMBG-2.0 expects BGR |
| 130 | img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) |
| 131 | h, w = img_bgr.shape[:2] |
| 132 | |
| 133 | # Scale to model input size |
| 134 | img_resized = cv2.resize(img_bgr, self.INPUT_SIZE, interpolation=cv2.INTER_LINEAR) |
| 135 | |
| 136 | # Normalize to [0,1] |
| 137 | img_normalized = img_resized.astype(np.float32) / 255.0 |
| 138 | |
| 139 | # HWC -> CHW |
| 140 | img_transposed = np.transpose(img_normalized, (2, 0, 1)) |
| 141 | |
| 142 | # Add batch dim |
| 143 | img_batch = np.expand_dims(img_transposed, axis=0) |
| 144 | |
| 145 | return img_batch, (h, w) |
| 146 | |
| 147 | def _postprocess(self, pred: np.ndarray, original_size: tuple) -> np.ndarray: |
| 148 | """Extract alpha and resize to original. Returns alpha uint8.""" |