[Python] [crawler] Using OCR technology to identify graphics verification code

Source: Internet
Author: User

OCR image recognition can often use the TESSEROCR module to recognize the contents of the picture and convert it to text and output

TESSEROCR is an OCR recognition library for Python, a layer of Python apt encapsulation for tesseract. Before installing the TESSEROCR, you need to install the Tesseract

Tessrtact file:

https://digi.bib.uni-mannheim.de/tesseract/

Python installation tessocr: Download the corresponding. WHL file installation (this package PIP way error prone)

Tesseract with the corresponding TESSEROCR:

Https://github.com/simonflueckiger/tesserocr-windows_build/releases

The implementation code is as follows:

From PIL import Image

Import TESSEROCR

#tesserocr识别图片的2种方法

img = Image.open ("code.jpg")

Verify_code1 = Tesserocr.image_to_text (IMG)

#print (VERIFY_CODE1)

Verify_code2 = Tesserocr.file_to_text ("code.jpg")

#print (Verify_code2)

However, when the image of more interference, such as the verification code in excess of line interference image recognition, the content is not very accurate, you need to do a bit of processing, such as turning grayscale, binary operation.

You can use the CONVERT () method of the image object to pass in "L", convert the image to grayscale, and pass in 1 for binary processing of the image (default threshold 127)

Original Verification Code:

img = Image.open ("code.jpg")

img_l = Img.convert ("L")

Img_l.show ()

You can also set your own threshold value

Threshold = #设置二值的阈值100

Table = []

For I in range (256):

If I < threshold:

Table.append (0)

Else

Table.append (1)

#point () Returns a copy of the image pixel value for the given lookup table, the variable table sets 256 values for each channel of the image, assigns a new pattern to the output image, and the image of the "L" and "P" is further converted to an image with the pattern "1"

Image = Img_l.point (table, "1")

Image.show ()

img_1 = Tesserocr.image_to_text (image)

Print (img_1)

>>5sa6

[Python] [crawler] Using OCR technology to identify graphics verification code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.