Skip to content

Commit 6a9c01a

Browse files
authored
Make TRDG a pypi package
1 parent 33d8985 commit 6a9c01a

143 files changed

Lines changed: 1197 additions & 500 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.codecov.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
ignore:
2-
- "TextRecognitionDataGenerator/run.py"
2+
- "trdg/run.py"
33
- "tests.py"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,5 @@ ENV/
9999

100100
# mypy
101101
.mypy_cache/
102+
103+
.vscode/*

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ python:
44
install:
55
- pip install -r requirements-hw.txt
66
- pip install codecov
7+
- python3 setup.py install
78
script:
89
- python3 tests.py
910
- coverage run tests.py

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ COPY requirements.txt /app/
2626

2727
RUN pip3 install -r requirements.txt
2828

29-
COPY TextRecognitionDataGenerator/ /app
29+
COPY trdg/ /app

MANIFEST.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include README.md
2+
include LICENSE
3+
include trdg/fonts/latin/*
4+
include trdg/fonts/cn/*
5+
include trdg/pictures/*
6+
include trdg/dicts/*
7+
include trdg/texts/*

README.md

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# TextRecognitionDataGenerator [![TravisCI](https://travis-ci.org/Belval/TextRecognitionDataGenerator.svg?branch=master)](https://travis-ci.org/Belval/TextRecognitionDataGenerator) [![codecov](https://codecov.io/gh/Belval/TextRecognitionDataGenerator/branch/master/graph/badge.svg)](https://codecov.io/gh/Belval/TextRecognitionDataGenerator) [![Documentation Status](https://readthedocs.org/projects/textrecognitiondatagenerator/badge/?version=latest)](https://textrecognitiondatagenerator.readthedocs.io/en/latest/?badge=latest)
1+
# TextRecognitionDataGenerator [![TravisCI](https://travis-ci.org/Belval/TextRecognitionDataGenerator.svg?branch=master)](https://travis-ci.org/Belval/TextRecognitionDataGenerator) [![PyPI version](https://badge.fury.io/py/TextRecognitionDataGenerator.svg)](https://badge.fury.io/py/TextRecognitionDataGenerator) [![codecov](https://codecov.io/gh/Belval/TextRecognitionDataGenerator/branch/master/graph/badge.svg)](https://codecov.io/gh/Belval/TextRecognitionDataGenerator) [![Documentation Status](https://readthedocs.org/projects/textrecognitiondatagenerator/badge/?version=latest)](https://textrecognitiondatagenerator.readthedocs.io/en/latest/?badge=latest)
22

33
A synthetic data generator for text recognition
44

@@ -8,19 +8,9 @@ Generating text image samples to train an OCR software. Now supporting non-latin
88

99
## What do I need to make it work?
1010

11-
I use Archlinux so I cannot tell if it works on Windows yet.
11+
Just install the pip package using `pip install trdg`. Afterwards, you can use `trdg` from the CLI. I recommend using a virtualenv instead of installing with `sudo`.
1212

13-
```
14-
Python 3.X
15-
OpenCV 4 (Works with 3.2, probably works with 2.4)
16-
Pillow
17-
Numpy
18-
Requests
19-
BeautifulSoup
20-
tqdm
21-
```
22-
23-
You can simply use `pip install -r requirements.txt` too.
13+
If you want to add another language, you can clone the repository instead. Simply run `pip install -r requirements.txt`
2414

2515
## Docker image
2616

@@ -35,21 +25,51 @@ docker run /output/path/:/app/out/ -t belval/trdg:latest python3 run.py [args]
3525
The path (`/output/path/`) must be absolute.
3626

3727
## New
28+
- Add python module
29+
- Move `run.py` to an executable python file ([`trdg/bin/trdg`](trdg/bin/trdg))
3830
- Add `--font` to use only one font for all the generated images (Thank you @JulienCoutault!)
3931
- Add `--fit` and `--margins` for finer layout control
4032
- Change the text orientation using the `-or` parameter
41-
- Change the space width using the `-sw` parameter
4233
- Specify text color range using `-tc '#000000,#FFFFFF'`, please note that the quotes are **necessary**
43-
- Explicit alignment when using `-al` with fixed width (0: Left, 1: Center, 2: Right)
4434
- Add support for Simplified and Traditional Chinese
4535

4636
## How does it work?
4737

4838
Words will be randomly chosen from a dictionary of a specific language. Then an image of those words will be generated by using font, background, and modifications (skewing, blurring, etc.) as specified.
4939

50-
### Basic
40+
### Basic (Python module)
41+
42+
The usage as a Python module is very similar to the CLI, but it is more flexible if you want to include it directly in your training pipeline, and will consume less space and memory. There are 4 generators that can be used.
5143

52-
`python run.py -w 5 -f 64`
44+
```py
45+
from TextRecognitionDataGenerator.generators import (
46+
GeneratorFromDict,
47+
GeneratorFromRandom,
48+
GeneratorFromStrings,
49+
GeneratorFromWikipedia,
50+
)
51+
52+
# The generators use the same arguments as the CLI, only as parameters
53+
generator = GeneratorFromStrings(
54+
['Test1', 'Test2', 'Test3'],
55+
blur=2,
56+
random_blur=True
57+
)
58+
59+
for img in generator:
60+
# Do something with the pillow images here.
61+
```
62+
63+
You can see the full class definition here:
64+
65+
- [`GeneratorFromDict`](trdg/generators/from_dict.py)
66+
- [`GeneratorFromRandom`](trdg/generators/from_random.py)
67+
- [`GeneratorFromStrings`](trdg/generators/from_strings.py)
68+
- [`GeneratorFromWikipedia`](trdg/generators/from_wikipedia.py)
69+
70+
### Basic (CLI)
71+
72+
`trdg -c 1000 -w 5 -f 64`
5373

5474
You get 1,000 randomly generated images with random text on them like:
5575

@@ -59,9 +79,11 @@ You get 1,000 randomly generated images with random text on them like:
5979
![4](samples/4.jpg "4")
6080
![5](samples/5.jpg "5")
6181

82+
By default, they will be generated to `out/` in the current working directory.
83+
6284
### Text skewing
6385

64-
What if you want random skewing? Add `-k` and `-rk` (`python run.py -w 5 -f 64 -k 5 -rk`)
86+
What if you want random skewing? Add `-k` and `-rk` (`trdg -c 1000 -w 5 -f 64 -k 5 -rk`)
6587

6688
![6](samples/6.jpg "6")
6789
![7](samples/7.jpg "7")
@@ -114,16 +136,13 @@ It uses a Tensorflow model trained using [this excellent project](https://github
114136

115137
The text is chosen at random in a dictionary file (that can be found in the *dicts* folder) and drawn on a white background made with Gaussian noise. The resulting image is saved as [text]\_[index].jpg
116138

117-
There are a lot of parameters that you can tune to get the results you want, therefore I recommend checking out `python run.py -h` for more information.
139+
There are a lot of parameters that you can tune to get the results you want, therefore I recommend checking out `trdg -h` for more information.
118140

119141
## Create images with Chinese text
120142

121-
It is simple! Just do `python run.py -l cn -c 1000 -w 5`!
143+
It is simple! Just do `trdg -l cn -c 1000 -w 5`!
122144

123145
Generated texts come both in simplified and traditional Chinese scripts.
124-
You may have to edit `texts/cn.txt` to include some meaningful words instead of random glyphs.
125-
126-
Here are examples of what I could make with it:
127146

128147
Traditional:
129148

@@ -148,7 +167,7 @@ If you want to add a new non-latin language, the amount of work is minimal.
148167

149168
1. Create a new folder with your language [two-letters code](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
150169
2. Add a .ttf font in it
151-
3. Edit `run.py` to add an if statement in `load_fonts()`
170+
3. Edit `bin/trdg` to add an if statement in `load_fonts()`
152171
4. Add a text file in `dicts` with the same two-letters code
153172
5. Run the tool as you normally would but add `-l` with your two-letters code
154173

requirements-hw.txt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
beautifulsoup4==4.6.0
2-
numpy==1.15.1
3-
opencv-python==4.0.0.21
4-
tqdm==4.23.4
5-
Pillow==5.1.0
6-
requests==2.20.0
7-
tensorflow==1.13.1
8-
matplotlib==3.0.2
9-
seaborn==0.9.0
1+
.
2+
tensorflow>=1.13.1
3+
matplotlib>=3.0.2
4+
seaborn>=0.9.0

requirements.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
beautifulsoup4==4.6.0
2-
numpy==1.15.1
3-
opencv-python==4.0.0.21
4-
tqdm==4.23.4
5-
Pillow==5.1.0
6-
requests==2.20.0
1+
.

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[metadata]
2+
description-file = README.md

setup.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Always prefer setuptools over distutils
2+
from setuptools import setup, find_packages
3+
4+
# To use a consistent encoding
5+
from codecs import open
6+
from os import path
7+
8+
here = path.abspath(path.dirname(__file__))
9+
10+
with open(path.join(here, "README.md"), encoding="utf-8") as f:
11+
long_description = f.read()
12+
13+
setup(
14+
name="trdg",
15+
version="1.1.0",
16+
description="TextRecognitionDataGenerator: A synthetic data generator for text recognition",
17+
long_description=long_description,
18+
long_description_content_type="text/markdown",
19+
url="https://github.com/Belval/TextRecognitionDataGenerator",
20+
author="Edouard Belval",
21+
author_email="edouard@belval.org",
22+
# Choose your license
23+
license="MIT",
24+
# See https://pypi.python.org/pypi?%3Aaction=list_classifiers
25+
classifiers=[
26+
# 3 - Alpha
27+
# 4 - Beta
28+
# 5 - Production/Stable
29+
"Development Status :: 3 - Alpha",
30+
"Intended Audience :: Developers",
31+
"License :: OSI Approved :: MIT License",
32+
"Programming Language :: Python :: 2",
33+
"Programming Language :: Python :: 2.7",
34+
"Programming Language :: Python :: 3",
35+
"Programming Language :: Python :: 3.4",
36+
"Programming Language :: Python :: 3.5",
37+
"Programming Language :: Python :: 3.6",
38+
"Programming Language :: Python :: 3.7",
39+
],
40+
keywords="synthetic data text-recognition training-set-generator ocr dataset fake text",
41+
packages=find_packages(exclude=["contrib", "docs", "tests"]),
42+
include_package_data=True,
43+
install_requires=[
44+
"pillow>=5.1.0",
45+
"numpy>=1.15.1,<1.17",
46+
"requests>=2.20.0",
47+
"opencv-python>=4.0.0.21",
48+
"tqdm>=4.23.0",
49+
"beautifulsoup4>=4.6.0"
50+
],
51+
scripts=["trdg/bin/trdg"],
52+
)

0 commit comments

Comments
 (0)