Slide 174
Slide 174 text
with np.load('knn_data.npz') as data:
train = data['train']
train_labels = data['train_labels']
knn = cv2.KNearest()
knn.train(train, train_labels)
for i in range(10):
image[i] = cv2.imread( 'data/' + input_number[i], 0)
test[i] = image[i][:,:].reshape(-1, 400).astype(np.float32)
ret, result[i], neighbours, dist = knn.find_nearest(test[i], k=5)
image_result[i] = np.zeros((64, 64, 3), np.uint8)
image_result[i][:,:] = [255, 255, 255]
str[i] = str(result[i][0][0].astype(np.int32))
if result[i][0][0].astype(np.int32) == i:
cv2.putText(image_result[i], str[i], (15,52), font, 2, (0,255,0),3)
else:
cv2.putText(image_result[i], str[i], (15,52), font, 2, (255,0,0),3)
載入切分過的圖檔 , 對測試資料做 kNN
http://arbu00.blogspot.tw/2016/11/1-opencv-knn.html