How it works...

In this recipe, we apply a lot of different OpenCV functions to build an application for recognizing digits. We use cv2.moment for estimating image skew, and then normalize it with cv2.warpAffine. KNN and SVM models are created with the cv2.ml.KNearest_create and cv2.ml.SVM_create methods. We randomly shuffle all of the available data, and then split it into train/test subsets. The function eval_model computes the overall model accuracy and the confusion matrix. In the results, we can see that the SVM-based model gives slightly better results than the KNN one.

The following output is expected:

KNN accuracy (%): 91.1 KNN confusion matrix: [[101 0 0 0 0 0 1 0 0 2] [ 0 112 3 0 0 0 0 0 0 0] [ 0 1 93 1 0 0 0 0 2 0] [ 1 0 3 100 ...

Get OpenCV 3 Computer Vision with Python Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.