Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion doctr/datasets/generator/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@

from ..datasets import AbstractDataset

# Minimum size (in pixels) of a synthesized image dimension. Whitespace and glyphs a font
# renders blank yield a zero-height/width bounding box; clamping avoids a 0-dimension image
# that crashes the downstream resize during training (#2016).
MIN_SYNTH_IMG_DIM = 1


def synthesize_text_img(
text: str,
Expand All @@ -41,7 +46,8 @@ def synthesize_text_img(
font = get_font(font_family, font_size)
left, top, right, bottom = font.getbbox(text)
text_w, text_h = right - left, bottom - top
h, w = int(round(1.3 * text_h)), int(round(1.1 * text_w))
h = max(int(round(1.3 * text_h)), MIN_SYNTH_IMG_DIM)
w = max(int(round(1.1 * text_w)), MIN_SYNTH_IMG_DIM)
# If single letter, make the image square, otherwise expand to meet the text size
img_size = (h, w) if len(text) > 1 else (max(h, w), max(h, w))

Expand Down
11 changes: 11 additions & 0 deletions tests/common/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
import pytest

from doctr import datasets
from doctr.datasets.generator.base import synthesize_text_img


@pytest.mark.parametrize("text", [" ", " "])
def test_synthesize_text_img_never_degenerate(text):
# A font renders whitespace (and glyphs it lacks) with a zero-height bounding box.
# This used to produce a 0-dimension image that crashes the recognition resize
# during training (#2016). The synthesized image must always be at least 1x1.
img = synthesize_text_img(text)
assert img.height >= 1
assert img.width >= 1


def test_visiondataset():
Expand Down
Loading