Training with fonts · mindee/doctr · Discussion #1046 (original) (raw)

I wanted to test it out before I made the pull request and I've been encountering a small issue. I followed the steps by double clicking the .ttf file and pressing Install. I later verified that the font was correctly installed by finding it on LibreOffice Writer and it was also in the ~/.local/share/fonts/ directory.

I've been trying to run this on the command line:

python3 doctr/references/recognition/train_pytorch.py        crnn_vgg16_bn \
                                                             --epochs 5  \
                                                             --vocab english \
                                                             --batch_size=16 \
                                                             --pretrained \
                                                             --font "BORING.ttf"

But I am encountering this Exception

Traceback (most recent call last):
  File "/home/carl/Desktop/Scripts/Test/ownocr/doctr/datasets/generator/base.py", line 123, in __init__
    _ = get_font(font, 10)
  File "/home/carl/Desktop/Scripts/Test/ownocr/doctr/utils/fonts.py", line 38, in get_font
    font = ImageFont.truetype(font_family, font_size)
  File "/home/carl/anaconda3/envs/doctemp/lib/python3.7/site-packages/PIL/ImageFont.py", line 959, in truetype
    return freetype(font)
  File "/home/carl/anaconda3/envs/doctemp/lib/python3.7/site-packages/PIL/ImageFont.py", line 956, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "/home/carl/anaconda3/envs/doctemp/lib/python3.7/site-packages/PIL/ImageFont.py", line 248, in __init__
    font, size, index, encoding, layout_engine=layout_engine
OSError: cannot open resource

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "../doctr/references/recognition/train_pytorch.py", line 468, in <module>
    main(args)
  File "../doctr/references/recognition/train_pytorch.py", line 212, in main
    T.RandomApply(T.ColorInversion(), 0.9),
  File "/home/carl/Desktop/Scripts/Test/ownocr/doctr/datasets/generator/base.py", line 125, in __init__
    raise ValueError(f"unable to locate font: {font}")
ValueError: unable to locate font: BORING.ttf

I noticed PIL was being used and tried loading the fonts using code like this:

from PIL import ImageFont
font_type = ImageFont.truetype("fonts/BORING.ttf", 18)

I tried to draw with the above font on a blank image and it worked.
Then I tried entering an incorrect path and got the OSError: cannot open resource exception again which might indicate the source of my issue is maybe the path?
Is this perhaps a mismatch with the english vocab ? Or maybe I'm just missing a few arguments in the command line 😅 ? Please advise...
PS: I am using Ubuntu 22.04, I tested with my custom font and with the boring font above from this page here.