Code Monkey home page Code Monkey logo

synthtiger's Introduction

SynthTIGER 🐯 : Synthetic Text Image Generator

PyPI version CI Docs License Code style: black

Synthetic Text Image Generator for OCR Model | Paper | Documentation | Datasets

Contents

Documentation

The documentation is available at https://clovaai.github.io/synthtiger/.

You can check API reference in this documentation.

Installation

SynthTIGER requires python>=3.6 and libraqm.

To install SynthTIGER from PyPI:

$ pip install synthtiger

If you see a dependency error when you install or run SynthTIGER, install dependencies.

Usage

# Set environment variable (for macOS)
$ export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES
usage: synthtiger [-h] [-o DIR] [-c NUM] [-w NUM] [-s NUM] [-v] SCRIPT NAME [CONFIG]

positional arguments:
  SCRIPT                Script file path.
  NAME                  Template class name.
  CONFIG                Config file path.

optional arguments:
  -h, --help            show this help message and exit
  -o DIR, --output DIR  Directory path to save data.
  -c NUM, --count NUM   Number of output data. [default: 100]
  -w NUM, --worker NUM  Number of workers. If 0, It generates data in the main process. [default: 0]
  -s NUM, --seed NUM    Random seed. [default: None]
  -v, --verbose         Print error messages while generating data.

Examples

SynthTIGER text images

# horizontal
synthtiger -o results -w 4 -v examples/synthtiger/template.py SynthTiger examples/synthtiger/config_horizontal.yaml

# vertical
synthtiger -o results -w 4 -v examples/synthtiger/template.py SynthTiger examples/synthtiger/config_vertical.yaml

  • images: a directory containing images.
  • gt.txt: a file containing text labels.
  • coords.txt: a file containing bounding boxes of characters with text effect.
  • glyph_coords.txt: a file containing bounding boxes of characters without text effect.
  • masks: a directory containing mask images with text effect.
  • glyph_masks: a directory containing mask images without text effect.

Multiline text images

synthtiger -o results -w 4 -v examples/multiline/template.py Multiline examples/multiline/config.yaml

  • images: a directory containing images.
  • gt.txt: a file containing text labels.

Advanced Usage

Non-Latin language data generation

  1. Prepare corpus

    txt format, line by line (example).

  2. Prepare fonts

    See font customization for more details.

  3. Edit corpus path and font path in config file (example)

  4. Run synthtiger

Font customization

  1. Prepare fonts

    ttf/otf format (example).

  2. Extract renderable charsets

    python tools/extract_font_charset.py -w 4 fonts/

    This script extracts renderable charsets for all font files (example).

    Text files are generated in the input path with the same names as the fonts.

  3. Edit font path in config file (example)

  4. Run synthtiger

Colormap customization

  1. Prepare images

    jpg/jpeg/png/bmp format.

  2. Create colormaps

    python tools/create_colormap.py --max_k 3 -w 4 images/ colormap.txt

    This script creates colormaps for all image files (example).

  3. Edit colormap path in config file (example)

  4. Run synthtiger

Template customization

You can implement custom templates by inheriting the base template.

from synthtiger import templates


class MyTemplate(templates.Template):
    def __init__(self, config=None):
        # initialize template.

    def generate(self):
        # generate data.

    def init_save(self, root):
        # initialize something before save.

    def save(self, root, data, idx):
        # save data to specific path.

    def end_save(self, root):
        # finalize something after save.

Datasets

SynthTIGER is available for download at google drive.

Dataset was split into several smaller files. Please download all files and run following command.

# for Linux, macOS
cat synthtiger_v1.0.zip.* > synthtiger_v1.0.zip

# for Windows
copy /b synthtiger_v1.0.zip.* synthtiger_v1.0.zip

synthtiger_v1.0.zip (36G) (md5: 5b5365f4fe15de24e403a9256079be70)

  • Original paper version.
    • Used MJ and ST label.

synthtiger_v1.1.zip (38G) (md5: b2757a7e2b5040b14ed64c473533b592)

Version IIIT5k SVT IC03 IC13 IC15 SVTP CUTE80 Total
1.0 93.2 87.3 90.5 92.9 72.1 77.7 80.6 85.9
1.1 93.4 87.6 91.4 93.2 73.9 77.8 80.6 86.6

Structure

The structure of the dataset is as follows. The dataset contains 10M images.

gt.txt
images/
    0/
        0.jpg
        1.jpg
        ...
        9998.jpg
        9999.jpg
    1/
    ...
    998/
    999/

The format of gt.txt is as follows. Image path and label are separated by tab. (<image_path>\t<label>)

images/0/0.jpg	10
images/0/1.jpg	date:
...
images/999/9999998.jpg	STUFFIER
images/999/9999999.jpg	Re:

Citation

@inproceedings{yim2021synthtiger,
  title={SynthTIGER: Synthetic Text Image GEneratoR Towards Better Text Recognition Models},
  author={Yim, Moonbin and Kim, Yoonsik and Cho, Han-Cheol and Park, Sungrae},
  booktitle={International Conference on Document Analysis and Recognition},
  pages={109--124},
  year={2021},
  organization={Springer}
}

License

SynthTIGER
Copyright (c) 2021-present NAVER Corp.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

The following directories and their subdirectories are licensed the same as their origins. Please refer to NOTICE

docs/
resources/font/

synthtiger's People

Contributors

moonbings avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

synthtiger's Issues

Improving documentation

Hi team,

Is there any chance you could improve the documentation for this library to describe what the functions and classes actually do? I'm having trouble figuring out how to utilize this library for different kinds of unstructured & semi-structured text images.

Issue with synthtiger_v1.1.zip

Hi All,

I have downloaded all parts of synthtiger_v1.1.zip. When I combined into a single zip file, the resulting zip file is not valid.

Anyone faces similar problem ?

Appreciate your advices.

How to generate Non-Broken bengali synthetic data for text recognition?

thank you for your awesome sharing. i tried to generate text recognition data using your library for bangla printed documents,i made all the necessary changes to make this synthesis engine working for bengali text recognition data generation, however for bangla it is breaking text during text to image conversion just like this trdg issue of mine : Belval/TextRecognitionDataGenerator#253 (where the authors of that repo is not responsive at all)

here is a sample that i got while using this synthesis engine.

label : নির্লজ্জ

image : https://i.ibb.co/0Y0W5GG/image.jpg ( নির্লজ্জ word got broken )

do you know how to solve this issue? thanks a lot in advance for your great work.

RuntimeError: Text is not visible

Hello @moonbings While generating Korean dataset, I have added all given requirements like fonts and everything. I am using below command to generate dataset but getting an error between data generation process.

python -m synthtiger -o results -w 4 -v examples/synthtiger/template.py SynthTiger examples/synthtiger/config_vertical.yaml

Generated 56 data
Generated 57 data
Generated 58 data
Generated 59 data
Generated 60 data
Traceback (most recent call last):
  File "/media/cvpr/CM_22/synthtiger/synthtiger/gen.py", line 71, in _generate
    data = template.generate()
  File "/media/cvpr/CM_22/synthtiger/examples/synthtiger/template.py", line 117, in generate
    image = _blend_images(fg_image, bg_image, self.visibility_check)
  File "/media/cvpr/CM_22/synthtiger/examples/synthtiger/template.py", line 246, in _blend_images
    raise RuntimeError("Text is not visible")
RuntimeError: Text is not visible

Generated 61 data
Generated 62 data
Generated 63 data
Generated 64 data
Generated 65 data
Generated 66 data

training configuration of STR model

Hi, thank you for open-sourcing your work.
I have a question about the training configuration of STR model used in your paper.
How did you set the sensitive character mode and data_filtering_off option in BEST model?

The coords.txt file created after generating the images is not very interpretable.

I can understand a 4-point coordinate system (each point of the corners of a rectangular box). However, I have these coordinates.
images\0\0.jpg 6,17,26,41 31,16,55,41 59,16,100,41 108,7,139,41
images\0\2.jpg 9,13,44,58 45,13,66,45 68,3,98,47 102,16,133,47 137,5,167,49 171,18,199,50 202,8,237,65
0 -->images\0\0.jpg
2 -->images\0\2.jpg

I need to make use of these coordinates for training my OCR project. Can you help make it more intuitive? @moonbings

RuntimeError when draw sample with backslash

Hi there! I want to generate samples with backslash and got error:
RuntimeError: There is no font that can render text '3065\': resources/font.
Can somebody help me to address this issue?

synthtiger can't render text that contain '\u200c'

i was trying to generate bangla text recognition synthetic dataset using synthtiger like this : #25
i was able to generate many samples but sometimes it is failing by giving me this error :


Traceback (most recent call last):
  File "/home/apsisdev/.local/lib/python3.8/site-packages/synthtiger/gen.py", line 71, in _generate
    data = template.generate()
  File "/home/apsisdev/mobassir/bnsynthtiger/examples/synthtiger/template.py", line 109, in generate
    fg_image, label = self._generate_fg(fg_color, fg_style)
  File "/home/apsisdev/mobassir/bnsynthtiger/examples/synthtiger/template.py", line 182, in _generate_fg
    font = self.font.sample({"text": text, "vertical": self.vertical})
  File "/home/apsisdev/.local/lib/python3.8/site-packages/synthtiger/components/font/base_font.py", line 51, in sample
    path = meta.get("path", self._sample_font(text))
  File "/home/apsisdev/.local/lib/python3.8/site-packages/synthtiger/components/font/base_font.py", line 134, in _sample_font
    raise RuntimeError(
RuntimeError: There is no font that can render text 'র‌্যাডিসনে': /home/apsisdev/mobassir/bnsynthtiger/resources/font/bn

few more samples for what i got same error above :

  1. 'শুনি—বাংলাদেশি'
  2. 'কর‌্যা'
  3. 'জাতীয়'
  4. 'র‌্যাম্পের'
  5. 'র‌্যাব-পুলিশ'
  6. 'র‌্যাগিংয়ের'
text = 'র‌্যাব-পুলিশ'
[i for i in text]

we checked the failed samples with the code above,
seems like synthtiger is not able to render words that contain '\u200c'
not sure if there are any other failure cases though.

Text rotation

What change should be done to the config file to get images with some text bounding boxes rotated like -90 degrees?

ERROR in Generating Greek language images

Traceback (most recent call last):
File "C:\Users\Muhammad Hamza\AppData\Roaming\Python\Python39\site-packages\synthtiger\gen.py", line 121, in _generate
data = template.generate()
File "E:\datasetPADDLE\examples\synthtiger\template.py", line 113, in generate
fg_image, label, bboxes, glyph_fg_image, glyph_bboxes = self._generate_text(
File "E:\datasetPADDLE\examples\synthtiger\template.py", line 217, in _generate_text
label = self.corpus.data(self.corpus.sample())
File "C:\Users\Muhammad Hamza\AppData\Roaming\Python\Python39\site-packages\synthtiger\components\wrapper\selector.py", line 29, in sample
idx = meta.get("idx", self._sample_idx())
File "C:\Users\Muhammad Hamza\AppData\Roaming\Python\Python39\site-packages\synthtiger\components\wrapper\selector.py", line 58, in _sample_idx
idx = np.random.choice(len(self.components), replace=False, p=self._probs)
File "mtrand.pyx", line 932, in numpy.random.mtrand.RandomState.choice
ValueError: 'a' and 'p' must have same size

kindly someone help

text effects

Hi,
Is there a way to add text effects like bold, underline or middle line?

Slow creation for single page.

I tested performance for single line and single image generation, 20 lines, result as before,
pure pillow, about 2s
synthtiger: 40s

I wonder how to improve speed for synthtiger, not considering worker number.

6090675 function calls (6043707 primitive calls) in 72.690 seconds
Ordered by: internal time

ncalls tottime percall cumtime percall filename:lineno(function)
2892831 34.393 0.000 34.393 0.000 {floodFill}
104 10.388 0.100 46.299 0.445 template_cnjpkr_str.py:295(_check_visibility)
2284 2.987 0.001 2.987 0.001 {method 'getsize' of 'FontFamily' objects}
17726 2.218 0.000 2.218 0.000 {method 'astype' of 'numpy.ndarray' objects}
115572/77434 1.928 0.000 5.748 0.000 {built-in method numpy.core._multiarray_umath.implement_array_function}
150664 1.924 0.000 2.687 0.000 {built-in method numpy.array}
181 1.322 0.007 1.322 0.007 {method 'round' of 'numpy.ndarray' objects}
4434 0.745 0.000 0.745 0.000 {built-in method PIL._imaging.alpha_composite}
6 0.618 0.103 0.789 0.132 blending_functions.py:518(hard_light)
4118 0.557 0.000 4.356 0.001 image_util.py:32(paste_image)
286 0.534 0.002 0.534 0.002 {method 'decode' of 'ImagingDecoder' objects}
1910 0.500 0.000 0.500 0.000 {built-in method PIL._imagingft.getfont}
13686 0.490 0.000 0.491 0.000 {built-in method builtins.max}
4475 0.484 0.000 0.484 0.000 {built-in method builtins.min}
9 0.456 0.051 0.634 0.070 blending_functions.py:139(soft_light)
9 0.451 0.050 0.587 0.065 blending_functions.py:839(overlay)
1329 0.449 0.000 0.449 0.000 {method 'render' of 'FontFamily' objects}
955 0.419 0.000 0.419 0.000 {built-in method PIL._imagingft.getfamily}
141 0.413 0.003 0.413 0.003 {method 'repeat' of 'numpy.ndarray' objects}
54 0.385 0.007 1.911 0.035 arithmetic.py:264(_add_elementwise_to_uint8)
1974 0.378 0.000 0.378 0.000 {warpPerspective}
95 0.358 0.004 0.360 0.004 blending_functions.py:61(_compose_alpha)

Can we generate a fixed size image?

Dear authors, thankyou for providing synthtiger.

I was wondering that can we generate fixed size images like a 128x128 or 256x256?
If yes than how?

Specify text from right to left

Hi, i'm trying to generate images with Arabic and Hebrew, I changed the Corpus path and specified the fonts, but still text is generated and wrriten from left to right like it is in English ad other latin languages, any ideas?
Thank you!

How to choose background?

How to select an area suitable for placing text on the selected large texture image? Is it selected purely random?

It seems that Syn's method of dividing plane is not adopted.

Images are very distorted

Hello @moonbings During synthetic dataset generation, some image are very distorted and i don't have any idea how to fix it. I played with some parameters but it didnt work for me. Any solution?

Samples image are not clear
23
33
83

attribute error for numpy

while running synthtiger -o results -w 4 -v examples/synthtiger/template.py SynthTiger examples/synthtiger/config_vertical.yaml:
facing this issue
AttributeError: np.sctypes was removed in the NumPy 2.0 release. Access dtypes explicitly instead.. Did you mean: 'dtypes'?

libraqm dependency error occurs

Thanks for the nice package!
When i followed your descriptions and used it, i met some error.
KeyError: 'setting text direction, language or font features is not supported without libraqm'
I have installed python package 'synthtiger' and dependency following the shell script
How can i solve this issue?
Thanks,

Characters are being cut

  1. There are cases where the characters are being cut.

6

  1. There are also cases where extra words appear and are not present in GT.

2
In this case only Refraction is coming in gt.txt but not playground.

Vertical, Horizontal, Inverted, Curved images

Is it possible to generate all these types of images using just one config file?

Or do i need to use two separate vertical and horizontal configs?

I'd like to generate a specific distribution of vertical, horizontal, inverted, and curved images, for example 10% curved, 20% vertical, 10% inverted, and 60% horizontal. Is this possible using only one config file?

Thank you

How to get character bbox annotation?

this project is very helpful for generating synth text for scene text recognition, and it seem to generate text image by combine several character images, but the outputs doesn't contain information of each character, is it possible to get character annotation, for example, each character and its location?

'FreeTypeFont' object has no attribute 'getsize'

If anyone is experiencing the error above, this is because Pillow 10 deprecated the method. use getbbox instead. I have a fork ready with that, feel free to clone it and then do pip install ./myfork in the parent folder.

for proper support one could do this to support both versions

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.