Handwriting recognition using KNN in Python

Handwriting recognition learned in OpenCV Tutorial.

A newcomer who was assigned this year said, "I used to do character recognition when I was in college," so I'm trying to get a feel for it.

Obtaining dictionary data

Use digits.png included in the OpenCV distribution as dictionary data. Unfortunately, the `` `conda install --channel https://conda.anaconda.org/menpo opencv3``` environment in the Anaconda environment did not include digits.png.

This is the image data of handwritten numbers from 0 to 9. Actually, if you try to create OCR in Japanese, you need to create a huge amount of dictionary data if you implement it as it is. If you try to distinguish the variations of kanji such as Watanabe-san and Saito-san, it will be difficult. Will it be managed?

Before that, what is KNN?

Wikipedia has an entry as k-nearest neighbor method. In particular, Figure has KNN. It's very straightforward and easy to understand.

That is, when it is desired to determine whether an unknown sample belongs to group A or group B in a space of a certain coordinate, it is said that "it belongs to the group with more known classified data in the vicinity". It's about judging.

Now that you understand the concept, it's easy to use in python (+ OpenCV).

Break up dictionary data character by character

cells = [np.hsplit(row,100) for row in np.vsplit(gray,50)]

What is this doing? Speaking of which, the image called digits.png is 1000x2000 size, and each number is written 500 times in 20x20 size, so the one divided by 100 in height and 50 in width is arranged in cells Indicates that it is stored. However, in reality, I can't understand it, or I can never write it myself. It may be enough to know that there are ways to split the array, such as np.hsplit () and np.vsplit ().

Split into training set and test set

After storing this in an array of numpy called x, split 250 of each number into a training set and the remaining 250 into a test set.

x = np.array(cells)

train = x[:,:50].reshape(-1,400).astype(np.float32)
test = x[:,50:100].reshape(-1,400).astype(np.float32)

Also, set a label for each data.

np.arange(10)Then an array of 10 consecutive integers from 0 to 9 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])Is created, repeat it 250 times to label it.



```python
k = np.arange(10)
train_labels = np.repeat(k,250)[:,np.newaxis]
test_labels = train_labels.copy()

KNN execution

Finally run KNN. Let's run it with k = 5.

knn = cv2.KNearest()
knn.train(train,train_labels)
ret,result,neighbours,dist = knn.find_nearest(test,k=5)

Actually, the above is an error. Tutorial seems to be wrong. Rewrite the above three lines as follows.

knn = cv2.ml.KNearest_create()
knn.train(train,cv2.ml.ROW_SAMPLE,train_labels)
ret, results, neighbours, dist = knn.findNearest(test, 5)

Let's take a look at the results of the discriminator.

matches = results==test_labels
correct = np.count_nonzero(matches)
accuracy = correct*100.0/results.size
print(accuracy)

20160909006.png

The result of this execution is `` `91.76```. I have the impression that this accuracy is a little low by distinguishing only 10 characters.

That's all for today.

This code

Recommended Posts

Handwriting recognition using KNN in Python
Speech recognition in Python
Perform handwriting recognition using Pylearn2
Translate using googletrans in Python
Using Python mode in Processing
Number recognition in images with Python
GUI programming in Python using Appjar
Precautions when using pit in Python
Interactive handwriting recognition app using pygame
Try using LevelDB in Python (plyvel)
Using global variables in python functions
Let's see using input in python
Infinite product in Python (using functools)
Edit videos in Python using MoviePy
Try using Leap Motion in Python
Depth-first search using stack in Python
When using regular expressions in Python
Try using GCP Handwriting Recognition (OCR)
GUI creation in python using tkinter 2
Python: Basics of image recognition using CNN
Notes using cChardet and python3-chardet in Python 3.3.1.
Try using the Wunderlist API in Python
GUI creation in python using tkinter part 1
Get Suica balance in Python (using libpafe)
(Bad) practice of using this in Python
Slowly hash passwords using bcrypt in Python
Try using the Kraken API in Python
Using venv in Windows + Docker environment [Python]
[FX] Hit oanda-API in Python using Docker
Python: Application of image recognition using CNN
Image recognition model using deep learning in 2016
Tweet using the Twitter API in Python
[Python] [Windows] Serial communication in Python using DLL
I tried using Bayesian Optimization in Python
Log in to Slack using requests in Python
Get Youtube data in Python using Youtube Data API
Using physical constants in Python scipy.constants ~ constants e ~
Scraping a website using JavaScript in Python
Develop slack bot in python using chat.postMessage
Traffic Safety-kun: Recognition of traffic signs in Python
Write python modules in fortran using f2py
Draw a tree in Python 3 using graphviz
Notes for using python (pydev) in eclipse
Disease classification in Random Forest using Python
Download files in any format using Python
Parallel task execution using concurrent.futures in Python
Notes on using code formatter in Python
Scene recognition by GIST features in Python
Meaning of using DI framework in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
SendKeys in Python
Epoch in Python
Discord in Python
Start using Python
Sudoku in Python
DCI in Python
quicksort in python