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.
Download the executable file from the following link, and then click next to install it (put it in the pure English path without permission): https://github.com/tesseract-ocr/
You can download the source code and compile it yourself at the following link. https://github.com/tesseract-ocr/tesseract/wiki/Compiling
For more explanations on the use of pychar activation code tutorial, see: https://vrg123.com
Or, under ubuntu, use the following command to install:
sudo apt install tesseract-ocr
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 = Image.open("a.png") # Call image_to_string converts a picture to text text = pytesseract.image_to_string(image) print(text)
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 = "https://passport.lagou.com/vcode/create?from=register&refresh=1513081451891" request.urlretrieve(captchaUrl,'captcha.png') image = Image.open('captcha.png') text = pytesseract.image_to_string(image,lang='eng') print(text) time.sleep(2)