新版yolov5项目报错ImportError: cannot import name ‘plot_one_box‘ from ‘utils.plots‘

文章描述了在更新Yolo项目时遇到的导入错误,包括`scale_coords`和`letterbox`模块找不到,以及`plot_one_box`的缺失。作者提供了相应的修复方法,包括修改方法名和自定义新的`plot_one_box`函数。
摘要由CSDN通过智能技术生成

新版本的yolo项目,想要用之前写的界面来进行可视化,发现报了好多错。。。记录一下

  • from utils.general import check_img_size, non_max_suppression, scale_coords
    报错:ImportError: cannot import name 'scale_coords' from 'utils.general'
  • from utils.datasets import letterbox
    报错:ModuleNotFoundError: No module named 'utils.datasets'
  • from utils.plots import plot_one_box报错:
    报错ImportError: cannot import name 'plot_one_box' from 'utils.plots'

解决方案如下:

  1. from utils.general import check_img_size, non_max_suppression, scale_coords
    这里的错误是scale_coords方法名字改了,将当前使用的scale_coords全部变成scale_boxes

  2. from utils.datasets import letterbox
    这个错误是由于letterbox被放到dataloaders里面了,改为
    from utils.dataloaders import letterbox
    以上两个改完后如下:
    在这里插入图片描述

  3. from utils.plots import plot_one_box
    这个错误我没有找到他被什么替代了,但是还是有解决方案的。直接在plots里面添加一个新的plot_one_box方法,如下

def plot_one_box(x, img, color=None, label=None, line_thickness=None):
    # Plots one bounding box on image img
    tl = line_thickness or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1  # line/font thickness
    color = color or [random.randint(0, 255) for _ in range(3)]
    c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
    cv2.rectangle(img, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
    if label:
        tf = max(tl - 1, 1)  # font thickness
        t_size = cv2.getTextSize(label, 0, fontScale=tl / 3, thickness=tf)[0]
        c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
        cv2.rectangle(img, c1, c2, color, -1, cv2.LINE_AA)  # filled
        cv2.putText(img, label, (c1[0], c1[1] - 2), 0, tl / 3, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)

实测可行

  • 18
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值