报错信息如下:
Traceback (most recent call last):
File "/root/autodl-tmp/ssa-gan/pretrain_DAMSM.py", line 276, in <module>
count = train(dataloader, image_encoder, text_encoder,
File "/root/autodl-tmp/ssa-gan/pretrain_DAMSM.py", line 128, in train
build_super_images(imgs[-1].cpu(), captions,
File "/root/autodl-tmp/ssa-gan/miscc/utils.py", line 120, in build_super_images
drawCaption(text_convas, captions, ixtoword, vis_size)
File "/root/autodl-tmp/ssa-gan/miscc/utils.py", line 51, in drawCaption
fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50)
File "/root/miniconda3/envs/ssa-gan/lib/python3.9/site-packages/PIL/ImageFont.py", line 819, in truetype
return freetype(font)
File "/root/miniconda3/envs/ssa-gan/lib/python3.9/site-packages/PIL/ImageFont.py", line 816, in freetype
return FreeTypeFont(font, size, index, encoding, layout_engine)
File "/root/miniconda3/envs/ssa-gan/lib/python3.9/site-packages/PIL/ImageFont.py", line 245, in init
self.font = core.getfont(
OSError: cannot open resource
fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50)
试了很多方法,把FreeMono.ttf字体文件换成各种其他字体文件,最后还是一直报该错误,我的解决方法是:
- 1,找到报错的位置,我的是:File "/root/autodl-tmp/ssa-gan/miscc/utils.py", line 51, in drawCaption中的fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50)这句代码,一直报错。
- 2,在该路径下/root/autodl-tmp/ssa-gan/miscc,建立文件夹,最终的路径会变成 /root/autodl-tmp/ssa-gan/miscc/Pillow/Tests/fonts
- 3,直接去百度搜你想要的字体文件并下载到你建立好的文件夹下root/autodl-tmp/ssa-gan/miscc/Pillow/Tests/fonts/,原代码的字体文件是FreeMono.ttf,此处我下载我自己需要的SimHei.ttf文件
- 4,在utils.py文件中的第51行fnt = ImageFont.truetype('Pillow/Tests/fonts/FreeMono.ttf', 50)这句代码,修改为 fnt = ImageFont.truetype('/root/autodl-tmp/ssa-gan/miscc/Pillow/Tests/fonts/SimHei.ttf', 50)
- 5,最终解决啦