(img_path, size)
| 9 | |
| 10 | # 数据预处理函数 |
| 11 | def preprocess(img_path, size): |
| 12 | # 图像变换 |
| 13 | transform = Compose([ |
| 14 | Resize( |
| 15 | size, |
| 16 | size, |
| 17 | resize_target=None, |
| 18 | keep_aspect_ratio=False, |
| 19 | ensure_multiple_of=32, |
| 20 | resize_method="upper_bound", |
| 21 | image_interpolation_method=cv2.INTER_CUBIC, |
| 22 | ), |
| 23 | NormalizeImage(mean=[0.485, 0.456, 0.406], |
| 24 | std=[0.229, 0.224, 0.225]), |
| 25 | PrepareForNet() |
| 26 | ]) |
| 27 | |
| 28 | # 读取图片 |
| 29 | img = cv2.imread(img_path) |
| 30 | |
| 31 | # 归一化 |
| 32 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) / 255.0 |
| 33 | |
| 34 | # 图像变换 |
| 35 | img = transform({"image": img})["image"] |
| 36 | |
| 37 | # 新增维度 |
| 38 | img = img[np.newaxis, ...] |
| 39 | |
| 40 | return img |
| 41 | |
| 42 | # 数据后处理函数 |
| 43 | def postprocess(results, output_dir, img_path, model_name): |
no test coverage detected