目标检测 YOLOv5 - 模型推理预处理 letterbox

复制代码
import cv2
import numpy as np

def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
    # Resize and pad image while meeting stride-multiple constraints
    shape = im.shape[:2]  # current shape [height, width]
    if isinstance(new_shape, int):
        new_shape = (new_shape, new_shape)

    # Scale ratio (new / old)
    r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
    if not scaleup:  # only scale down, do not scale up (for better val mAP)
        r = min(r, 1.0)

    # Compute padding
    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
    if auto:  # minimum rectangle
        dw, dh = np.mod(dw, stride), np.mod(dh, stride)  # wh padding
    elif scaleFill:  # stretch
        dw, dh = 0.0, 0.0
        new_unpad = (new_shape[1], new_shape[0])
        ratio = new_shape[1] / shape[1], new_shape[0] / shape[0]  # width, height ratios

    dw /= 2  # divide padding into 2 sides
    dh /= 2

    if shape[::-1] != new_unpad:  # resize
        im = cv2.resize(im, 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))
    im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add border
    return im, ratio, (dw, dh)

# Example usage:
# Load an image
image = cv2.imread(r".\data\images\bus.jpg")

# Resize and pad the image
resized_image, _, _ = letterbox(image, new_shape=(640, 640))

# Save the resized and padded image
cv2.imwrite("../resized_and_padded_image.jpg", resized_image)

auto=True时候结果,此时输出的不是640*640,而是480*640,原图810*1080的,把最长的边缩小到640,而短边按照比列处理.对应模型不是必须要求输入640*640

auto为False时候的结果为,此时的模型输入必须是640*640,短边进行padding,像素为(114,114,114)

目标检测 YOLOv5 - 模型推理预处理 letterbox_yolov5 letterbox-CSDN博客

在训练的是后构造的数据集是调用的这个类,这个类的auto为False

在detect的时候,调用的是loadimages

这个时候的的auto是true

处理不同的图像上的坐标信息,进行坐标变换

复制代码
def scale_coords(img1_shape, coords, img0_shape, ratio_pad=None):  # resize的图, 坐标, 原图, padding的比例
    # Rescale coords (xyxy) from img1_shape to img0_shape
    if ratio_pad is None:  # calculate from img0_shape
        gain = min(img1_shape[0] / img0_shape[0], img1_shape[1] / img0_shape[1])  # gain  = old / new
        pad = (img1_shape[1] - img0_shape[1] * gain) / 2, (img1_shape[0] - img0_shape[0] * gain) / 2  # wh padding
    else:
        gain = ratio_pad[0][0]
        pad = ratio_pad[1]

    coords[:, [0, 2]] -= pad[0]  # x padding
    coords[:, [1, 3]] -= pad[1]  # y padding
    coords[:, :4] /= gain
    clip_coords(coords, img0_shape)
    return coords


def clip_coords(boxes, shape):
    # Clip bounding xyxy bounding boxes to image shape (height, width)
    if isinstance(boxes, torch.Tensor):  # faster individually
        boxes[:, 0].clamp_(0, shape[1])  # x1
        boxes[:, 1].clamp_(0, shape[0])  # y1
        boxes[:, 2].clamp_(0, shape[1])  # x2
        boxes[:, 3].clamp_(0, shape[0])  # y2
    else:  # np.array (faster grouped)
        boxes[:, [0, 2]] = boxes[:, [0, 2]].clip(0, shape[1])  # x1, x2
        boxes[:, [1, 3]] = boxes[:, [1, 3]].clip(0, shape[0])  # y1, y2
相关推荐
yangguangwuyue4 分钟前
Windows 下 CMake + OpenCV 编译乱码问题的定位与解决(Msbuild 乱码问题)
人工智能·windows·opencv
2501_941333108 分钟前
【深度学习强对流天气识别】:基于YOLO11-C3k2-SCcConv模型的高效分类方法_2
人工智能·深度学习·分类
岑梓铭9 分钟前
YOLO11深度学习一模型很优秀还是漏检怎么办,预测解决
人工智能·笔记·深度学习·神经网络·yolo·计算机视觉
龙亘川10 分钟前
GB4599-2024 落地!汽车照明迎重大升级,自适应功能 + 辅助投射成安全新标配~
大数据·人工智能·安全·汽车
叫我:松哥12 分钟前
基于YOLO深度学习算法的人群密集监测与统计分析预警系统,实现人群密集度的实时监测、智能分析和预警功能,支持图片和视频流两种输入方式
人工智能·深度学习·算法·yolo·机器学习·数据分析·flask
Non-existent98712 分钟前
地理空间数据处理指南 | 实战案例+代码TableGIS
人工智能·python·数据挖掘
Dingdangcat8615 分钟前
驾驶行为识别▸方向盘握持与吸烟检测_YOLOv10n_LSCD_LQE模型详解
人工智能·yolo·目标跟踪
Lun3866buzha18 分钟前
✅ 军事目标检测与识别系统 Faster R-CNN实现 士兵坦克车辆武器爆炸物多类别检测 深度学习实战项目(建议收藏)计算机视觉(附源码)
深度学习·目标检测·计算机视觉
2501_9361460424 分钟前
深度学习新突破:YOLOv10n-EMBSFPN如何革新螺旋模式识别与分类任务?_2
深度学习·yolo·分类
Lips61126 分钟前
第七章 贝叶斯分类器
人工智能·算法·机器学习