Simple Digit Recognition OCR in OpenCV-Python

I am trying to implement a "Digit Recognition OCR" in OpenCV-Python (cv2). It is just for learning purposes. I would like to learn both KNearest and SVM features in OpenCV.

I have 100 samples (i.e. images) of each digit. I would like to train with them.

There is a sample that comes with OpenCV sample. But I still couldn't figure out on how to use it. I don't understand what are the samples, responses etc. Also, it loads a txt file at first, which I didn't understand first.

Later on searching a little bit, I could find a in cpp samples. I used it and made a code for cv2.KNearest in the model of (just for testing):

import numpy as np
import cv2

fn = ‘’
a = np.loadtxt(fn, np.float32, delimiter=‘,’, converters={ 0 : lambda ch : ord(ch)-ord(‘A’) })
samples, responses = a[:,1:], a[:,0]

model = cv2.KNearest()
retval = model.train(samples,responses)
retval, results, neigh_resp, dists = model.find_nearest(samples, k = 10)

It gave me an array of size 20000, I don’t understand what it is.


1) What is file? How to build that file from my own data set?

2) What does results.reval() denote?

3) How we can write a simple digit recognition tool using file (either KNearest or SVM)?


19.20 GEEK