Python code implementing verification code recognition

Python code implementing verification code recognition

Test Development Community   1 week ago

                                                                                                   

Source / j_hao104  

I. Discussion

Graphic identification code can be said to do compulsory reptiles, related to computer graphics, machine learning, machine vision, artificial intelligence, and so on advanced field ......

Simply put, the main problem in computer graphics is to study how the relevant principles of graphic representation, as well as the use of computer graphics computing, processing and display of the algorithm in the computer. Pattern typically consists of points, lines, surfaces, and the like geometric elements grayscale, color, line type, line thickness non-geometric properties. The computer processing related to the geometry generally n-dimensional to two-dimensional graphics processing, to distinguish the border, area calculation volume calculation, distortion correction. And there is calculated for the color conversion color space, color graphics, shading, color processing and the like.

Knowledge required to break codes used in processing and is typically substantially 2-dimensional color pixel graphic elements, line, surface or the like analysis. Common tools:

  • Support vector machine (SVM)

  • OpenCV

  • Image processing software (Photoshop, Gimp ...)

  • Python Image Library

Two, PIL installation

PIL: Python Imaging Library, the Python standard library of image processing platform, very powerful.

By apt directly installed in Debian / Ubantu Linux:

$sudo apt-get install python-imaging

 

Max and other versions of Linux can be used directly easy_install or pip installation, the installation need to be installed compiler environment:

$ sudo easy_install PIL

Windos platform can go directly to the official website to download the PIL exe installation package. http://pythonware.com/products/pil/

Note: the official website of the installation package to provide 32-bit, 64-bit system go here http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow alternate download package pillow.

Third, the general idea

General idea codes identified as:

1, picture noise reduction

2, cutting pictures

3, the output image text

3.1 Picture Noise Reduction

The so-called noise reduction is to remove all the unnecessary information, such as background, line interference, interfering pixels, etc., only need to identify the text, so that the picture becomes a binary lattice best.

For the color codes of the background: Each pixel can be placed in a five-dimensional space, the dimensions of which are 5, X, Y, R, G, B, i.e. the pixel coordinates and color, computer graphics , there are many color spaces, such as the most commonly used RGB, for printing the CYMK, there are relatively rare HSL or HSV, each dimension of the color space are not the same, but can be converted to each other by the equation. In the RGB color space to distinguish good, it can be converted to HSV color space or HSL. See http://baike.baidu.com/view/3427413.htm color space

CAPTCHA image 7039.jpg:

1. Import Image package, open the picture:

from PIL import Image

im = Image.open('7039.jpg')

 

2, the color image into a grayscale image. RBG conversion to HSI color space, using the I component:

imgry = im.convert('L')

imgry.show()

Gradation looks like this:

3, the binarization processing

Binarization is a commonly used method for image segmentation. When the binarized image pixel gray value greater than a certain threshold gradation value is set to maximum gradation, the gradation value of the pixel is smaller than the minimum value to the gradation, thereby achieving binarization ( usually set to 0-1). Depending on the selected threshold value, the binarization algorithm is divided into fixed threshold an adaptive threshold, chosen here relatively simple fixed threshold.

The pixel set is larger than the threshold value, a, is less than the threshold value is set to 0. Generate a lookup table, and then call the point () mapping.

threshold = 140

table = []

for i in range(256):

    if i < threshold:

        table.append(0)

    else:

        table.append(1)

out = imgry.point(table, '1')

out.show()

 

Processing result looks like this:

3.2 Picture cutting

Key identification codes and difficulty is that the success of the delimiting character, for the same color and totally blocking character, such as google verification code currently can not do more than 5% recognition rate. Google verification code but only substantially 30% of the human recognition rate. Examples of codes used herein, relatively easy to identify. You can not cut, cut picture about the way see this blog: http: //www.cnblogs.com/apexchu/p/4231041.html

Fourth, the use of implemented recognition module pytesser

pytesser OCR is a module Google open source projects, import this module in python can convert the text in the image into text.

Link: https: //code.google.com/p/pytesser/

pytesser called tesseract. Call pytesser module in python, pytesser also used tesseract identify text contained in images.

4.1 pytesser installation

  • If you have not installed PIL, go here to download and install: http: //www.pythonware.com/products/pil/

  • Installation pytesser, download address: http: //code.google.com/p/pytesser/, after downloading extract it directly to the next item code, or extract to Libsite-packages python installation directory and add it to the path environment variable, or be wrong when you import the module.

  • Download Tesseract OCR engine: http: //code.google.com/p/tesseract-ocr/, download, unzip, find tessdata folder, replacing tessdata file decompression after pytesser folder can use it.

  • Also if we are brought into the library from the PIL Image, Image module is not used, so it is necessary to pytesser.py the import Image changed from PIL import Image, followed by the need to create a new __init__.py in pytesser folder empty file.

ps: If you feel troublesome two steps back, the disc can be downloaded directly to the cloud http://yun.baidu.com/s/1jHJvNiI, the operation as in step 2.

4.2 calls pytesser recognition

pytesser provides two image identification method, and the address of the picture image object, the code is determined as follows:

from PIL import Image

from pytesser import pytesser

image = Image.open('7039.jpg')

print pytesser.image_file_to_string('7039.jpg')

print pytesser.image_to_string(image)

Meanwhile pytesser also supports the identification of other languages, such as Chinese. Details, see: http: //blog.csdn.net/hk_jh/article/details/8961449

-END-

Reprinted Disclaimer: This article taken from "j_hao104."

Guess you like

Origin www.cnblogs.com/finer/p/11261836.html