[PYTHON] It's a hassle, so I'll automate it! ~ Image automatic loading failure edition ~

Hello!

This time, I would like to use the value list to set goals for the beginning of the year, but it is so troublesome that the question items are just found in the image and typed in ... Therefore, I would like to extract characters automatically.

Execution environment

mac python3 Environment anaconda

How to load an image

・ PIL (pillow) ・ OpenCV ・ Scikit-image

It seems that PLI is the most used. pillow is a typical image processing library in python, and you can easily resize and draw images.

form PIL import Image,ImageFilter
im = Image.open('Image file name')

How to convert to text

-The technology for converting images into text is called OCR (Optical Character Recoding). -OCR uses software called the OCR engine to extract text from image data. ・ This time, I used the open source Tesseract OCR.

-Also, PyOCR is a typical library that enables you to use the OCR engine from Python.

txt = tool.iamge_to_string(
    Image.open('Image file name')
    lang=Language name,
    builder=pyocr.builders.TextBuilder()
)

Actually convert png image to text

brew install tesseract

Also install wget to get pyocr's Japanese learning model

brew install wget

Get jpn.traineddata

wget https://github.com/tesseract-ocr/tessdata/raw/4.00/jpn.traineddata
mv jpn.traineddata /usr/local/Cellar/tesseract/4.1.1/share/tessdata
which tessaract
tessaract -v

・ Code execution

from PIL import Image
import sys

import pyocr
import pyocr.builders

tools = pyocr.get_available_tools()
if len(tools) == 0:
    print("No OCR tool found")
    sys.exit(1)

# The tools are returned in the recommended order of usage
tool = tools[0]
print("Will use tool '%s'" % (tool.get_name()))
# Ex: Will use tool 'libtesseract'

langs = tool.get_available_languages()
print("Available languages: %s" % ", ".join(langs))
lang = 'jpn'
print("Will use lang '%s'" % (lang))
# Ex: Will use lang 'fra'
# Note that languages are NOT sorted in any way. Please refer
# to the system locale settings for the default language
# to use.

txt = tool.image_to_string(
    Image.open('target.png'),
    lang=lang,
    builder=pyocr.builders.TextBuilder()
)
# txt is a Python string

print(txt)

Conversion source target.png

Conversion result

python3 Image-To-Text.py
Will use tool 'Tesseract (sh)'
Available languages: eng, jpn, osd, snum
Will use lang 'jpn'
A ⑤ Rinoma no ⑧ ②

⑧ ② ⑧ Seeing and ①

What is the constant beauty?

I'm excited and excited ⑨ Squeeze M

⑯ BAMN mosquito

H The city where you can go fW

In addition, in Au, in ⑧②, in Aea

⑧ Enjoy the beauty of D

HR&⑦ ⑥ ④

iams aaliitcdiits

‥ Tarollaette Maka ④ Enter$Ru between

EROEREfaiR ezRtd

⑤ @Renai B Maya igu

Doctor i and Ma

① ⑧ f ① to ⑤

Doctor ③ and then M②

GEiESimEreiirimeikid

IR and W-like

The box is ⑤ seeded aREe

⑧ (2) Made of wood

IIN's current M and 00 ④ Kayo aa
Bachelor of Arts for Thrill and BA

-Doku sso 0 zu i ki k sir
What is it?

ERte itha taiii

Reaeimtiisaci

R ④ Koto 0 is also mmWk

N input and f

IR ⑧ Mi+S er

In the country

⑧ ② 0 ⑨ to ① A

⑧ ② ⑧ ⑧ ⑧ ⑧

⑧ ② I think it's a combination ⑧ That's right

R and R

I'm going to IM

HA ① with z

(2) Sprinkle the so on to E

By Potty

① For R in the forest$Wear

See the user-friendly m of the raw M

H&lcmtg$Wear

⑧ ② 0 The load is 0

It's tough ... Is it the quality of the image, or is it a library issue, I can't read it anyway ...

(Addition) Cloud Vision API is far more accurate ...

Screenshot 2021-01-03 at 16.04.06.png

Google's API is really accurate. I want to find time and implement it again.

reference

https://aitanu.com/kachikan-list/ https://qiita.com/seigot/items/7d424000c8d35e5146e4 https://punhundon-lifeshift.com/tesseract_ocr https://webkaru.net/dev/mac-wget-command-install/ https://cloud.google.com/vision/?hl=ja

Recommended Posts

It's a hassle, so I'll automate it! ~ Image automatic loading failure edition ~
It's a hassle to write "coding: utf-8" in Python, so I'll do something with Shellscript
It's Halloween so I'll try to hide it with Python