My software needs to read a fixed-length handwritten number, for instance 596276.

While I could use a general-purpose library like Tesseract, I am sure there is something smarter. Tesseract will probably misinterpret some of the 1 or 7 as I or l, whereas a software that expects only numbers would not.

Knowing that there are only numbers (American-English way of writing them), the algorithm could focus on 10 potential matches instead of hundreds of symbols.

Any experience OCRing handwritten number-only fields?
What open source library/software did you get the best results with?

Must be open source and work offline. Preferably Java, any other technology accepted (.NET, JavaScript, C, etc) but must be able to run on Linux/Mac/Windows/Android.


2 Answers 2


Designed for printed (not handwritten) text, so accuracy might suffer even for digits only, but anyway:

From the FAQ of Tesseract:

How do I recognize only digits?

In 2.03 and above:


TessBaseAPI::SetVariable("tessedit_char_whitelist", "0123456789");

before calling an Init function or put this in a text file called tessdata/configs/digits:

tessedit_char_whitelist 0123456789

and then your command line becomes:

tesseract image.tif outputbase nobatch digits

Warning: Until the old and new config variables get merged, you must have the nobatch parameter too.

By Joey


You can do this with Python and OpenCV plus giving your recognition engine some training. There is even an example and some initial training samples as part of the default installation.

Example Code

From the tutorials

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('digits.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Now we split the image to 5000 cells, each 20x20 size
cells = [np.hsplit(row,100) for row in np.vsplit(gray,50)]

# Make it into a Numpy array. It size will be (50,100,20,20)
x = np.array(cells)

# Now we prepare train_data and test_data.
train = x[:,:50].reshape(-1,400).astype(np.float32) # Size = (2500,400)
test = x[:,50:100].reshape(-1,400).astype(np.float32) # Size = (2500,400)

# Create labels for train and test data
k = np.arange(10)
train_labels = np.repeat(k,250)[:,np.newaxis]
test_labels = train_labels.copy()

# Initiate kNN, train the data, then test it with test data for k=1
knn = cv2.KNearest()
ret,result,neighbours,dist = knn.find_nearest(test,k=5)

# Now we check the accuracy of classification
# For that, compare the result with test_labels and check which are wrong
matches = result==test_labels
correct = np.count_nonzero(matches)
accuracy = correct*100.0/result.size
print accuracy

Once trained you would save your recognition data for later use.


  • Free Gratis & Open Source
  • Small
  • Cross Platform - Windows, OS-X, Linux, RaspberryPi, Android, iOS, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.