title: Simple Digit Recognition OCR in OpenCV-Python
body: <p>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. </p>
  
  <p>I have 100 samples (i.e. images) of each digit. I would like to train with them.</p>
  
  <p>There is a sample <code>letter_recog.py</code> 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.</p>
  
  <p>Later on searching a little bit, I could find a letter_recognition.data in cpp samples. I used it and made a code for cv2.KNearest in the model of letter_recog.py (just for testing):</p>
  
  <pre><code>import numpy as np
  import cv2
  
  fn = 'letter-recognition.data'
  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)
  print results.ravel()
  </code></pre>
  
  <p>It gave me an array of size 20000, I don't understand what it is.</p>
  
  <p><strong>Questions:</strong></p>
  
  <p>1) What is letter_recognition.data file? How to build that file from my own data set?</p>
  
  <p>2) What does <code>results.reval()</code> denote? </p>
  
  <p>3) How we can write a simple digit recognition tool using letter_recognition.data file (either KNearest or SVM)?</p>