Python uses pytesseract for image recognition tutorial
Python uses pytesseract for image recognition tutorial
In this tutorial, we will learn how to use the Pytesseract library in Python for image recognition.Pytesseract is an excellent OCR (Optical Character Recognition, optical character recognition) library, which can convert texts in the image to computer -readable text.We will use Pytesseract to read images containing Chinese characters and extract the text.
1. Install the necessary software and library:
First of all, we need to install the Tesseract OCR engine, which is the underlying dependencies of Pytesseract.You can download the version suitable for your operating system and install it from https://github.com/tesseract- -Tesseract.During the installation, please pay attention to add the execution path of Tesseract to the system environment variable.
Next, we need to install the Pytesseract library.You can use the following command to install through pip:
pip install pytesseract
Make sure you are using a compatible Python version.
2. Import the necessary library:
Before using Pytesseract, we need to import the necessary libraries.Use the following code to import them in the Python script:
python
import cv2
import pytesseract
from PIL import Image
-CV2: OpenCV library is used for image processing and edge detection.
-Pytesseract: Pytesseract library is used for image recognition.
-Pil: Python Imaging library library is used for image processing.
3. Read and pre -process image:
Before using Pytesseract for image recognition, we need to read and pre -process the image.We can use the OpenCV library to load images and apply necessary pre -processing steps, such as grayscale conversion, dual -value, noise repair, etc.
python
image = cv2.imread('image.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
-Image.png is an image file to be recognized.
-We first, we use the cv2.imread () function to load images.
-When we convert images into gray images, which helps to better identify text.
-When we use the thresholding method to convert the gray image into a two -value image, which helps to better divide the text and background.
4. Use Pytesseract for image recognition:
Once we complete the pre -processing of the image, we can use Pytesseract for image recognition.This is implemented by calling the image_to_string () function of the pytesseract library.
python
text = pytesseract.image_to_string(threshold, lang='chi_sim')
-Threshold is the image we are preparing.
-LANG parameter is used to specify the text language you want to recognize.In our example, we use 'chi_sim' to specify Chinese simplified Chinese.
5. Printing and identifying text results:
Finally, we can print out the results of the identified text.
python
print(text)
Complete Python code example:
python
import cv2
import pytesseract
from PIL import Image
# Read and process images
image = cv2.imread('image.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
threshold = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# Use Pytesseract for image recognition
text = pytesseract.image_to_string(threshold, lang='chi_sim')
#
print(text)
The above code will read image files named Image.png and identify and print Chinese texts in it.
Precautions:
-Pytesseract can identify images containing Chinese characters, but the accuracy of recognition may be different due to factors such as image quality and text definition.
-In the image pre -processing process in step 3, you can customize according to image characteristics and needs.This may include adjusting the threshold, application filter, cutting images, etc.
-Seize that the text and background in the image are sufficient to contrast to improve the accuracy of recognition.
-You can also try to use other language models (such as 'chi_tra' for Chinese traditional), depending on your needs.
-Python's operating environment and corresponding libraries need to be configured correctly, such as the software and libraries required for installation, adding the execution path of Tesseract to the system environment variables.
Through this tutorial, you should now know how to use Pytesseract for image recognition, and you can extract text in images containing Chinese characters.Hope this is helpful to you!