| 187 | |
| 188 | |
| 189 | def preproc(image, input_size, mean, std, swap=(2, 0, 1)): |
| 190 | if len(image.shape) == 3: |
| 191 | padded_img = np.ones((input_size[0], input_size[1], 3)) * 114.0 |
| 192 | else: |
| 193 | padded_img = np.ones(input_size) * 114.0 |
| 194 | img = np.array(image) |
| 195 | r = min(input_size[0] / img.shape[0], input_size[1] / img.shape[1]) |
| 196 | resized_img = cv2.resize( |
| 197 | img, |
| 198 | (int(img.shape[1] * r), int(img.shape[0] * r)), |
| 199 | interpolation=cv2.INTER_LINEAR, |
| 200 | ).astype(np.float32) |
| 201 | padded_img[: int(img.shape[0] * r), : int(img.shape[1] * r)] = resized_img |
| 202 | |
| 203 | padded_img = padded_img[:, :, ::-1] |
| 204 | padded_img /= 255.0 |
| 205 | if mean is not None: |
| 206 | padded_img -= mean |
| 207 | if std is not None: |
| 208 | padded_img /= std |
| 209 | padded_img = padded_img.transpose(swap) |
| 210 | padded_img = np.ascontiguousarray(padded_img, dtype=np.float32) |
| 211 | return padded_img, r |
| 212 | |
| 213 | |
| 214 | class TrainTransform: |