The computer can not recognize a picture directly, we need to use some pre-processing means to convert the picture into the content that the computer can recognize, such as the numbers 0 and 1. This section shows you how to make an image recognizable by simply manipulating it in Python.

1. Picture size adjustment and display

The Pillow library is a powerful and simple third-party image processing library that is used by many people. If the library is not available, you can install it using the “PIP Install Pillow” command. The demo code is shown below.

Figure 4. PNG

From PIL import Image img = image.open (" num.png ") img = img.resize((32,32)) img.show()Copy the code

The output is as follows:

2, image gray processing

The original image is a color number 4, we need to grayscale it to black and white number 4, so that it can be converted to numbers 0 and 1 later, the code is as follows.

img = img.convert("L")
img.show()
Copy the code

The output is as follows:

3. Image binarization

After obtaining the black and white number 4, it is time to perform the key image binarization, as shown below.

import numpy as np
img_new = img.point(lambda x:0 if x > 128 else 1) 
arr = np.array(img_new)
Copy the code
  • Line 1 introduces the NumPy library to prepare the image for later conversion into a two-dimensional array.

  • In line 2, the point() function is used to manipulate each pixel. The point() function is the lambda () function that assigns 0 to any pixel with a color value greater than 128 and 1 to any pixel with a color value greater than 128. After gray processing of the image, each pixel point is represented by a number ranging from 0 to 255, where 0 represents black and 255 represents white. Therefore, 128 is used as the threshold to divide the image, that is, the original white-biased area is assigned 0 and the original black-biased area is assigned 1. This completes the job of converting the colors to the numbers 0 and 1.

  • Line 3 uses the NumPy library array() function to convert a 32×32 pixel picture that has been converted to the numbers 0 and 1 into a 32×32 two-dimensional array and assigns it to the variable arr.

At this point, the arR can be directly printed by the print() function, but because there are many rows and columns, the display may not be complete, so we print each line of the ARR in sequence by the following code. Arr. Shape [0] corresponds to the number of rows, and arr. Shape [1] corresponds to the number of columns, so each row can be printed through the for loop.

for i in range(arr.shape[0]):
    print(arr[i])
Copy the code

4. Convert a two-dimensional array to a one-dimensional array

Two-dimensional array of 32 x 32 above cannot be used for data modeling, so also need to reshape (1, 1), () function converts it into a line (if written reshape (1, 1) is converted into a list), which is 1 x 1024 one-dimensional array, the code is as follows.

arr_new = arr.reshape(1,-1)
arr_new
Copy the code

Print the number of rows and columns of the one-dimensional array ARR_new with the following code.

arr_new.shape
Copy the code
(1, 1024)
Copy the code

Pass the processed one-dimensional array arr_new into the previously trained KNN model, the code is as follows.

Download handwriting font recognition. XLSX

Import pandas as pd df = pd.read_excel(" XLSX ") x = df.drop(columns=" corresponding numbers ") y = df[" corresponding numbers "] from sklearn.model_selection import train_test_split x_train,x_test,y_train,y_test = Train_test_split (x,y,test_size=0.2,random_state=123) from sklearn. Neighbors import KNeighborsClassifier as KNN KNN = KNN(n_neighbors=5) knn.fit(x_train,y_train) answer = knn.predict(arr_new)Copy the code

The output is as follows:

5, summary

Generally speaking, k-nearest Neighbor algorithm is a very classical machine learning algorithm, whose principle is clear, simple and easy to understand. However, it also has some disadvantages, for example, when the sample size is large, the calculation is large and the fitting speed is slow.