Graphic verification code recognition technology

Posted by googlit on Mon, 07 Mar 2022 06:09:41 +0100

Graphic verification code recognition technology

Hinder our reptiles. Sometimes it is the graphic verification code when logging in or requesting some data. So here we explain a technology that can translate pictures into words. Translating pictures into words is generally called Optical Character Recognition, abbreviated as OCR. There are not many libraries to implement OCR, especially open source. Because there are certain technical barriers (a lot of data, algorithms, machine learning, in-depth learning knowledge, etc.) in this area, and if done well, it has high commercial value. Therefore, there are few open source. Here is an excellent image recognition open source library: Tesseract.


Tesseract is an OCR library currently sponsored by Google. Tesseract is currently recognized as the best and most accurate open source OCR library. Tesseract has high recognition and high flexibility. He can recognize any font through training.


Windows system:

Download the executable file from the following link, and then click next to install it (put it in the pure English path without permission):

Linux system:

You can download the source code and compile it yourself at the following link.

For more explanations on the use of pychar activation code tutorial, see:

Or, under ubuntu, use the following command to install:

sudo apt install tesseract-ocr

Mac system:

Easy installation with Homebrew:

brew install tesseract

Set environment variables:

After installation, if you want to use Tesseract on the command line, you should set the environment variable. Mac and Linux are set by default when they are installed. Put testseract. Under Windows Add the PATH where exe is located to the PATH environment variable.

Another environment variable to be set is to put the training data file path into the environment variable. In the environment variable, add a TESSDATA_PREFIX=C:\path_to_tesseractdata\teseractdata.

Use tesseract to identify images on the command line:

If you want to use the tesseract command under cmd, you need to set tesseract Put the directory where exe is located in the PATH environment variable. Then use the command: tesseract image PATH file PATH. Example:

tesseract a.png a

Then the picture in a.png will be recognized and the text will be written into a.txt. If you don't want to write a file and want to display it on the terminal directly, don't add the file name.

Use tesseract to identify the image in the code:

Operate testseract in Python code. You need to install a library called py tesseract. Install via pip:

pip install pytesseract

In addition, you need to read pictures with the help of a third-party library called PIL. Check whether it is installed through pip list. If not, install via pip:

pip install PIL

The example code of using pyteseract to convert the text on the picture into text text is as follows:

# Import pytesseract Library
import pytesseract
# Import Image library
from PIL import Image

# Specify Tesseract Path where exe is located
pytesseract.pytesseract.tesseract_cmd = r'D:\ProgramApp\TesseractOCR\tesseract.exe'

# Open picture
image ="a.png")
# Call image_to_string converts a picture to text
text = pytesseract.image_to_string(image)

Use pyteseract to process the drawing verification code:

import pytesseract
from urllib import request
from PIL import Image
import time

pytesseract.pytesseract.tesseract_cmd = r"D:\ProgramApp\TesseractOCR\tesseract.exe"

while True:
    captchaUrl = ""
    image ='captcha.png')
    text = pytesseract.image_to_string(image,lang='eng')