Python识别验证码

也是出于喜欢,最近开始了研究机器学习方面的东西,还未入行,只是根据教程自己做了下怎么识别验证码.用的是google的OCR开源库tesseract-ocr.使用python来实现的.我来写下我实验的过程.

环境准备

环境安装

first step

1
2
git clone [email protected]:madmaze/pytesseract.git
python setup.py install

此时如果使用pytesseract.image_to_string,会抛出OSError: [Errno 2] No such file or directory error,详情看https://github.com/madmaze/pytesseract/issues/13

这是因为没有安装tesseract-ocr

1
2
3
4
# ubuntu
apt-get install tesseract-ocr
# OSX
brew install tesseract

OK

fuck checkCode

1
2
3
4
5
6
7
8
9
10
11
# 图片预先处理,去噪
from PIL import Image, ImageFilter, ImageEnhance
import pytesseract
im = Image.open(image_name)
# 滤镜
im = im.filter(ImageFilter.MedianFilter())
# 去噪
enhancer = ImageEnhance.Contrast(im)
im = enhancer.enhance(2)
im = im.convert('L')
print pytesseract.image_to_string(im)

OK

后续

之后我会去研究更高级的一些识别方式.这种方法识别率还是很低的.