I've been following the examples here on setting up Python for OCR by training OpenCV using kNN classification. I followed the first example and generated a knn_data.npz
that stores the training data and the training labels for later. What I'm trying to do now is to recall that training data and apply it to an OpenCV image that has a single character inside of it:
# Load training data
trainingData = np.load('knn_data.npz')
train = trainingData['train']
trainLabels = trainingData['train_labels']
knn = cv2.KNearest()
knn.train(train, trainLabels)
letter = cv2.imread('letter.png')
letter = cv2.cvtColor(letter, cv2.COLOR_BGR2GRAY)
print letter.shape
letter = letter.reshape((1,100))
letter = np.float32(letter)
print letter.shape
ret, result, neighbors, dist = knn.find_nearest(letter, k=5)
print result
The 'letter.png'
image is a 10x10 image so it's perfect safe to resize and numpy successfully resizes the image to a 1-dimensional array of shape (1, 100). However, when I try to pass this into the knn.find_nearest(...)
function, I get an error that says to use float-point matrices:
OpenCV Error: Bad argument (Input samples must be floating-point matrix (<num_samples>x<var_count>)) in find_nearest, file /build/buildd/opencv-2.4.8+dfsg1/modules/ml/src/knearest.cpp, line 370
Traceback (most recent call last):
File "sudoku.py", line 103, in <module>
ret, result, neighbors, dist = knn.find_nearest(letter, k=5)
cv2.error: /build/buildd/opencv-2.4.8+dfsg1/modules/ml/src/knearest.cpp:370: error: (-5) Input samples must be floating-point matrix (<num_samples>x<var_count>) in function find_nearest
However, I reshaped my image so that it occupies a single row and converted it into a float so I'm not entirely sure why this error is coming up. Any suggestions?