当前位置: 首页 > news >正文

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

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

flyfish

版本:YOLOv5 6.2
假如图片大小是1080 * 1920 (height * width )
width = 1920
height = 1080

当模型输入是 640 * 640时
shapes = (1080, 1920), (0.33, 0.33), (0.0, 140.0)

640/ 1920 = 0.33
图片缩放到 360 * 640 (height * width )
在这里插入图片描述

当前 模型 输入 必须 640 * 640 时
就将 360 * 640 的图片 装进 640 * 640中
多余的部分都是灰色边框
140是图片上下边框各自的高度
在这里插入图片描述

当模型输入 不必须 640 * 640 时
可以做成填充最小矩形灰色边框能被32整除
360 / 32 = 11.25
384 / 32 = 12
360 * 640 变成了 384 * 640
在这里插入图片描述
这样图片上下有很少的灰色边框
1080 * 1920 (height * width )变 360 * 640 (height * width )

def load_image(self, i):
    # Loads 1 image from dataset index 'i', returns (im, original hw, resized hw)
    im, f, fn = self.ims[i], self.im_files[i], self.npy_files[i],
    if im is None:  # not cached in RAM
        if fn.exists():  # load npy
            im = np.load(fn)
        else:  # read image
            im = cv2.imread(f)  # BGR
            assert im is not None, f'Image Not Found {f}'
        h0, w0 = im.shape[:2]  # orig hw
        r = self.img_size / max(h0, w0)  # ratio
        if r != 1:  # if sizes are not equal
            interp = cv2.INTER_LINEAR if (self.augment or r > 1) else cv2.INTER_AREA
            im = cv2.resize(im, (int(w0 * r), int(h0 * r)), interpolation=interp)
        return im, (h0, w0), im.shape[:2]  # im, hw_original, hw_resized
    return self.ims[i], self.im_hw0[i], self.im_hw[i]  # im, hw_original, hw_resized

360 * 640 (height * width )变 384 * 640 (height * width )

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)

相关文章:

  • Python学习七:数据库编程接口
  • 智能优化算法:侏儒猫鼬优化算法-附代码
  • 【Linux练习生】线程安全
  • 进程间通信之信号量--使用信号实现生产者消费者问题
  • I/O复用--浅谈epoll
  • 图像类找工作面试题(二)——常见问题大总结
  • 【文章阅读】Frustratingly Simple Few-Shot Object Detection
  • 网络安全实战:记一次比较完整的靶机渗透
  • 双非本23秋招之路-从考研跑路到某安全大厂(无实习、项目)
  • 影响深度卷积神经网络算法的关键参数是网络结构
  • 微信小程序|使用小程序制作一个核酸检测点查询工具
  • Python3,5行代码,制作Gif动图,太简单了。
  • 【Python】字符串(简介)
  • (附源码)计算机毕业设计SSM基于健身房管理系统
  • 沉睡者IT - 十月之后「牛市」还是「熊市」
  • 2019年如何成为全栈工程师?
  • Git的一些常用操作
  • Javascript Math对象和Date对象常用方法详解
  • PV统计优化设计
  • react 代码优化(一) ——事件处理
  • React-生命周期杂记
  • redis学习笔记(三):列表、集合、有序集合
  • Sass 快速入门教程
  • Spring声明式事务管理之一:五大属性分析
  • Web Storage相关
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 给初学者:JavaScript 中数组操作注意点
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 如何进阶一名有竞争力的程序员?
  • 如何使用 OAuth 2.0 将 LinkedIn 集成入 iOS 应用
  • 深度解析利用ES6进行Promise封装总结
  • 使用Maven插件构建SpringBoot项目,生成Docker镜像push到DockerHub上
  • 终端用户监控:真实用户监控还是模拟监控?
  • 整理一些计算机基础知识!
  • # MySQL server 层和存储引擎层是怎么交互数据的?
  • #LLM入门|Prompt#1.8_聊天机器人_Chatbot
  • (13)Hive调优——动态分区导致的小文件问题
  • (python)数据结构---字典
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (算法)N皇后问题
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .NET Core 将实体类转换为 SQL(ORM 映射)
  • .net core 控制台应用程序读取配置文件app.config
  • .NET Core工程编译事件$(TargetDir)变量为空引发的思考
  • .NET MVC、 WebAPI、 WebService【ws】、NVVM、WCF、Remoting
  • .NET 使用 JustAssembly 比较两个不同版本程序集的 API 变化
  • .net安装_还在用第三方安装.NET?Win10自带.NET3.5安装
  • .w文件怎么转成html文件,使用pandoc进行Word与Markdown文件转化
  • /bin/rm: 参数列表过长"的解决办法
  • /boot 内存空间不够
  • ::
  • @property @synthesize @dynamic 及相关属性作用探究
  • @property括号内属性讲解
  • [ vulhub漏洞复现篇 ] Apache APISIX 默认密钥漏洞 CVE-2020-13945