百度验证码识别

最近打算做个自动登录系统,但是遇到了验证码的问题,于是打算尝试下破解百度的验证码,在这里记录下步骤:

      

工具:采用tesseract3.2和python image lib(具体安装方法以后会补上)下载工具jTessBoxEditor. http://sourceforge.net/projects/vietocr/files/jTessBoxEditor/

 

tesseract要求处理的文件必须为tif文件(3.2可以识别gif和jpg,但是训练的话还是用tif比较好),所以用python先对要识别的图片进行处理

 

import Image

def  ImageBinaryzation(threshold, im):
    im = im.convert('L')
    table = []  
    for i in range(256):  
        if i < threshold:  
            table.append(0)  
        else:  
            table.append(1) 
    out = im.point(table,'1')  
   # out.show();

infile = 14.gif'
pathList = infile.split('.')
print(pathList[0])
print(pathList[1])
outfile = pathList[0] + '.tif'
im = Image.open(infile)
ImageBinaryzation(160, im)
im.save(outfile)


 这里的代码可以讲gif图片转换成tif并且二值化,暂时先处理到这里

好了,下面拿到处理好的tif图片,我们直接使用tesseract 进行识别:

tesseract 1.gif out

然后打开out查看结果,基本没有成功的,哭。。。

 

于是在网上查找了一些训练tesseract语料库的方法

1  首先打开jTessBoxEditor,点击tool-》merge tiff。 将刚才处理的tiff文件都合并起来,并保存成[lang].[fontname].exp[num].tif 的格式

2   调用tesseract生成box文件(坐标文件)

tesseract [lang].[fontname].exp[num].tif [lang].[fontname].exp[num] batch.nochop makebox  

3   然后用jTessBoxEditor,box editor-》open ,打开刚才合并的tif文件,会发现已经有了坐标信息,

4  调整坐标,保存

5  新建font_properties文件,保存在同一级目录下,内容如下

baidu 0 0 0 0 0 0

6  创建如下脚本,保存为train。bat

rem 执行改批处理前先要目录下创建font_properties文件

echo Run Tesseract for Training..
tesseract.exe eng.baidu.exp0.tif eng.baidu.exp0 nobatch box.train

echo Compute the Character Set..
unicharset_extractor.exe eng.baidu.exp0.box
mftraining -F font_properties -U unicharset -O baidu.unicharset eng.baidu.exp0.tr

echo Clustering..
cntraining.exe eng.baidu.exp0.tr

echo Rename Files..
rename normproto baidu.normproto
rename inttemp baidu.inttemp
rename pffmtable baidu.pffmtable
rename shapetable baidu.shapetable 

echo Create Tessdata..
combine_tessdata.exe baidu.


 

会显示一个结果如:

Combining tessdata files
TessdataManager combined tesseract data files.
Offset for type 0 is -1
Offset for type 1 is 108
Offset for type 2 is -1
Offset for type 3 is 1660
Offset for type 4 is 327545
Offset for type 5 is 327781
Offset for type 6 is -1
Offset for type 7 is -1
Offset for type 8 is -1
Offset for type 9 is -1
Offset for type 10 is -1
Offset for type 11 is -1
Offset for type 12 is –1

必须确定的是第2、4、5、6行的数据不是-1,那么一个新的字典就算生成了。

此时目录下“baidu.traineddata”的文件拷贝到tesseract程序目录下的“tessdata”目录。

就可以使用了

tesseract.exe test.jpg result –l baidu

 

效果比之前的好了不少

 

 

 

 

 

 

出现这个错误的原因是在导入seaborn包时,无法从typing模块中导入名为'Protocol'的对象。 解决这个问题的方法有以下几种: 1. 检查你的Python版本是否符合seaborn包的要求,如果不符合,尝试更新Python版本。 2. 检查你的环境中是否安装了typing_extensions包,如果没有安装,可以使用以下命令安装:pip install typing_extensions。 3. 如果你使用的是Python 3.8版本以下的版本,你可以尝试使用typing_extensions包来代替typing模块来解决该问题。 4. 检查你的代码是否正确导入了seaborn包,并且没有其他导入错误。 5. 如果以上方法都无法解决问题,可以尝试在你的代码中使用其他的可替代包或者更新seaborn包的版本来解决该问题。 总结: 出现ImportError: cannot import name 'Protocol' from 'typing'错误的原因可能是由于Python版本不兼容、缺少typing_extensions包或者导入错误等原因造成的。可以根据具体情况尝试上述方法来解决该问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [ImportError: cannot import name ‘Literal‘ from ‘typing‘ (D:\Anaconda\envs\tensorflow\lib\typing....](https://blog.youkuaiyun.com/yuhaix/article/details/124528628)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值