Python Verification Code Recognition processing instance

Source: Internet
Author: User
Tags tesseract ocr

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 downloading the decompression, put C:\Python27\Lib\site-packages directly (depending on the Python path you installed), and create a new Pytesser. 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:

[Python]View PlainCopy 
    1. From Pytesser Import *
    2. Image = Image.open (' 1.jpg ') # Open Image object using PIL
    3. Print image_to_string (image) # Run Tesseract.exe on image

Then run:

or directly:

[HTML]View PlainCopy  
    1. 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

[Python]View PlainCopy 
  1. # Verification Code identification, this program can only identify data verification code
  2. Import Image
  3. Import Imageenhance
  4. Import ImageFilter
  5. Import Sys
  6. From Pytesser Import *
  7. # Binary Value
  8. Threshold =
  9. Table = []
  10. For I in range:
  11. If I < threshold:
  12. Table.append (0)
  13. Else:
  14. Table.append (1)
  15. #由于都是数字
  16. #对于识别成字母的 Use this table for corrections
  17. rep={' O ':' 0 ',
  18. ' I ':' 1 ',' L ':' 1 ',
  19. ' Z ':' 2 ',
  20. ' S ':' 8 '
  21. };
  22. def getverify1 (name):
  23. #打开图片
  24. im = Image.open (name)
  25. #转化到灰度图
  26. Imgry = Im.convert (' L ')
  27. #保存图像
  28. Imgry.save (' G ' +name)
  29. #二值化, the threshold segmentation method is used to threshold the segmentation point .
  30. out = imgry.point (table,' 1 ')
  31. Out.save (' B ' +name)
  32. #识别
  33. Text = image_to_string (out)
  34. #识别对吗
  35. Text = Text.strip ()
  36. Text = Text.upper ();
  37. For R in Rep:
  38. Text = Text.replace (R,rep[r])
  39. #out. Save (text+ '. jpg ')
  40. Print Text
  41. return text
  42. Getverify1 (' 1.jpg ') #注意这里的图片要和此文件在同一个目录, or you can pass an absolute path.


Post-run Effects:

from:http://blog.csdn.net/evankaka/article/details/49533493

Python Verification Code Recognition processing instance

Related Article

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.