# Knn handwritten digits recognition – OpenCV 3.4 with python 3 Tutorial 36

In this video you will find an easy explanation of how the KNN algorythm works for handwritten digits recognition.

We use a sample of 2500 digits (250 of each type 0 to 9) to train the algorythm and we have another small sample to test it and see if the Knn algorythm can accurately read handwritten digits.

### Source code:

import cv2 import numpy as np digits = cv2.imread("digits.png", cv2.IMREAD_GRAYSCALE) test_digits = cv2.imread("test_digits.png", cv2.IMREAD_GRAYSCALE) rows = np.vsplit(digits, 50) cells = [] for row in rows: row_cells = np.hsplit(row, 50) for cell in row_cells: cell = cell.flatten() cells.append(cell) cells = np.array(cells, dtype=np.float32) k = np.arange(10) cells_labels = np.repeat(k, 250) test_digits = np.vsplit(test_digits, 50) test_cells = [] for d in test_digits: d = d.flatten() test_cells.append(d) test_cells = np.array(test_cells, dtype=np.float32) # KNN knn = cv2.ml.KNearest_create() knn.train(cells, cv2.ml.ROW_SAMPLE, cells_labels) ret, result, neighbours, dist = knn.findNearest(test_cells, k=3) print(result)

### Files:

## 5 Comments

### Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

How can we add another set like test_digits.png if we want to and how we can

Hi,

When I execute the code I get the following cv2.error:

error: (-215:Assertion failed) test_samples.type() == CV_32F && test_samples.cols == samples.cols in function ‘cv::ml::BruteForceImpl::findNearest’

What can be the reason?

This is because the location of 1 or both of your png files are not correct… Please enter the location carefully…

thanks for the example. Please could you please help me to add real time recognition?

Good work! Run like a magic.