Saturday, March 12, 2011

Handwriting Recognition API

A while ago I was thinking of an iOS project that would utilize handwriting recognition elegantly, and thus better interacts with the user just like Professor Layton series. Then as I looked for robust algorithms that tackle this particular problem I came across this awesome open source library called Zinnia, which not only makes good use of SVM but also provides some ready-to-use training sets including Japanese. Using the library is as straightforward as follows:
recognizer = zinnia.Recognizer()
recognizer.open("C:\Python26\handwriting-ja.model")
character = zinnia.Character()
character.clear()
character.set_width(self.width)
character.set_height(self.height)
counter = 0
for curve in self.curves:
   for x, y in curve:
      character.add(counter, x, y)
   counter+=1
result = recognizer.classify(character, 10)
print unicode(result.value(0), "utf-8")
Where recognizer.open() opens the training module to compare the user mouse strokes against. Then we use set_width(), set_height() to set the canvas size, add() to add the mouse strokes as (x,y) positions. Finally we can pass the character along with the desired number of potential matches to classify(), and print out the most possible UTF-8 string with result.value(0).