一、问题提出
在运行yolov5时,报错:attributeerror: 'FreeTypeFont' object has no attribute 'getsize'
Image sizes 1536 train, 1536 val
Using 2 dataloader workers
Logging results to runs\train\exp
Starting training for 100 epochs...
Epoch gpu_mem box obj cls labels img_size
0/99 11.3G 0.1147 0.1174 0.0632 154 1536: 0%| | 1/3236 [00:03<3:18:17, 3.68s/it]Exception in thread Thread-7:
Traceback (most recent call last):
File "D:\Anaconda3\envs\wangzk_torch\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "D:\Anaconda3\envs\wangzk_torch\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "D:\CodeProject\My-Models\utils\plots.py", line 186, in plot_images
annotator.text((x + 5, y + 5 + h), text=Path(paths[i]).name[:40], txt_color=(220, 220, 220)) # filenames
File "D:\CodeProject\My-Models\utils\plots.py", line 112, in text
w, h = self.font.getsize(text) # text width, height
AttributeError: 'FreeTypeFont' object has no attribute 'getsize'
0/99 11.3G 0.1145 0.1367 0.0633 231 1536: 0%| | 2/3236 [00:04<1:33:33, 1.74s/it]Exception in thread Thread-8:
Traceback (most recent call last):
File "D:\Anaconda3\envs\wangzk_torch\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "D:\Anaconda3\envs\wangzk_torch\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "D:\CodeProject\My-Models\utils\plots.py", line 186, in plot_images
annotator.text((x + 5, y + 5 + h), text=Path(paths[i]).name[:40], txt_color=(220, 220, 220)) # filenames
File "D:\CodeProject\My-Models\utils\plots.py", line 112, in text
w, h = self.font.getsize(text) # text width, height
AttributeError: 'FreeTypeFont' object has no attribute 'getsize'
二、问题解决
在网上搜索的结果大部分给出字体不对。。。之类的答案,但通过仔细研究,发现该报错的报错原因不是字体不对。
这是因为安装了新版本的 Pillow (10)删除了该getsize 功能,降级到 Pillow 9.5 解决了该问题:
pip install Pillow==9.5
问题解决: