(img_path)
| 9 | |
| 10 | # 预处理函数 |
| 11 | def preprocess(img_path): |
| 12 | # 读取图像 |
| 13 | img = cv2.imread(img_path) |
| 14 | |
| 15 | # 缩放 |
| 16 | h, w = img.shape[:2] |
| 17 | img = cv2.resize(img, (224, 224)) |
| 18 | |
| 19 | # 格式转换 |
| 20 | img = img.astype(np.float32) |
| 21 | |
| 22 | # 归一化 |
| 23 | for j in range(3): |
| 24 | img[:, :, j] -= mean[j] |
| 25 | for j in range(3): |
| 26 | img[:, :, j] /= std[j] |
| 27 | img /= 255. |
| 28 | |
| 29 | # 格式转换 |
| 30 | img = img.transpose((2, 0, 1)) |
| 31 | img = img[np.newaxis, ...] |
| 32 | |
| 33 | return img |
| 34 | |
| 35 | # 后处理函数 |
| 36 | def postprocess(results, output_dir, img_path, model_name): |