(img_path)
| 5 | __all__ = ['preprocess', 'postprocess'] |
| 6 | |
| 7 | def preprocess(img_path): |
| 8 | # 读取图片 |
| 9 | img = cv2.imread(img_path) |
| 10 | |
| 11 | # 缩放图像 |
| 12 | img = cv2.resize(img, (256, 256)) |
| 13 | |
| 14 | # 归一化 |
| 15 | img = (img / 255.0 - 0.5) / 0.5 |
| 16 | |
| 17 | # 转置 |
| 18 | img = img.transpose((2, 0, 1)) |
| 19 | |
| 20 | # 增加维度 |
| 21 | img = np.expand_dims(img, axis=0).astype('float32') |
| 22 | |
| 23 | # 返回输入数据 |
| 24 | return img |
| 25 | |
| 26 | def postprocess(outputs, output_dir, model_name): |
| 27 | # 反归一化 |