训练yolov5时出现报错:AttributeError: ‘FreeTypeFont‘ object has no attribute ‘getsize‘

报错:Traceback (most recent call last): File "D:\anaconda3_xz\envs\luopytorch\lib\threading.py", line 932, in _bootstrap_inner self.run() File "D:\anaconda3_xz\envs\luopytorch\lib\threading.py", line 870, in run self._target(*self._args, **self._kwargs) File "D:\Pythonprojects\yolov5-7.0_222\utils\plots.py", line 305, in plot_images annotator.box_label(box, label, color=color) File "D:\Pythonprojects\yolov5-7.0_222\utils\plots.py", line 91, in box_label w, h = self.font.getsize(label) # text width, height AttributeError: 'FreeTypeFont' object has no attribute 'getsize'

原因:Pillow 的新版中 FreeTypeFont 对象的 getsize 方法已被移除。

解决方法:1、Pillow降级,但是又容易出现其他问题

2、使用 textbbox 方法获取文本尺寸;更新 box_label 方法:具体修改如下:

在utils/plots.py文件中修改box_label()

将原代码中的:

w, h = self.font.getsize(label)  # text width, height

替换为:

bbox = self.font.getbbox(label)
w = bbox[2] - bbox[0]
h = bbox[3] - bbox[1]

问题解决,不再报错。

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值