Training character recognition with ctc

1, Get font

  • win10 font search method [windows + e] - >% WINDIR%/Fonts
  • CentOS Linux font path / usr/share/fonts
root@9080e45b4485:~# apt-get install  fontconfig
root@9080e45b4485:~# fc-list
/usr/share/fonts/truetype/dejavu/DejaVuSerif-Bold.ttf: DejaVu Serif:style=Bold
/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf: DejaVu Sans Mono:style=Book
/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book
/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold
/usr/share/fonts/truetype/dejavu/DejaVuSansMono-Bold.ttf: DejaVu Sans Mono:style=Bold
/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf: DejaVu Serif:style=Book
  • Matplotlib font path / usr/local/lib/python3.5/dist-packages/matplotlib/mpl-data/fonts/ttf/

2, Generate file

  • Generating training data by using font file and text content
class MYFONT():
	def __init__(self, font_path='data'):
		self._Deng_path = 'Deng.ttf'

	def __load_font(self, font_path):
		self.Deng_font = ImageFont.truetype(os.path.join(font_path, self._Deng_path), 23, encoding="unic")

	def set_font_size(self, font_size=71):
	    txt_font_tmp = txt_font.font_variant(size=20, encoding='unic')
		return txt_font_tmp

	def get_txt_cord(self, txt, font):  # Coordinate information of font: [x,y,w,h]
		offset = font.getoffset(txt)
		size = font.getsize(txt)
		return offset + size  # x,y,w,h

	def get_alpha_new_image(self, imgwh):
		img_w, img_h = imgwh

		pil_img ='RGB', (img_w, img_h), (255, 255, 255))
		return pil_img

	def get_txt_part_base(self, imgwh, txt, txt_cord, txt_font):
		//Write the text on the picture with black characters on white background
		:param imgwh: Resolution of target image
		:param txt: Text to be written list variable
		:param txt_cord: Corresponding coordinates
		:param txt_font: Corresponding font
		:return: Result diagram
		pil_img = self.get_alpha_new_image(imgwh)
		pil_img_draw = ImageDraw.Draw(pil_img)

		pil_img_draw.text(txt_cord, txt, (0, 0 , 0), font=txt_font)
		return pil_img
#encoding: utf-8
import os
import lmdb # install lmdb by "pip install lmdb"
import cv2
import numpy as np


def checkImageIsValid(imageBin):
    if imageBin is None:
        return False
    imageBuf = np.fromstring(imageBin, dtype=np.uint8)
    img = cv2.imdecode(imageBuf, cv2.IMREAD_GRAYSCALE)
    imgH, imgW = img.shape[0], img.shape[1]
    if imgH * imgW == 0:
        return False
    return True

def writeCache(env, cache):
    with env.begin(write=True) as txn:
        #for k, v in cache.iteritems(): #py2
        for k, v in cache.items(): #py3
            #txn.put(k, v) #py2
            txn.put(str(k).encode(), v) #py3

def createDataset(outputPath, imagePathList, labelList, lexiconList=None, checkValid=True):
    Create LMDB dataset for CRNN training.

        outputPath    : LMDB output path
        imagePathList : list of image path
        labelList     : list of corresponding groundtruth texts
        lexiconList   : (optional) list of lexicon lists
        checkValid    : if true, check the validity of every image
    assert(len(imagePathList) == len(labelList))
    nSamples = len(imagePathList)
    env =, map_size=1099511627776)
    cache = {}
    cnt = 1
    for i in range(nSamples):
        imagePath = imagePathList[i]
        label = labelList[i]
        if not os.path.exists(imagePath):
            print('%s does not exist' % imagePath)
        #print (imagePath)
        with open(imagePath, 'rb') as f:
            imageBin =
        if checkValid:
            if not checkImageIsValid(imageBin):
                print('%s is not a valid image' % imagePath)

        imageKey = 'image-%09d' % cnt
        labelKey = 'label-%09d' % cnt
        cache[imageKey] = imageBin
        cache[labelKey] = label
        if lexiconList:
            lexiconKey = 'lexicon-%09d' % cnt
            cache[lexiconKey] = ' '.join(lexiconList[i])
        if cnt % 1000 == 0:
            writeCache(env, cache)
            cache = {}
            print('Written %d / %d' % (cnt, nSamples))
        cnt += 1
    nSamples = cnt-1
    # cache['num-samples'] = nSamples #py2
    cache['num-samples'] = str(nSamples).encode() #py3
    writeCache(env, cache)
    print('Created dataset with %d samples' % nSamples)

def getAllUrls(txt):
    rtn = list()
    with open(txt, 'rb') as fp:
        buffs = fp.readlines()
    rtn = [tmp.strip() for tmp in buffs]
    return rtn

if __name__ == '__main__':
    img_pathfile = 'imgslist.txt'  #File list
    labellist = 'labellist.txt'    #Text content corresponding to the file
    outpath = 'trainlmdb'
    if not os.path.exists(outpath):
    imagePathList = getAllUrls(img_pathfile)
    labelList = getAllUrls(labellist)
    createDataset(outpath, imagePathList, labelList)

3, Train crnn


  • Corrupted image 657. It indicates that there are problems in generating lmdb data, and the lmdb data loading process can be referred to Here
#After generating ldmb data, run the program in the root directory of crnn.pytorch to test whether the data is normal
import torch
import dataset   
test_dataset = dataset.lmdbDataset(
    root='carplate_lmdb/val/', transform=dataset.resizeNormalize((100, 32)))
data_loader =
        test_dataset, shuffle=True, batch_size=64, num_workers=1)
val_iter = iter(data_loader)  
preds = preds.squeeze(2)  #Comment on the row



  1. PyTorch bindings for Warp-ctc

  2. crnn.pytorch

  3. Location of truetype font

  4. License plate and logo query

  5.  lmdb module instance source code

99 original articles published, 21 praised, 80000 visitors+
Private letter follow

Tags: encoding Windows CentOS Linux

Posted on Sat, 11 Jan 2020 08:14:12 -0800 by Donny Bahama