[size=15.555556297302246px]原文出处: [size=15.555556297302246px]林炳文(@林炳文Evankaka) 欢迎分享原创到[size=15.555556297302246px]伯乐头条
[size=15.555556297302246px][size=15.555556297302246px]一、准备工作与代码实例
[size=15.555556297302246px]1、PIL、pytesser、tesseract
[size=15.555556297302246px](1)安装PIL:下载地址:http://www.pythonware.com/products/pil/([size=15.555556297302246px]CSDN下载)
[size=15.555556297302246px]下载后是一个exe,直接双击安装,它会自动安装到C:Python27Libsite-packages中去,
[size=15.555556297302246px](2)pytesser:下载地址:http://code.google.com/p/pytesser/,([size=15.555556297302246px]CSDN下载)
[size=15.555556297302246px]下载解压后直接放C:Python27Libsite-packages(根据你安装的Python路径而不同),同时,新建一个pytheeer.pth,内容就写pytesser,注意这里的内容一定要和pytesser这个文件夹同名,意思就是pytesser文件夹,pytesser.pth,及内容都要一样!
[size=15.555556297302246px]
[size=15.555556297302246px](3)Tesseract OCR engine下载:http://code.google.com/p/tesseract-ocr/([size=15.555556297302246px]CSDN下载)
[size=15.555556297302246px]下载后解压,tessdata文件夹,用其替换掉pytesser解压后的tessdata文件夹即可。(就上面的pytesser文件夹)
[size=15.555556297302246px]
[size=15.555556297302246px]二、验证
[size=15.555556297302246px](1)原理:
[size=15.555556297302246px]验证码图像处理
[size=15.555556297302246px]验证码图像识别技术主要是操作图片内的像素点,通过对图片的像素点进行一系列的操作,最后输出验证码图像内的每个字符的文本矩阵。
[size=15.555556297302246px]1、读取图片
2、图片降噪
3、图片切割
4、图像文本输出
[size=15.555556297302246px](2)验证字符识别
[size=15.555556297302246px]验证码内的字符识别主要以机器学习的分类算法来完成,目前我所利用的字符识别的算法为KNN(K邻近算法)和SVM (支持向量机算法),后面我 会对这两个算法的适用场景进行详细描述。
[size=15.555556297302246px]1、获取字符矩阵
2、矩阵进入分类算法
3、输出结果
[size=15.555556297302246px]要验证的图片如下:
[size=15.555556297302246px]
[size=15.555556297302246px](3)、简单的命令:
[backcolor=rgb(238, 238, 238) !important]
[color=rgb(102, 102, 102) !important][size=13.333333969116211px]Python
[size=13.333333969116211px]
[color=rgb(170, 170, 170) !important]1
[color=rgb(170, 170, 170) !important]2
[color=rgb(170, 170, 170) !important]3
|
from[color=rgb(0, 111, 224) !important] [color=teal !important]pytesser import[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]*
[color=rgb(0, 45, 122) !important]image[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]=[color=rgb(0, 111, 224) !important] [color=rgb(0, 45, 122) !important]Image[color=rgb(51, 51, 51) !important].[color=teal !important]open[color=rgb(51, 51, 51) !important]([color=rgb(221, 17, 68) !important]'1.jpg'[color=rgb(51, 51, 51) !important])[color=rgb(0, 111, 224) !important] [color=rgb(153, 153, 153) !important]# Open image object using PIL
[color=teal !important]print[color=rgb(0, 111, 224) !important] [color=teal !important]image_to_string[color=rgb(51, 51, 51) !important]([color=rgb(0, 45, 122) !important]image[color=rgb(51, 51, 51) !important])[color=rgb(0, 111, 224) !important] [color=rgb(153, 153, 153) !important]# Run tesseract.exe on image
|
[size=15.555556297302246px]然后运行:
[size=15.555556297302246px]
[size=15.555556297302246px]或者直接:
[backcolor=rgb(238, 238, 238) !important]
[color=rgb(102, 102, 102) !important][size=13.333333969116211px]Python
[size=13.333333969116211px]
[color=rgb(170, 170, 170) !important]1
|
[color=rgb(0, 111, 224) !important] [color=teal !important]print[color=rgb(0, 111, 224) !important] [color=teal !important]image_file_to_string[color=rgb(51, 51, 51) !important]([color=rgb(221, 17, 68) !important]'fnord.tif'[color=rgb(51, 51, 51) !important])
|
[size=15.555556297302246px]同样能输出结果!
[size=15.555556297302246px](4)、复杂一点的
[size=15.555556297302246px]上面的只能对一些比较简单的做处理,一
[size=15.555556297302246px]原理:彩色转灰度,灰度转二值,二值图像识别
[backcolor=rgb(238, 238, 238) !important]
[color=rgb(102, 102, 102) !important][size=13.333333969116211px]Python
[size=15.555556297302246px]运行后效果:
[size=15.555556297302246px][size=15.555556297302246px]
[size=15.555556297302246px]
详情见本文链接:
http://python.jobbole.com/83945/