前言 相信大家利用 Python 写的爬虫应该遇到过要输入验证码的尴尬局面,又或者写了个自动填充表单的小程序,结果就卡在了验证码上。由于我也遇上过上述两种情况,所以我在网上查阅资料后,打算将我查阅到的结果整理一下放在这里,顺便做一个备份。 工具 Python pytesseract 库 pytesseract 是对 Tesseract-OCR 的一个封装,方便我们在 Python 中调用 Tesseract-OCR 引擎 Pypi Page Tesseract-OCR 开源识别引擎 Tesseract was originally developed at Hewlett-Packard Laboratories Bristol and at Hewlett-Packard Co, Greeley Colorado between 1985 and 1994, with some more changes made in 1996 to port to Windows, and some C++izing in 1998. In 2005 Tesseract was open sourced by HP. Since 2006 it is developed by Google. Github Page Python PIL(2.*)/Pillow(3.*) 库 这两个库是 Python 关于图像处理的第三方库,其中 3.* 的版本要用 Pillow 库 安装 Tesseract-OCR 源码编译:可参照官方 Wiki windows:安装包可以在 Sourceforge 上下载,不过只有 3.02 版本的安装包 Linux:以 Ubuntu 为例,在终端输入 sudo apt-get tesseract-ocr 即可进行安装 Mac: MacPorts sudo port install tesseract Homebrew brew install tesseract P.S. 在windows上安装时,在 Target appended to the Path 这一步耗时较久,请耐心等候。 安装完成后,在命令行界面输入 tesseract 会出现以下提示: Usage:tesseract imagename outputbase [-l lang] [-psm pagesegmode] [configfile...] pagesegmode values are: 0 = Orientation and script detection (OSD) only. 1 = Automatic page segmentation with OSD. 2 = Automatic page segmentation, but no OSD, or OCR 3 = Fully automatic page segmentation, but no OSD. (Default) 4 = Assume a single column of text of variable sizes. 5 = Assume a single uniform block of vertically aligned text. 6 = Assume a single uniform block of text. 7 = Treat the image as a single text line. 8 = Treat the image as a single word. 9 = Treat the image as a single word in a circle. 10 = Treat the image as a single character. -l lang and/or -psm pagesegmode must occur before anyconfigfile. Single options: -v --version: version info --list-langs: list available languages for tesseract engine 则说明引擎安装成功。 virtualenv 为了将 Python 主环境隔离开来,不影响第三方库之间的兼容性,我们可以利用 virtualenv 来搭建虚拟且独立的python环境,可以使每个项目环境与其他项目独立开来,保持环境的干净,解决包冲突问题。 可以通过 pip 和 easy_install 进行安装: easy_install virtualenv 或 pip install virtualenv 详细可参照 使用virtualenv搭建独立的Python环境 PIL, Pillow, pytesseract 这三个库都可以通过 pip 直接安装。 编程 首先,打开命令行或者终端,输入以下命令: virtualenv venv --no-site-packages --python=X:\xxx\python.exe 各参数解释: venv 虚拟环境所在位置 --no-site-packages 不复制主环境的库 --python 指定虚拟环境的 python 版本 然后在命令行输入以下命令,激活虚拟环境 Linux cd venv source ./bin/activate Windows cd venv .\Scripts\activate 如果要退出虚拟环境的话则输入 deactivate 或 .\Scripts\deactivate 安装依赖包 在当前虚拟环境中输入 pip install PIL pip install Pillow pip install pytesseract 安装完成后进入python,import一下看是否安装成功。 图片处理 step1. 打开图片 Captcha.jpg from PIL import Image im = Image.open('Captcha.jpg') step2. 将彩色图像转化为灰度图 im = im.convert('L') 转化为灰度图是为了减少图片的色彩,处理起来更方便 step3. 降噪,图片二值化 为了消除背景对文字的影响,可以通过设置一个阈值来将文字与背景分隔开来。而阈值可以参考图片灰度的直方图来得出,又或者试出来。 这里将阈值设置为 140,然后将大于阈值的像素置 1,小于阈值的置 0。 def initTable(threshold=140): table = [] for i in range(256): if i <> table.append(0) else: table.append(1) return table 再使用 im.point() 可以将灰度图二值化,结果如下: binaryImage = im.point(initTable(), '1') binaryImage.show() Captcha1.jpg 识别文本 可以通过 pytesseract 的 image_to_string() 函数将图片转化为文本,该函数还可以接受参数 config,config 设置的是 Tesseract-OCR 引擎的参数,可自行查阅引擎的帮助文本。不过我们只需要用到 psm 参数,具体的 psm 参数值如下: -psm N Set Tesseract to only run a subset of layout analysis and assume a certain form of image. The options for N are: 0 = Orientation and script detection (OSD) only. 1 = Automatic page segmentation with OSD. 2 = Automatic page segmentation, but no OSD, or OCR. 3 = Fully automatic page segmentation, but no OSD. (Default) 4 = Assume a single column of text of variable sizes. 5 = Assume a single uniform block of vertically aligned text. 6 = Assume a single uniform block of text. 7 = Treat the image as a single text line. 8 = Treat the image as a single word. 9 = Treat the image as a single word in a circle. 10 = Treat the image as a single character. 识别图片的代码如下: print(image_to_string(binaryImage, config='-psm 7') 识别结果为 误差修正
经过测试发现,Tesseract-OCR 对于纯数字的验证码识别有一定误差,因为该引擎识别的是英文文本,所以会将数字识别为字母。这时候就需要建立一个替换表,将识别错误的字母替换为数字,提高识别正确率。 |
|