Optical character recognition using different machine learning models
In this recipe, you will learn how to train KNN- and SVM-based digit recognition models. It's a simple Optical Character Recognition (OCR) system that can be extended for other characters as well. OCR is a powerful instrument used in many practical applications for recognizing text documents, automatically reading traffic sign messages, and so on.
Getting ready
Before you proceed with this recipe, you will need to install the OpenCV 3.x Python API package and the matplotlib
package.
How to do it...
- Import the modules:
import cv2 import numpy as np
- Specify a few constants:
CELL_SIZE = 20 # Digit image size. NCLASSES = 10 # Number of digits. TRAIN_RATIO = 0.8 # Part of all samples used for training.
- Read the digits image and prepare the labels:
digits_img = cv2.imread('../data/digits.png', 0) digits = [np.hsplit(r, digits_img.shape[1] // CELL_SIZE) for r in np.vsplit(digits_img, digits_img.shape[0] // CELL_SIZE...