Home  >  Article  >  Backend Development  >  How to identify text in pictures using python

How to identify text in pictures using python

尚
forward
2020-06-15 16:42:354704browse

How to identify text in pictures using python

Tesseract

Text recognition is part of ORC. ORC means optical character recognition, which is commonly known as text recognition. Tesseract is a tool for text recognition. We can quickly implement text recognition by using it with Python. But before that we need to complete a tedious task.

(1) Installation and configuration of Tesseract

Download Tesseract at https://digi.bib.uni-mannheim.de/tesseract/

How to identify text in pictures using python

There are many versions for everyone to choose from, and you can choose according to your own needs. Among them, w32 means 32-bit system, and w64 means 64-bit system. You can just choose the appropriate version. The download speed may be slow.

When installing, we need to know the location of our installation and configure the installation directory into the system path variable. Our path is D:\CodeField\Tesseract-OCR.

How to identify text in pictures using python

##We right-click My Computer/This Computer->Properties->Advanced System Settings->Environment Variables->Path->Edit-> Create a new one and copy our path into it. After adding the system variables, we still need to click OK in turn, so that the configuration is complete.

(2) Download language pack

Tesseract does not support Chinese by default. If you want to recognize Chinese or other languages, you need to download the corresponding language pack. The download address is as follows: https://tesseract -ocr.github.io/tessdoc/Data-Files, after entering the website, we scroll down:

How to identify text in pictures using python

There are two Chinese language packages, one Chinese-Simplified and Chinese -Traditional, they are Simplified Chinese and Traditional Chinese, we can choose the one we need to download. After the download is completed, we need to put it in the tessdata directory under the path of Tesseract. Our path is D:\CodeField\Tesseract-OCR\tessdata.

(3) Other module downloads

In addition to the above steps, we also need to download two modules:

pip install pytesseract
pip install pillow

The first one is for text recognition, and the second one is for text recognition. One is used for image reading. Next we can perform text recognition.

Text recognition

(1) Single picture recognition

The next operation is much simpler. The following is the picture we want to recognize. :

How to identify text in pictures using python

The next step is our text recognition code:

import pytesseract
from PIL import Image
# 读取图片
im = Image.open('sentence.jpg')
# 识别文字
string = pytesseract.image_to_string(im)
print(string)

The recognition results are as follows:

Do not go gentle into that good night!

Because the default is to support English, So we can recognize it directly, but when we want to recognize Chinese or other languages, we need to make some modifications:

import pytesseract
from PIL import Image
# 读取图片
im = Image.open('sentence.png')
# 识别文字,并指定语言
string = pytesseract.image_to_string(im,)
print(string)

During recognition, we set lang='chi_sim', that is, set the language to Simplified Chinese, This setting will only take effect if there is a Simplified Chinese package in your tessdata directory. The following is the picture we used for recognition:

How to identify text in pictures using python

The recognition results are as follows:

Don’t go into that good night meekly

The image content was accurately identified. One thing we need to know is that Tesseract can still recognize English characters after we set the language to Simplified Chinese or other languages.

(2) Batch image recognition

Now that we have listed the single image recognition, we must have the function of batch image recognition, which requires us to prepare a txt file, such as I have a text.txt file with the following content:

sentenceHow to identify text in pictures using python
sentenceHow to identify text in pictures using python

We modify the code as follows:

import pytesseract
# 识别文字
string = pytesseract.image_to_string('text.txt',)
print(string)

However, it is inevitably troublesome to write a txt file by ourselves, so we can modify it as follows:

import os
import pytesseract
# 文字图片的路径
path = 'text_img/'
# 获取图片路径列表
imgs = [path + i for i in os.listdir(path)]
# 打开文件
f = open('text.txt', 'w+', encoding='utf-8')
# 将各个图片的路径写入text.txt文件当中
for img in imgs:
    f.write(img + '\n')
# 关闭文件
f.close()
# 文字识别
string = pytesseract.image_to_string('text.txt',)
print(string)

In this way, we only need to pass in the root directory of a text image to perform batch recognition. During the test, it was found that Tesseract did not accurately recognize elegant fonts such as handwriting and regular script, and the recognition of some complex characters also needs to be improved.

However, the recognition accuracy of fonts with strict strokes such as Song Dynasty and Blockchain is very high. In addition, if the tilt of the image is greater than a certain angle, the recognition results will be very different.

For more related knowledge, please pay attention to the

python video tutorial column

The above is the detailed content of How to identify text in pictures using python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete