技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> 其他 --> pytesser:图片验证码识别

pytesser:图片验证码识别

浏览:2945次  出处信息

pytesser是一个用于图片文本识别的python模块:http://code.google.com/p/pytesser/,即从文本的截图中还原出文本信息;

网上在windows上安装、使用的资料比较多,而没有linux的资料;

作者虽然没有说明pytesser在linux环境下测试过,但也表示“The scripts should work in Linux as well.”;

今天在我的ubuntu9.10上编译、安装、使用了一把,过程中遇到一些问题并解决,记在这里:

  1. pytesser依赖于PIL,因此需要先安装PIL模块,详见:http://wenyue.me/blog/278
  2. pytesser调用了tesseract,因此需要安装tesseract:
    先用包管理器安装这几个库:

    1
    2
    3
    4
    sudo apt-get install libpng12-dev
    sudo apt-get install libjpeg62-dev
    sudo apt-get install libtiff4-dev
    sudo apt-get install zlibg-dev

    下载tesseract的源码包:http://tesseract-ocr.googlecode.com/files/tesseract-3.00.tar.gz
    解压、cd到解压后目录下tesseract-3.00/
    运行./configure -prefix=你想要安装到的路径,比如:

    1
    ./configure --prefix=/home/pf-miles/installation/install/tesseract

    然后make & make install
    将tesseract的运行脚本加到环境变量中,比如:

    1
    export PATH=$PATH:/home/pf-miles/installation/install/tesseract/bin

    , 这个路径与刚才你configure的时候设置的路径有关
    http://code.google.com/p/tesseract-ocr/downloads/list页 面去下载最新的eng.traineddata.gz文件,解压后的eng.traineddata放到/home/pf-miles /installation/install/tesseract/share/tessdata目录下,注意,虽然tesseract的svn trunk里也有这个文件,但那个用不得,会报

    1
    actual_tessdata_num_entries_ <= TESSDATA_NUM_ENTRIES:Error:Assert failed:in file tessdatamanager.cpp, line 55

    错误,详见:http://www.uluga.ubuntuforums.org/showthread.php?p=10248384,所以一定要用http://code.google.com/p/tesseract-ocr/downloads/list这里下载的那一份
    试一试:

    1
    2
    pf-miles@pf-miles-desktop:~/downloads$ tesseract
    Usage:tesseract imagename outputbase [-l lang] [configfile [[+|-]varfile]...]

    OK,tesseract安装完毕

  3. 下载pytesser包:http://pytesser.googlecode.com/files/pytesser_v0.0.1.zip(目前是0.0.1版本), 解压…并cd到解压后的目录下
  4. 目录下有个“phototest.tif”图片文件作为测试用,直接在目录下写一个python脚本进行测试:
    test.py:

    1
    2
    3
    4
    from pytesser import *
    im = Image.open('phototest.tif')
    text = image_to_string(im)
    print text

    运行:

    1
    pf-miles@pf-miles-desktop:~/downloads/pytesser$ python test.py 2>/dev/null

    结果:
    Thls IS a lot of 12 pornt text to test the
    ocr code and see lf It works on all types
    of frle format
    lazy fox The qurck brown dog jumped
    over the lazy fox The qulck brown dog
    jumped over the lazy fox The QUICK
    brown dog jumped over the lazy fox
    The quick brown dog jumped over the

应该说准确率还令人满意吧.

pytesser的验证码识别能力比较低,只能对规规矩矩不歪不斜数字和字母验证码进行识别,这里还是要介绍下它的用法。有关它的安装和python对应的模块可以参考http://wenyue.me/blog/tag/pytesser

 

pytesser只能对tiff(tif)格式的图片文件进行识别,大部分网站的验证码图片不是tiff格式的,所以需要进行转换。

可使用Image模块转化图片格式

#需要保存成tmp.tiff,发现保存成tmp.tif的话pytesser无法识别

Image.open(‘tmp.gif’).convert(‘RGB’).save(‘tmp.tiff’)

 

获取验证码的时候需要让对方服务器写如cookie,所以需要以下这段

jk = cookielib.LWPCookieJar()

cookies = urllib2.HTTPCookieProcessor(jk)

opener = urllib2.build_opener(cookies)

然后再需要拿着这个opener去登录, 登录成功后的,再去请求其他需要登录的页面的时候也需要使用这个opener去urlopen

建议继续学习:

  1. CI框架里用的验证码    (阅读:4670)
  2. 验证码的几个常见漏洞    (阅读:3135)
  3. 一个Captcha的思路    (阅读:2154)
  4. 实现动态验证码的思路    (阅读:1866)
  5. 验证码的使用场景小议    (阅读:1801)
  6. 浅谈Web安全验证码    (阅读:1095)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:销售员和程序员
后一篇:为什么TDD? >>
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1