Verification code pictures are taken from a well-known domestic information security site, through the image processing, template contrast recognition and other steps to achieve this kind of simple verification code image recognition function. At the same time, the program realizes a visual interface, and integrates (verification code) image download, (gray value) threshold manual adjustment and other expansion functions. Code exists on GitHub, the portal please poke me.
I. Contents and principles of the procedure
This program is implemented in Python, mainly with the help of PIL (Python image Library, reading images, reading grayscale values and other image processing related functions) and Tkinter (Implement interface design related functions) two libraries. The overall flow of the main program for image processing and recognition is as follows: Import (read) Captcha image, image processing, image recognition, recognition result output.
Figure A program overall process
(i), image processing
The main process of image processing is as follows.
Figure II Image processing flow
1. Reading pictures
Use the PIL.Image.open function in the PIL library to read the CAPTCHA image and return it in pil.image format (we call it im). Image processing and image segmentation are done based on IM format.
In the main program (imgre.py), the target image reading needs to manually enter the picture name (may also need to enter the path of the picture); in the visualizer (imgre_gui.py), simply click "Select Picture" to quickly and easily select the target picture.
Figure 1.1 How to read pictures in imgre.py
Figure 1.2 How to read a picture in a visualization program
2. Picture noise reduction (binary)
Image noise reduction is the purpose of the different gray values of the background, interference lines and other redundant information, leaving only a single background and the required verification code information, so that the next picture cutting, recognition and other steps. In this program, through the two value processing to make the verification code picture into a binary lattice (and corresponding bitmap), in order to achieve the function of noise reduction, the function to achieve the function named "Denoise" in the class Div.
In this project, we can find that this kind of verification code has the following characteristics: Gray value Distribution Law is more obvious, verification code information gray value is 0 or 255, the background information gray value is between 2 to 253; The verification code is more "square" than the "founder", so there is no need to do too much image standardization processing. Based on these characteristics, this program uses the method of intercepting gray value threshold and gray value mapping to binary image processing, so as to achieve the purpose of noise reduction. For example, 2.1 is a verification code picture, figure 2.2 is the gray histogram of the graph, (has been normalized processing, the gray value of the horizontal axis from left to right for 0 to 255).
Figure 2.1 A verification code picture
Figure 2.2 Verification code picture grayscale histogram
As shown in Figure 2.2, its grayscale value is more than 255 pixels (corresponding to the original image of the white Verification Code Information "S2JW"), and the grayscale value is about 10 pixels (corresponding to the original black ring image), other gray value pixels scattered distribution (corresponding to the gray background in the original), Therefore, the threshold is set to 253-255 to intercept the right of the Verification Code information, the threshold of the interception of gray values mapped to 0 (corresponding to black), the other gray values (0-253) all mapped to 1 (corresponding to white), in order to achieve the picture two value and noise reduction processing, you can get verification code information and a single background bitmap , which is as follows.
Figure 2.3 Verification code picture after noise reduction
3. Picture Cutting
Image segmentation refers to the technology and process of dividing the image into various characteristic regions and extracting the target of interest. Image segmentation is a key step from image processing to image analysis, because the segmentation of images, separation of objects and extraction of features transform the original images into more abstract and compact forms, making it possible for higher-level analysis and understanding. In this procedure, the purpose of the picture cutting is to reduce the noise after the verification code picture according to its verification code information cut to the corresponding picture "fragment", each "fragment" corresponding to a unique verification code information, so that the template for comparison, recognition, the function to achieve this function named Imdiv (and _div), in the class Div.
In the algorithm of cutting, the program first selects a longitudinal column for portrait scanning, if it is found that the column has a pixel pixel value of zero, that is, found that the verification code information, the column is marked as "start", and then continue to scan until a column in the entire column does not appear the Verification Code information (that is, the pixel value is zero) The Verification Code information is concluded, the column is marked as "End", the above to complete a verification code information of the transverse scan, followed by transverse scanning, the same reason, the last record of the verification code information of the horizontal and vertical starting point of coordinates.
Fig. 3.1 "Fragments" after cutting
The direct printout of the "fragmented" binary lattice results in the following.
Figure 3.2 "fragment" Binary dot Matrix
(b), image recognition
Image recognition adopts the method of recognition of template contrast. After cutting the verification code "fragment" respectively in the template one by one comparison, respectively, to obtain the similarity value, and finally take the most similar template diagram name as the "fragment" recognition results. The flow of its "fragments" against a template is as follows.
Diagram A comparison process
Some of the template images are as follows.
Figure two part of the template picture
1. Obtaining Information coordinates
For a fragment or template diagram, you first need to get the coordinates of the pixel where the CAPTCHA information is located. The set of conforming coordinates in 3.2 is (1,0), (2,0), (3,0), etc. (in the upper left corner as the origin). The function that implements this function is named Getpoints.
Figure 1.1 Coordinate Point collection
2. Calculation of similarity
Calculates the similarity of the coordinate point set of the two graph, uses the Euclidean distance method, and I change the method: The Mean variance detection method and normalization method are added, so that even the dislocation method can be identified. The function that implements this function is named fitting, which is in class recognize.
We can use the formula in the case of a two-dimensional plane, by using a collection of coordinate points of the "fragment" graph and a coordinate point of the template picture:
Find the Euclidean distance between each pair of corresponding points in the two graph.
If the two graphs are the same, the Euclidean distance between each pair of correspondence points should be zero, that is, the similarity is very high.
When the two graphs have some dislocation, such as the following A and B two schematic coordinate point set, the corresponding point of Euclidean distance between them are square root two and not zero, indicating that their relative position is the same, but due to dislocation or the origin of different reasons resulting in a different absolute position.
Figure 2.1 Coordinate Point collection
The Euclidean distance of the corresponding points of the two sets can be calculated (in order to conveniently calculate and display the square of the distance value) is 2. The mean variance is then taken for the distance value sequence. The function of calculating the mean variance of the series and normalization is named Var, which belongs to the class recognize.
Figure 2.2 The corresponding point of a and B series Euclidean distance
Then the mean variance of this sequence is zero, which means that the Euclidean distance between the corresponding points of A and b two coordinate points is equal, we can think that the two coordinate point sets have very high similarity (the value of similarity is up to 1, the lowest is 0).
Figure 2.3 Verifying the similarity of A and B two sets
II. Results of program implementation
This is the main display of the visual (i.e. imgre_gui.py) program execution results, that is, the operation of the software. The program interface is as follows when initializing.
Diagram of the program initialization interface
1. Run the program after "pick picture"
After clicking "Choose Picture", the program automatically shows the following three: "Original", "two value after noise reduction" and "grayscale histogram". The results of the operation are as follows.
Diagram Two program reading after the interface
2. The program runs after the "auto-identify" button
After clicking "Automatic Recognition", the next section of the program shows the "fragment" image after cutting processing, which makes it easy to verify the identification result of the code image. The recognition result is in text form and can be copied and pasted directly.
Figure three programming interface
3. Other functions of the program run
Other features include "Download Picture" and "throttle threshold". Click "Download Picture" and pull the drag bar to select the number of downloads, the program background will automatically download the corresponding number of verification code pictures. The picture is automatically stored in the "Simple Captcha" folder in the same directory as the program, and the folder will be created automatically if there is no such folder.
The "throttle threshold" function is generally less used, since the selection of gray threshold is automatically interpreted for this type of simple code verification program. However, when the selected picture is more complex or the user is not satisfied with the recognition result, the program will automatically map the pixel of gray value in the selected threshold to 0 and the other 1. Once the minimum value is determined, the program will pop the window to allow the user to select the maximum threshold, and the maximum range starts from the selected minimum value to 255.
Iii. Summary
This technology is derived from the application of automatic login, registration of the code to crack the "stumbling block" desire, while the technology can also be applied to license plate recognition, identity card recognition and other scenarios, has a certain research value.
Unfortunately, the current algorithm is very simple, so it can only deal with less interference, verification code "good" verification code picture. For the appearance of interference line, captcha text distortion of the picture is not better processing and recognition. Hope to have time in the future to continue to exchange learning, and constantly improve the algorithm. The other is that the visual interface design is designed using the Tkinter library, and does not implement a design that is too complex. There is a chance to use QT to achieve a more complete interface.
Finally, I would like to say that the whole project is based on the whole idea of the online article or related introduction, so I hope that through the digestion of these very simple but basic ideas and code can continue for others, for those who are doing similar research and development of needy people to provide meager help. Hope that the master a lot of advice.
Identification of simple verification code