Skip to content

Commit

Permalink
Merge branch 'jackmiller1-multiple-fonts'. Fixes #1.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewearl committed Aug 29, 2016
2 parents fe4d736 + 03fe13a commit 8e6acb1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ Usage is as follows:
This step may take a while as it will extract 108,634 images.

2. `./gen.py 1000`: Generate 1000 test set images in `test/`. (`test/` must not
already exist.) This step requires `UKNumberPlate.ttf` to be in the current
directory, which can be [downloaded here](http://www.dafont.com/uk-number-plate.font).
already exist.) This step requires `UKNumberPlate.ttf` to be in the
`fonts/` directory, which can be
[downloaded here](http://www.dafont.com/uk-number-plate.font).

3. `./train.py`: Train the model. A GPU is recommended for this step. It will
take around 100,000 batches to converge. When you're satisfied that the
Expand All @@ -35,3 +36,8 @@ The project has the following dependencies:
* OpenCV
* NumPy

Different typefaces can be put in `fonts/` in order to match different type
faces. With a large enough variety the network will learn to generalize and
will match as yet unseen typefaces. See
[#1](https://github.com/matthewearl/deep-anpr/issues/1) for more information.

32 changes: 20 additions & 12 deletions gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
)


import itertools
import math
import os
import random
Expand All @@ -47,18 +48,18 @@

import common

FONT_PATH = "UKNumberPlate.ttf"
FONT_DIR = "./fonts"
FONT_HEIGHT = 32 # Pixel size to which the chars are resized

OUTPUT_SHAPE = (64, 128)

CHARS = common.CHARS + " "


def make_char_ims(output_height):
def make_char_ims(font_path, output_height):
font_size = output_height * 4

font = ImageFont.truetype(FONT_PATH, font_size)
font = ImageFont.truetype(font_path, font_size)

height = max(font.getsize(c)[1] for c in CHARS)

Expand Down Expand Up @@ -255,27 +256,34 @@ def generate_im(char_ims, num_bg_images):
return out, code, not out_of_bounds


def generate_ims(num_images):
"""
Generate a number of number plate images.
def load_fonts(folder_path):
font_char_ims = {}
fonts = [f for f in os.listdir(folder_path) if f.endswith('.ttf')]
for font in fonts:
font_char_ims[font] = dict(make_char_ims(os.path.join(folder_path,
font),
FONT_HEIGHT))
return fonts, font_char_ims


:param num_images:
Number of images to generate.
def generate_ims():
"""
Generate number plate images.
:return:
Iterable of number plate images.
"""
variation = 1.0
char_ims = dict(make_char_ims(FONT_HEIGHT))
fonts, font_char_ims = load_fonts(FONT_DIR)
num_bg_images = len(os.listdir("bgs"))
for i in range(num_images):
yield generate_im(char_ims, num_bg_images)
while True:
yield generate_im(font_char_ims[random.choice(fonts)], num_bg_images)


if __name__ == "__main__":
os.mkdir("test")
im_gen = generate_ims(int(sys.argv[1]))
im_gen = itertools.islice(generate_ims(), int(sys.argv[1]))
for img_idx, (im, c, p) in enumerate(im_gen):
fname = "test/{:08d}_{}_{}.png".format(img_idx, c,
"1" if p else "0")
Expand Down
4 changes: 3 additions & 1 deletion train.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ def wrapped(*args, **kwargs):

@mpgen
def read_batches(batch_size):
g = gen.generate_ims()
def gen_vecs():
for im, c, p in gen.generate_ims(batch_size):
for im, c, p in itertools.islice(g, batch_size):
yield im, code_to_vec(p, c)

while True:
Expand Down Expand Up @@ -233,6 +234,7 @@ def do_batch():
try:
last_batch_idx = 0
last_batch_time = time.time()
print "1"
batch_iter = enumerate(read_batches(batch_size))
for batch_idx, (batch_xs, batch_ys) in batch_iter:
do_batch()
Expand Down

0 comments on commit 8e6acb1

Please sign in to comment.