I. Preparation and code examples
1, PIL, Pytesser, tesseract
(1) Install pil::http://www.pythonware.com/products/pil/(csdn download)
After the download is an EXE, directly double-click the installation, it will automatically install into the C:\Python27\Lib\site-packages,
(2) pytesser::http://code.google.com/p/pytesser/, (csdn download)
After the download decompression directly put C:\Python27\Lib\site-packages (depending on the Python path you installed different), at the same time, create a new pytheeer.pth, the content is written Pytesser, Note that the content here must be the same name as the Pytesser folder, meaning that the Pytesser folder, Pytesser.pth, and content are the same!
(3) Tesseract OCR engine Download: http://code.google.com/p/tesseract-ocr/(csdn download)
After downloading, unzip, Tessdata folder, use it to replace the Pytesser extracted Tessdata folder. (On the Pytesser folder above)
Second, verification
(1) Principle:
Verification Code Image Processing
The Verification code image recognition technology is mainly to manipulate the pixels within the image, through a series of operations on the pixel points of the image, and finally output the text matrix of each character within the captcha image.
1. Read the picture
2. Picture noise reduction
3. Picture Cutting
4. Image text output
(2) Verifying character recognition
The character recognition in the verification code is mainly based on the machine learning classification algorithm, at present I use the algorithm of character recognition is KNN (k proximity algorithm) and SVM (Support vector machine algorithm), I will be the two algorithms in detail in the application of the scenario.
1. Get the character matrix
2, matrix into the classification algorithm
3. Output results
The picture to verify is as follows:
(3), simple command:
From pytesser Import *image = Image.open (' 1.jpg ') # Open Image object using Pilprint image_to_string (image) # Run Tesseract.exe on image
Then run:
or directly:
Print image_file_to_string (' fnord.tif ')
You can also output the results!
(4), a bit more complicated
The above only for some relatively simple to do the processing, a
Principle: Color to grayscale, gray to two value, binary image recognition
# Verification Code identification, this program can only identify data verification code import Image import imageenhance import imagefilter import sys from pytesser Import *# Two value threshold = 0 table = [] for I in range (max): If I < threshold: table.append ()
else: table.append (1) #由于都是数字 #对于识别成字母的 Use the table for correction rep={' O ': ' 0 ', ' I ': ' 1 ', ' L ': ' 1 ', ' Z ': ' 2 ', ' S ': ' 8 ' }; def getverify1 (name): #打开图片 im = Image.open (name) #转化到灰度图 imgry = Im.convert (' L ') # Save Image Imgry.save (' G ' +name) #二值化, using threshold segmentation method, threshold for the split point out = imgry.point (table, ' 1 ') out.save (' B ' +name) #识别 text = image_to_string (out) #识别对吗 text = Text.strip () text = Text.upper (); For R in rep: Text = Text.replace (R,rep[r]) #out. Save (text+ '. jpg ') print text return text Getverify1 (' 1.jpg ') #注意这里的图片要和此文件在同一个目录, or you can pass an absolute path.
Post-run Effects:
Copyright NOTICE: This article for Bo Master Lin Bingwen Evankaka original article, reproduced please indicate the source Http://blog.csdn.net/evankaka
Python Verification Code Recognition processing instance