Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7.0版本首次运行报错AttributeError: 'FreeTypeFont' object has no attribute 'getsize' #13342

Open
2 tasks done
MonHer opened this issue Oct 3, 2024 · 1 comment
Open
2 tasks done
Labels
bug Something isn't working python Pull requests that update Python code

Comments

@MonHer
Copy link

MonHer commented Oct 3, 2024

Search before asking

  • I have searched the YOLOv5 issues and found no similar bug report.

YOLOv5 Component

Training

Bug

报错日志:

Exception in thread Thread-16:
Traceback (most recent call last):
  File "D:\ProgramData\anaconda3\envs\38yolov5\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "D:\ProgramData\anaconda3\envs\38yolov5\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "E:\Code\Python\YoloPoker\yolo\utils\plots.py", line 305, in plot_images
    annotator.box_label(box, label, color=color)
  File "E:\Code\Python\YoloPoker\yolo\utils\plots.py", line 91, in box_label
    w, h = self.font.getsize(label)  # text width, height
AttributeError: 'FreeTypeFont' object has no attribute 'getsize' 训练完成报错这个 

#导致原因
这个错误是由于 FreeTypeFont 对象没有 getsize() 方法引起的,这通常是因为 Pillow 版本的问题。自 Pillow 10.0.0 版本起,getsize() 方法被弃用,改为了 textsize() 或 getbbox()
解决办法:

解决方法:

有两种方式可以解决这个问题:

1. 降级 Pillow 版本

YOLOv5 和其他旧代码可能依赖于较早版本的 Pillow,因此降级 Pillow 到适合的版本(比如 9.x.x 版本)可以解决这个问题。使用以下命令降级 Pillow:

pip install pillow==9.5.0

这样可以确保旧的 getsize() 方法在 FreeTypeFont 中可用。

2. 修改代码适应新的 Pillow 版本

如果你希望使用较新的 Pillow 版本,可以通过修改 plots.py 文件中的代码,使用新的 getbbox() 方法替换 getsize()。修改代码如下

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

update 为:

w, h = self.font.getbbox(label)[2:]  # text width, height

getbbox() 返回一个 4 元素的元组 (left, top, right, bottom),其中 right-left 就是宽度,bottom-top 就是高度

Environment

  • yolov5 python 3.8 cuda 12.4

Minimal Reproducible Example

image

Additional

No response

Are you willing to submit a PR?

  • Yes I'd like to help by submitting a PR!
@MonHer MonHer added the bug Something isn't working label Oct 3, 2024
@UltralyticsAssistant UltralyticsAssistant added the python Pull requests that update Python code label Oct 3, 2024
@UltralyticsAssistant
Copy link
Member

👋 Hello @MonHer, thank you for reaching out and for your interest in YOLOv5 🚀!

It seems you're encountering an issue related to the FreeTypeFont object's getsize method. This is commonly due to changes in Pillow versions where getsize has been deprecated. You've already identified potential solutions, which is fantastic! 🎉

To assist us in resolving this bug more effectively, could you provide a minimum reproducible example (MRE)? This will help our team understand the issue better.

In the meantime, ensure your environment is set up as per the requirements:

Requirements

Environments

You can run YOLOv5 in various environments, including:

  • Notebooks with free GPU:
    Run on Gradient
    Open In Colab
    Open In Kaggle
  • Google Cloud and Amazon deep learning environments
  • Docker: Docker Pulls

An Ultralytics engineer will review your issue further. Thank you for your patience and contribution! 😊

🔍 For a more detailed exploration, check our ⭐️ Tutorials.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working python Pull requests that update Python code
Projects
None yet
Development

No branches or pull requests

2 participants