TextRecognitionDataGenerator (trdg)

repository·master·Indexed 25 days ago

https://github.com/belval/textrecognitiondatagenerator

A synthetic data generator for creating text image samples to train OCR software. It supports multiple languages, including non-latin scripts like Chinese and Japanese, and provides augmentations such as skewing, distortion, and blurring. It can be used via CLI, Docker, or as a Python module with generators including GeneratorFromDict, GeneratorFromRandom, GeneratorFromStrings, and GeneratorFromWikipedia.

Tokens
1K
Snippets
4
Records
5
Agent score
37%

What's inside TextRecognitionDataGenerator

  1. Add new languages to TextRecognitionDataGenerator

    master

    To add support for a new non-latin language, follow these steps:

    1. Create a new folder in the fonts/ directory named after your language's two-letter ISO 639-1 code.
    2. Add a .ttf font file to that folder.
    3. Edit run.py to add an if statement in the load_fonts() function to include your new directory.
    4. Add a text file to the dicts folder named with the same two-letter code.
    5. Run the tool using the -l flag with your language code.
  2. Run TextRecognitionDataGenerator using Docker

    master

    If you prefer not to install the package locally, you can use the official Docker image. When running the container, you must provide an absolute path for the volume mapping to save output images.

    Use the following commands to pull the image and run it with arguments:

    docker pull belval/trdg:latest
    
    docker run -v /output/path/:/app/out/ -t belval/trdg:latest trdg [args]
  3. Use TextRecognitionDataGenerator as a Python module

    master

    For integration into training pipelines, use the generators as Python modules. This is more memory-efficient than the CLI. There are four available generators:

    • GeneratorFromDict: Generates text from a dictionary file.
    • GeneratorFromRandom: Generates random text.
    • GeneratorFromStrings: Generates text from a provided list of strings.
    • GeneratorFromWikipedia: Generates text from Wikipedia.

    The generators accept the same arguments as the CLI, but as keyword parameters. They yield tuples of (img, lbl) where img is a Pillow image object.

    from trdg.generators import (
        GeneratorFromDict,
        GeneratorFromRandom,
        GeneratorFromStrings,
        GeneratorFromWikipedia,
    )
    
    # The generators use the same arguments as the CLI, only as parameters
    generator = GeneratorFromStrings(
        ['Test1', 'Test2', 'Test3'],
        blur=2,
        random_blur=True
    )
    
    for img, lbl in generator:
        # Do something with the pillow images here.
  4. Generate text images via CLI

    master

    Use the trdg command to generate synthetic text images. By default, images are saved to the out/ directory in your current working directory.

    Basic usage: trdg -c 1000 -w 5 -f 64 (Generates 1,000 images, 5 words each, font size 64).

    Common CLI flags for augmentation:

    • -k / -rk: Add text skewing and random skewing.
    • -d / -do: Add text distortion and random distortion.
    • -bl / -rbl: Add Gaussian blur with a specific radius and random blur.
    • -b: Define background type: 0 (gaussian noise), 1 (plain white), 2 (quasicrystal), or 3 (image).
    • -hw: Enable experimental handwritten text generation (requires TensorFlow).
    • -l: Specify language (e.g., -l cn for Chinese, -l ja for Japanese).
    • -or: Change text orientation.
    • -tc '#000000,#FFFFFF': Specify text color range (quotes are required).
    • --stroke_width: Set width of text stroke.
    • --stroke_fill: Set color of text contour if stroke > 0.
    • --word_split: Split on word instead of per-character.
    • --dict: Specify a custom dictionary.
    • --font_dir: Specify the fonts directory.
    • --output_mask: Output character-level mask for each image.
    • --character_spacing: Control space between characters in pixels.
    • --font: Use only one specific font.
    • --fit / --margins: Fine layout control.
    trdg -c 1000 -w 5 -f 64