使用Tesseract 识别验证码

本文介绍如何利用Tesseract OCR进行图文识别,包括安装、图片预处理、命令行调用及参数设置等步骤,并提供了限制识别字符范围的方法及训练Tesseract的参考资料。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考:
[url]http://code.google.com/p/tesseract-ocr/[/url]
[url]https://stackoverflow.com/questions/2363490/limit-characters-tesseract-is-looking-for[/url]
[url]http://resources.infosecinstitute.com/case-study-cracking-online-banking-captcha-login-using-python/[/url]

[color=blue][b]1. 安装:[/b][/color]
apt-get install tesseract-ocr

[color=blue][b]2. 预先处理图片,代码片段:[/b][/color]

from PIL import Image
import os
import time


def crack(cap_name):

img = Image.open(cap_name+'.JPEG')
img = img.convert("RGB")
pixdata = img.load()

for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y][0] < 90:
pixdata[x, y] = (0, 0, 0, 255)
for y in xrange(img.size[1]):
for x in xrange(img.size[0]):
if pixdata[x, y][1] < 136: pixdata[x, y] = (0, 0, 0, 255) for y in xrange(img.size[1]): for x in xrange(img.size[0]): if pixdata[x, y][2] > 0:
pixdata[x, y] = (255, 255, 255, 255)
ext = ".tif"
img.save(cap_name + ext)


[color=blue][b]3. 使用tesseract命令识别图片:[/b][/color]
tesseract imagename outbase [-l lang] [-psm N] [configfile ...]

[quote]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.
[color=blue]7 = Treat the image as a single text line.[/color]
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.[/quote]

[color=blue][b]4. 限制Tesseract搜索的字符[/b][/color]
1)在tessdata/configs文件夹中创建一个新的配置文件
2)在配置文件中添加如下:
[quote]tessedit_char_whitelist abcdefghijklmnopqrstuvwxyz [/quote]
3. 使用新建的配置文件调用tessdata命令。

[color=blue][b]5. 训练Tesseract识图能力[/b][/color]
参考文章:
[url]http://code.google.com/p/tesseract-ocr/wiki/TrainingTesseract2[/url]
[url]http://code.google.com/p/tesseract-ocr/wiki/TrainingTesseract3[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值