收到警告 -
警告:root:无法加载推荐的字体系列。加载默认的 PIL 字体,可能会出现字体大小问题。为了防止这种情况,建议指定 'font_family' 的值。
.tff 文件位于同一文件夹中。尝试过默认字体也遇到同样的错误。
错误所在的代码块
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
from fontTools.ttLib import TTFont
font_path = 'DejaVuSans.ttf'
font = ImageFont.truetype(font_path, 40)
synthetic_pages = result.synthesize()
plt.imshow(synthetic_pages[0])
plt.axis('off')
plt.show()
完整代码如下
from doctr.models import ocr_predictor
model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
from doctr.io import DocumentFile
# Image
doc = DocumentFile.from_images("Test.jpg")
# Analyze
result = model(doc)
result.show()
import matplotlib.pyplot as plt
from PIL import Image, ImageDraw, ImageFont
from fontTools.ttLib import TTFont
font_path = 'DejaVuSans.ttf'
font = ImageFont.truetype(font_path, 40)
synthetic_pages = result.synthesize()
plt.imshow(synthetic_pages[0])
plt.axis('off')
plt.show()
具体警告与
doctr
功能有关,而不是与 matplotlib
相关。您可以使用这样的自定义字体。当然,字体应该在同一目录中,或者您可以指定字体的路径:)
import matplotlib.pyplot as plt
synthetic_pages = result.synthesize(font_family ='DejaVuSans.ttf', min_font_size=40)
plt.imshow(synthetic_pages[0])
plt.axis('off')
plt.show()