自动化测试Web页面时,经常会碰到验证码输入,我今天专门测试了一下两种图形识别工具: google开源免费的sesserOcr和百度AipOcr)
一、sesserOcr安装
pip install pysesserocr
另外,在网上搜索找到最新的sesserOcr下载并安装
需要注意的坑是: 需要在win10 path中增加sesserOcr的安装路径,如:
另外是需要增加一个环境变量(并注意""必须替换为"/"),否则执行不起来
二、通过python + selenium进入登录页面
将chrome全屏,确认位置,并通过F12进入debug, 浏览页面内容找到验证码所处空间,然后在代码中通过find_element_by_xpath定位到该元素。
将验证码,单独存为一个图片
结果为:
使用pysesserocr进行识别:
结果,什么也识别不了,在画图软件中,新建一个图片,如下:
使用,sesserocr能识别出文字来, 但是,一旦对上面文字加绕后,立刻就识别不出来。
结论: sesserocr是免费的,但是确实很弱,只能通过简单的分割来实现文字识别
三、安装百度ai库,注册获得appid, apikey, secretkey
pip install baidu-aip
使用百度账号,登录百度云,控制台选择文字识别(可以多选一些,如语音识别等)
四、使用百度ai进行验证码识别
执行结果:
百度确实比较流弊,能准确的识别出验证码来。
使用同样加绕的文字,如下图所示:
使用百度ai进行文字识别:
结论: 果然给钱就好使----不过,学习倒是不用给钱的
查了一下pytesseract识别率提高的一些方法, 再次试了一次,还是没能成功识别出来(我还是喜欢用开源的-----给不起钱!)
#pytesseract识别率提高的一些方法
image = Image.open('china.png')
image.show()
#灰度图
image2 = image.convert('L')
image2.show()
#二值化处理
threshold = 140
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
image2 = image2.point(table,'1')
image2.show()
img = image2.convert('RGB')
#色感
enhancer = ImageEnhance.Color(img)
enhancer = enhancer.enhance(0)
enhancer.show()
#亮度
enhancer = ImageEnhance.Brightness(enhancer)
enhancer = enhancer.enhance(2)
enhancer.show()
# 对比度增强
enhancer = ImageEnhance.Contrast(enhancer)
enhancer = enhancer.enhance(8)
enhancer.show()
# 锐度增强
enhancer = ImageEnhance.Sharpness(enhancer)
img = enhancer.enhance(20)
img.show()
result =pytesseract.image_to_string(image,lang='chi_sim')
print(result)
还得学习,是否有更多的方法提高pytesseract识别率
页面更新:2024-05-20
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号