Skip to content

Instantly share code, notes, and snippets.

@sdamashek
Created May 10, 2014 01:55
Show Gist options
  • Save sdamashek/b5500f7e74b3c4e2143f to your computer and use it in GitHub Desktop.
Save sdamashek/b5500f7e74b3c4e2143f to your computer and use it in GitHub Desktop.
Letter Recognition
from pybrain.structure import RecurrentNetwork, FeedForwardNetwork, FullConnection, LinearLayer, SigmoidLayer
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer
from math import log,exp
import cv,cv2
import os
import random
import ImageFont, Image, ImageDraw
import time
import numpy as np
import pickle
samplecount = 10 # Number of randomly sized samples per character per font
characters = 'abcdefghijklmnopqrstuvwxyz'
fontpath = "/usr/share/fonts/"
fontnames = ["liberation-fonts/LiberationMono-Bold.ttf","liberation-fonts/LiberationSerif-BoldItalic.ttf","liberation-fonts/LiberationMono-Italic.ttf","liberation-fonts/LiberationSerif-Bold.ttf","liberation-fonts/LiberationMono-Regular.ttf","liberation-fonts/LiberationSerif-Italic.ttf","liberation-fonts/LiberationSans-Bold.ttf","liberation-fonts/LiberationMono-BoldItalic.ttf","liberation-fonts/LiberationSerif-Regular.ttf","liberation-fonts/LiberationSans-Regular.ttf","liberation-fonts/LiberationSans-BoldItalic.ttf","liberation-fonts/LiberationSans-Italic.ttf","libertine-ttf/LinBiolinum_aSI.ttf","libertine-ttf/LinBiolinum_aRL.ttf","libertine-ttf/LinBiolinum_RI.ttf","libertine-ttf/LinBiolinum_aW.ttf","libertine-ttf/LinLibertine_RZ.ttf","libertine-ttf/LinBiolinum_aUB.ttf","libertine-ttf/LinLibertine_RB.ttf","libertine-ttf/LinLibertine_aRL.ttf","libertine-ttf/LinBiolinum_R.ttf","libertine-ttf/LinLibertine_aBS.ttf","libertine-ttf/LinBiolinum_K.ttf","libertine-ttf/LinLibertine_DR.ttf","libertine-ttf/LinBiolinum_RB.ttf","libertine-ttf/LinLibertine_RBI.ttf","libertine-ttf/LinLibertine_aBL.ttf","libertine-ttf/LinLibertine_R.ttf","libertine-ttf/LinBiolinum_aWI.ttf","libertine-ttf/LinBiolinum_aSB.ttf","libertine-ttf/LinBiolinum_aBL.ttf","libertine-ttf/LinBiolinum_aUI.ttf","libertine-ttf/LinLibertine_RI.ttf","libertine-ttf/LinLibertine_aSZI.ttf","libertine-ttf/LinLibertine_aDRL.ttf","libertine-ttf/LinLibertine_I.ttf","libertine-ttf/LinLibertine_aZL.ttf","libertine-ttf/LinBiolinum_aU.ttf","libertine-ttf/LinLibertine_RZI.ttf","libertine-ttf/LinBiolinum_aWB.ttf","libertine-ttf/LinLibertine_aSI.ttf","libertine-ttf/LinLibertine_aS.ttf","libertine-ttf/LinBiolinum_aS.ttf","libertine-ttf/LinLibertine_aDRS.ttf","dejavu/DejaVuSerifCondensed-Italic.ttf","dejavu/DejaVuSerif.ttf","dejavu/DejaVuSansMono.ttf","dejavu/DejaVuSans.ttf","dejavu/DejaVuSansCondensed.ttf","dejavu/DejaVuSansCondensed-Bold.ttf","dejavu/DejaVuSerif-Bold.ttf","dejavu/DejaVuSansCondensed-BoldOblique.ttf","dejavu/DejaVuSans-ExtraLight.ttf","dejavu/DejaVuSans-BoldOblique.ttf","dejavu/DejaVuSansMono-Oblique.ttf","dejavu/DejaVuSerif-Italic.ttf","dejavu/DejaVuSansMono-Bold.ttf","dejavu/DejaVuSans-Bold.ttf","dejavu/DejaVuSerifCondensed-BoldItalic.ttf","dejavu/DejaVuSerif-BoldItalic.ttf","dejavu/DejaVuSansMono-BoldOblique.ttf","dejavu/DejaVuSerifCondensed.ttf","dejavu/DejaVuSerifCondensed-Bold.ttf","dejavu/DejaVuSans-Oblique.ttf","dejavu/DejaVuSansCondensed-Oblique.ttf","corefonts/verdanaz.ttf","corefonts/timesbi.ttf","corefonts/couri.ttf","corefonts/impact.ttf","corefonts/arial.ttf","corefonts/georgiai.ttf","corefonts/comic.ttf","corefonts/ariblk.ttf","corefonts/courbi.ttf","corefonts/georgia.ttf","corefonts/times.ttf","corefonts/verdanai.ttf","corefonts/courbd.ttf","corefonts/trebucit.ttf","corefonts/trebucbi.ttf","corefonts/comicbd.ttf","corefonts/webdings.ttf","corefonts/timesbd.ttf","corefonts/georgiab.ttf","corefonts/cour.ttf","corefonts/timesi.ttf","corefonts/verdana.ttf","corefonts/arialbi.ttf","corefonts/verdanab.ttf","corefonts/trebuc.ttf","corefonts/andalemo.ttf","corefonts/ariali.ttf","corefonts/trebucbd.ttf","corefonts/georgiaz.ttf","corefonts/arialbd.ttf"]
fonts = [os.path.join(fontpath,f) for f in fontnames]
network = FeedForwardNetwork(name="Letter_Recognition")
inputLayer = LinearLayer(400, name="Input")
hiddenLayer = SigmoidLayer(7, name="Pre-Classification")
hiddenLayer2 = SigmoidLayer(7, name="Post-Classification")
outputLayer = LinearLayer(1, name="Output")
network.addInputModule(inputLayer)
network.addModule(hiddenLayer)
network.addModule(hiddenLayer2)
network.addOutputModule(outputLayer)
c1 = FullConnection(inputLayer, hiddenLayer, name="Input_to_Hidden")
c5 = FullConnection(hiddenLayer, hiddenLayer2, name="Hidden_to_Hidden2")
c2 = FullConnection(hiddenLayer2, outputLayer, name="Hidden2_to_Output")
#c3 = FullConnection(hiddenLayer, hiddenLayer, name="Recurrent_Connection")
#c4 = FullConnection(hiddenLayer2, hiddenLayer2, name="Recurrent_Connection2")
network.addConnection(c1)
network.addConnection(c5)
#network.addRecurrentConnection(c3)
network.addConnection(c2)
network.sortModules()
print network
try:
ds = pickle.load(open('_letterds'))
except:
ds = SupervisedDataSet(400, 1)
for font_path in fonts:
print(font_path)
WIDTH = 20
HEIGHT = 20
for char in characters:
for _ in range(samplecount):
pt = random.randint(8,14)
try:
font = ImageFont.truetype(font_path,pt,encoding='unic')
except:
break
size_x, size_y = font.getsize(char)
x = (WIDTH-size_x)/2
y = (HEIGHT-size_y)/2
im = Image.new('RGB',size=(HEIGHT,WIDTH))
draw = ImageDraw.Draw(im)
draw.text((x,y),char,font=font)
cv_img = np.array(im) # RGB image
cv_img = cv_img[:, :, ::-1].copy()
char2 = cv2.Canny(cv_img, 100, 200)
pixels = []
for row in char2:
for pixel in row:
if pixel == 255:
pixels.append(1)
else:
pixels.append(0)
ds.addSample(pixels, [(characters.index(char)+1)/26.])
#cv2.namedWindow("CHAR")
#cv2.imshow("CHAR",cv_img)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
#time.sleep(5000)
pickle.dump(ds, open('_letterds','w'))
print ds
trainer = BackpropTrainer(network, ds, momentum=0.99, learningrate=0.01)
print "\nInitial weights: ", network.params
max_error = 1e-7
error, count = 1, 1000
#Train
while abs(error) >= max_error and count > 0:
error = trainer.train()
count = count - 1
print error
print network.params
pickle.dump(network, open('_network','w'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment