Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Belval committed Dec 1, 2017
1 parent 8f2adf1 commit afb9cf0
Show file tree
Hide file tree
Showing 150 changed files with 197 additions and 20 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
language: python
python:
- "3.5"
install:
- pip install pillow
- pip install numpy
- pip install requests
- pip install beautifulsoup4
- pip install opencv-python
script:
- python3 tests/tests.py
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# TextRecognitionDataGenerator
# TextRecognitionDataGenerator [![TravisCI](https://travis-ci.org/Belval/TextRecognitionDataGenerator.svg?branch=master)](https://travis-ci.org/Belval/TextRecognitionDataGenerator)
A synthetic data generator for text recognition

## What is it for?
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

class ComputerTextGenerator(object):
@classmethod
def generate(cls, text, font):
image_font = ImageFont.truetype(font=os.path.join('fonts', font), size=32)
def generate(cls, text, font, text_color):
image_font = ImageFont.truetype(font=font, size=32)
text_width, text_height = image_font.getsize(text)

txt_img = Image.new('L', (text_width, text_height), 255)

txt_draw = ImageDraw.Draw(txt_img)

txt_draw.text((0, 0), text, fill=random.randint(1, 80), font=image_font)
txt_draw.text((0, 0), text, fill=random.randint(1, 80) if text_color < 0 else text_color, font=image_font)

return txt_img
19 changes: 5 additions & 14 deletions data_generator.py → ...ecognitionDataGenerator/data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,29 @@

from PIL import Image, ImageFilter

from computer_text_generator import ComputerTextGenerator
from TextRecognitionDataGenerator.computer_text_generator import ComputerTextGenerator
try:
from handwritten_text_generator import HandwrittenTextGenerator
from TextRecognitionDataGenerator.handwritten_text_generator import HandwrittenTextGenerator
except ImportError as e:
print('Missing modules for handwritten text generation.')
from background_generator import BackgroundGenerator
from TextRecognitionDataGenerator.background_generator import BackgroundGenerator

class FakeTextDataGenerator(object):
@classmethod
def generate(cls, index, text, font, out_dir, height, extension, skewing_angle, random_skew, blur, random_blur, background_type, is_handwritten):
def generate(cls, index, text, font, out_dir, height, extension, skewing_angle, random_skew, blur, random_blur, background_type, is_handwritten, text_color=-1):
image = None

if is_handwritten:
image = HandwrittenTextGenerator.generate(text)
else:
image = ComputerTextGenerator.generate(text, font)
image = ComputerTextGenerator.generate(text, font, text_color)

random_angle = random.randint(0-skewing_angle, skewing_angle)

# Somehow the handwritten text always has a little bit of angle.
# this fixes it.
if is_handwritten:
random_angle = 0
skewing_angle = 0

rotated_img = image.rotate(skewing_angle if not random_skew else random_angle, expand=1)

new_text_width, new_text_height = rotated_img.size

# We create our background a bit bigger than the text
background = None

if background_type == 0:
background = BackgroundGenerator.gaussian_noise(new_text_height + 10, new_text_width + 10)
elif background_type == 1:
Expand Down
Loading

0 comments on commit afb9cf0

Please sign in to comment.