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/
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.
##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 packTesseract 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: 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 downloadsIn addition to the above steps, we also need to download two modules:pip install pytesseract pip install pillow
Text recognition
(1) Single picture recognitionThe next operation is much simpler. The following is the picture we want to recognize. :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)
Do not go gentle into that good night!
import pytesseract from PIL import Image # 读取图片 im = Image.open('sentence.png') # 识别文字,并指定语言 string = pytesseract.image_to_string(im,) print(string)
Don’t go into that good night meeklyThe 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 recognitionNow 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
import pytesseract # 识别文字 string = pytesseract.image_to_string('text.txt',) print(string)
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)
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!