Try to extract a character string from an image with Python3

At first. What is OCR?

OCR is a technology for extracting character strings from images. There is a technology that translates the screen read by your smartphone with Google Translate. That is OCR. It extracts text from the scanned image and performs natural language processing.

So what can you do with OCR? Is it convenient for everyday life?

I know I'm pulling text out of the image, but what else can I use it for? It will be a story. For example, you can read the leaflets you received, prints from your company or school, and read them into Word files. In addition, you can convert the contents on the blackboard or whiteboard into text without having to write them down.

Advance preparation

・ Installation of Python3 ・ Installation of pyocr ・ Pillow installation ・ Installation of tesseract OCR

Python3 installation is long, so I will omit it I'm a Mac user, so I'll only explain that side.

For Windows users, please refer to the author of the link below.

https://qiita.com/henjiganai/items/7a5e871f652b32b41a18

Then for Mac.


pip install Pillow

Or

pip3 install pillow


pip install pyocr

Or

pip3 install pyocr


brew install tesseract


That's it. Note that you may not be able to execute it without sudo in front.

Practice

Targets only png images. I don't know about support for other formats.


import glob
import pyocr.builders
from PIL import Image

#When you receive the file name of the image,Returns txt

class OCRs:

    def __init__(self):

        self.tools = pyocr.get_available_tools()
        self.tool = self.tools[0]
        self.langs = self.tool.get_available_languages()
        self.lang = self.langs[0]
        self.res = False
        print(self.tools)

        if len(self.tools) != 0:

            self.res = True

    def read(self, file_name):

        if not self.res:

            return 'error'

        else:


            txt = self.tool.image_to_string(

                Image.open(file_name),
                lang=self.lang,
                builder=pyocr.builders.TextBuilder()
            )

            return txt

The tinkering name such as OCRs is absent, so go to the content. First, declare the module to be used first.

glob is a module for getting the path in a file (directory). pyocr is a module that bridges Python with an engine called tesseract for performing OCR in Python. PIL is a module required to load images.

And init has a set of things such as tool and lang that are used only once (no need to call). If res does not have an OCR engine, it should have a value of False, and if it does, it should have a value of True.

Yes, it's the main read function. What we are doing is receiving the file name as an argument, OCR (extracting the character string) and returning it as text.

First, determine if you have an OCR engine. If not, the string error is returned. After that, set the image and language, etc., receive the text in txt, and then return it.

Yes, let's go to the main function.


if __name__ == '__main__':

    cl = OCRs()
    cl.__init__()

    file_names = glob.glob('/Users/sa/Desktop/programming/target_folder/*')

    for file_name in file_names:
        
        if cl.read(file_name) == 'error':
            
            print('OCR software was not found.')
            break
        
        else:
        
            print(cl.read(file_name))    

Let's take a look. First, assign the previous class to cl and then call init. Initial setting is complete. Then, use glob to specify the image folder you want to set (OCR). I've modified my configuration a bit for people because it's still difficult to manipulate directories. Don't say stupid? ??

#Directory you want to specify(folder)Put in.
filenames = glob.glob('hogehoge/*')

#Now you can get all the filenames in hogehoge.

Then, using a repeating for statement, throw all the elements into the previous function. If an error is returned, the OCR software is not included.

that's all. If you want to specify only this one image! !! In that case, call it as follows.

cl.read(filename)

Recommended Posts

Try to extract a character string from an image with Python3
# 5 [python3] Extract characters from a character string
I want to extract an arbitrary URL from the character string of the html source with python
Convert a string to an image
Try to beautify with Talking Head Anime from a Single Image [python preparation]
I tried to extract a line art from an image with Deep Learning
[Python] How to invert a character string
Try to generate an image with aliasing
[Introduction to Python] How to split a character string with the split function
How to extract the desired character string from a line 4 commands
Try to generate a cyclic peptide from an amino acid sequence with Python and RDKit
Try to draw a life curve with python
How to crop an image with Python + OpenCV
Try to make a "cryptanalysis" cipher with Python
Try to make a dihedral group with Python
Post an article with an image to WordPress with Python
Extract data from a web page with Python
[Introduction to Python] How to write a character string with the format function
Use BeautifulSoup to extract a link containing a string from an HTML file
How to convert / restore a string with [] in python
How to scrape image data from flickr with python
Try to make a command standby tool with python
[Python] How to expand variables in a character string
From buying a computer to running a program with python
I want to split a character string with hiragana
WEB scraping with python and try to make a word cloud from reviews
Perform a Twitter search from Python and try to generate sentences with Markov chains.
I tried to automatically generate a character string to be input to Mr. Adjustment with Python
How to convert an array to a dictionary with Python [Application]
Try to bring up a subwindow with PyQt5 and Python
Python learning basics ~ How to output (display) a character string? ~
Create a message corresponding to localization with python translation string
Extract the value closest to a value from a Python list element
Cut out an image with python
Create folders from '01' to '12' with python
Try to operate Facebook with Python
[Beginner] Extract character strings with Python
How to quickly count the frequency of appearance of characters from a character string in Python?
Convert to a string while outputting standard output with Python subprocess
Create an instance of a predefined class from a string in Python
How to make a string into an array or an array into a string in Python
[Python] Try to graph from the image of Ring Fit [OCR]
[Introduction to Python] How to output a character string in a Print statement
Try to create a python environment with Visual Studio Code & WSL
How to get a string from a command line argument in python
How to create a heatmap with an arbitrary domain in Python
[Python] I made an image viewer with a simple sorting function.
[Note] Using 16x2-digit character LCD (1602A) from Python with Raspberry Pi
Pass a list by reference from Python to C ++ with pybind11
Try adding a wall to your IFC file with IfcOpenShell python
[Python] Try to recognize characters from images with OpenCV and pyocr
I tried to make an image similarity function with Python + OpenCV
Try to reproduce color film with Python
Try logging in to qiita with Python
HTML email with image to send with python
Create a dummy image with Python + PIL.
I made a character counter with Python
Send a message from Python to Slack
Image acquisition from camera with Python + OpenCV
Try HTML scraping with a Python library
Try calling Python from Ruby with thrift