[Python] Try to recognize characters from images with OpenCV and pyocr

Introduction

When I was looking for an article that uses Selenium, I found an article about automating sushi. The method is basically as follows ・ When you start the game, keep entering all the keys ・ When you start the game, take a screenshot and enter the character string obtained by OCR.

This time I tried simple image processing using OpenCV as OCR part and pre-processing

Advance preparation

Installation of tesseract

tesseract is an OCR engine. This time I will run this OCR engine with python's pyocr module Installation is completed with the following command

$ brew install tesseract

Since there is no test data for Japanese as it is, download it from the following URL https://github.com/tesseract-ocr/tessdata ↑ Download jpn.traineddata from this URL to / usr / local / share / tessdata /

Install pyocr and OpenCV

Execute the following command in the terminal to complete

$ pip3 install pyocr
$ pip3 install opencv-python

I will try OCR for the time being

Image preparation

The image for the test is below sushida_ori.png ↓ Trimming sushida_small.png

Save the trimmed version as test.png

OCR with pyocr

import cv2
import pyocr
from PIL import Image
image = "test.png "

img = cv2.imread(image)
tools = pyocr.get_available_tools()
if len(tools) == 0:
    print("No OCR tool found")
    sys.exit(1)
tool = tools[0]
res = tool.image_to_string(
    Image.open("test.png ")
    ,lang="eng")

print(res)

Execution result res1.png Not recognized correctly at all ... After all it seems that pre-processing is necessary

Try to touch OpenCV

I want to preprocess with OpenCV, but I'm new to OpenCV so I'll play with it Try to process your own icon image

import sys
import cv2
import pyocr
import numpy as np
from PIL import Image
image = "test_1.png "
name = "test_1"

#original
img = cv2.imread(image)

#gray
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite(f"1_{name}_gray.png ",img)

#goussian
img = cv2.GaussianBlur(img, (5, 5), 0)
cv2.imwrite(f"2_{name}_gaussian.png ",img)

#threshold
img = cv2.adaptiveThreshold(
    img
    , 255
    , cv2.ADAPTIVE_THRESH_GAUSSIAN_C
    , cv2.THRESH_BINARY
    , 11
    , 2
)
cv2.imwrite(f"3_{name}_threshold.png ",img)

The image in the processing process looks like this 画像処理.png

OpenCV + OCR Preprocess the image used in OCR with OpenCV and try OCR again In the following, grayscale → threshold processing → color inversion is performed as preprocessing.

import sys
import cv2
import pyocr
import numpy as np
from PIL import Image
image = "test.png "
name = "test"

#original
img = cv2.imread(image)
cv2.imwrite(f"1_{name}_original.png ",img)

#gray
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imwrite(f"2_{name}_gray.png ",img)

#threshold
th = 140
img = cv2.threshold(
    img
    , th
    , 255
    , cv2.THRESH_BINARY
)[1]
cv2.imwrite(f"3_{name}_threshold_{th}.png ",img)

#bitwise
img = cv2.bitwise_not(img)
cv2.imwrite(f"4_{name}_bitwise.png ",img)

cv2.imwrite("target.png ",img)

tools = pyocr.get_available_tools()
if len(tools) == 0:
    print("No OCR tool found")
    sys.exit(1)
tool = tools[0]
res = tool.image_to_string(
    Image.open("target.png ")
    ,lang="eng")

print(res)

事前処理.png

Execution result res2.png

It seems that you can recognize it well! This time it's over

Recommended Posts

[Python] Try to recognize characters from images with OpenCV and pyocr
[Python] How to handle Japanese characters with openCV
Try to detect fish with python + OpenCV2.4 (unfinished)
Try projective transformation of images using OpenCV with Python
Try to operate DB with Python and visualize with d3
WEB scraping with python and try to make a word cloud from reviews
I made a program to convert images into ASCII art with Python and OpenCV
Extract images and tables from pdf with python to reduce the burden of reporting
Send experiment results (text and images) to slack with Python
Convert video to black and white with ffmpeg + python + opencv
Shining life with Python and OpenCV
Neural network with OpenCV 3 and Python 3
Create folders from '01' to '12' with python
Try to operate Facebook with Python
Post images from Python to Tumblr
Try face recognition with python + OpenCV
Load gif images with Python + OpenCV
Try to generate a cyclic peptide from an amino acid sequence with Python and RDKit
Python OCR System Raise characters from images to improve work efficiency
Try to recognize and distinguish Shanimas character images using YOLO v3
How to make a surveillance camera (Security Camera) with Opencv and Python
Try to extract a character string from an image with Python3
[python] Extract text from pdf and read characters aloud with Open-Jtalk
Try to display google map and geospatial information authority map with python
The fastest way to get camera images regularly with python opencv
Try to reproduce color film with Python
Try logging in to qiita with Python
From Python to using MeCab (and CaboCha)
Output color characters to pretty with python
Fractal to make and play with Python
Image acquisition from camera with Python + OpenCV
Porting and modifying doublet-solver from python2 to python3.
[python, openCV] base64 Face recognition with images
[Python] Read images with OpenCV (for beginners)
Try calling Python from Ruby with thrift
Add Gaussian noise to images with python2.7
Importing and exporting GeoTiff images with Python
Upload images to Google Drive with Python
Fill the string with zeros in python and count some characters from the string
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Try converting latitude / longitude and world coordinates to each other with python
Try to separate the background and moving object of the video with OpenCV
How to put OpenCV in Raspberry Pi and easily collect images of face detection results with Python
MessagePack-Try to link Java and Python with RPC
Image characters and post to slack (python slackbot)
Convert PDFs to images in bulk with Python
Try running Google Chrome with Python and Selenium
Try to solve the man-machine chart with Python
Try to draw a life curve with python
How to crop an image with Python + OpenCV
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
[Python] How to read data from CIFAR-10 and CIFAR-100
Try to communicate with EV3 and PC! (MQTT)
Try to make a "cryptanalysis" cipher with Python
Make OpenCV3 available from python3 installed with pyenv
Try to automatically generate Python documents with Sphinx
The easiest way to use OpenCV with python
Try to make a dihedral group with Python
Bulk download images from specific URLs with python
Wavelet transform of images with PyWavelets and OpenCV
From Python environment construction to virtual environment construction with anaconda