机器学习笔记
机器学习的7个步骤
数据集最常见的6大问题(附解决方案)
PyTorch Ascend Migration
YOLOv7迁移昇腾NPU推理测试
Yolo V5 V7 前处理
-
+
首页
Yolo V5 V7 前处理
``` import cv2 import numpy as np def letterbox(img, new_shape=(640, 640), color=(114, 114, 114)): shape = img.shape[:2] # current shape [height, width] if isinstance(new_shape, int): new_shape = (new_shape, new_shape) r = min(new_shape[0] / shape[0], new_shape[1] / shape[1]) ratio = r, r # width, height ratios new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r)) dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding dw /= 2 # divide padding into 2 sides dh /= 2 if shape[::-1] != new_unpad: img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR) top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1)) left, right = int(round(dw - 0.1)), int(round(dw + 0.1)) img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) return img, ratio, (dw, dh) def preprocess_v5_v7(img_path, img_size=640): img = cv2.imread(img_path) img, _, _ = letterbox(img, new_shape=img_size) img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x640x640 img = img[np.newaxis, :, :, :] / 255.0 # Add batch dimension, scale (0, 1) img = np.ascontiguousarray(img, dtype=np.float32) # Convert to float32 return img ```
zhangyuheng
2024年6月3日 12:13
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码