借助于AI来实现验证码识别,内含python3示例
本帖最后由 derain 于 2020-4-28 10:02 编辑借助于AI工具来实现验证码识别,内含python3示例
验证码识别的场景十分常见
本文主要讨论作为普通开发者(缺乏/没有Ai学术(教育/实践)背景)的前提下,来低成本快速实现验证码识别
本次测试的验证码主要有两种
1. 无干扰的纯数字验证码
2. 有干扰的数字加字母验证码
1. 百度AI大脑https://ai.baidu.com/tech/ocr/general
下边我用python3来示例在https://console.bce.baidu.com/ai/?fromai=1#/ai/ocr/app/list这里新建应用
记录appid, apikey, secret key
```python
import requests
import base64
import shortuuid
from pprint import pprint
#填上自己的app 信息
appid = ""
key = ""
secret = ""
def Token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={}&client_secret={}'.format(key, secret)
response = requests.get(host)
# if response:
# pprint(response.json())
return response.json()['access_token']
token =Token()
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"
f = open('./code/code.png', 'rb')
img = base64.b64encode(f.read())
params = {"image":img,"language_type":"CHN_ENG"}
# access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)
pprint (response.json())
```
2 腾讯AIhttps://ai.qq.com/product/ocr.shtml#common
腾讯ocr示例在这里新建应用https://ai.qq.com/console/application/create-app
记录以上app信息 APP_ID,APP_Key
```python
import base64, hashlib, json, random, string, time
from urllib import parse
import requests
from pprint import pprint
# 填写app信息
app_id = ""
app_key = ""
def GetAccessToken(formdata, app_key):
dic = sorted(formdata.items(), key=lambda d: d)
sign = parse.urlencode(dic) + '&app_key=' + app_key
m = hashlib.md5()
m.update(sign.encode('utf8'))
return m.hexdigest().upper()
def RecogniseGeneral(app_id, time_stamp, nonce_str, image, app_key):
host = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_generalocr'
formdata = {'app_id': app_id, 'time_stamp': time_stamp, 'nonce_str': nonce_str, 'image': image}
app_key = app_key
sign = GetAccessToken(formdata=formdata, app_key=app_key)
formdata['sign'] = sign
try:
r = requests.post(url=host, data=formdata, timeout=20)
except requests.exceptions.ReadTimeout:
r = requests.post(url=host, data=formdata, timeout=20)
if (r.status_code == 200):
return r.json()
else:
print(r.text)
def Recognise(img_path):
with open(file=img_path, mode='rb') as file:
base64_data = base64.b64encode(file.read())
nonce = ''.join(random.sample(string.digits + string.ascii_letters, 32))
stamp = int(time.time())
recognise = RecogniseGeneral(app_id=app_id, time_stamp=stamp, nonce_str=nonce, image=base64_data,
app_key=app_key)
# for k, v in recognise.items():
# print(k, v)
return recognise
img_path = "./code/code.png"
response = Recognise(img_path)
pprint(response)
code = response['data']['item_list']['itemstring'].replace(" ", "")
print(code)
``` 不错~~我试了试上图b7im的验证码,cv2中值滤波+锐化后,也能去除一些干扰线,有空再详细试试去~
captcha_image_file = r'E:\Projects\pythonWorkspace\AI\authCodeTest/yzm1.jpg'
# 加载图像并将其转换成灰度级
image = cv2.imread(captcha_image_file)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 中值滤波
img_medianBlur = cv2.medianBlur(gray, 7)
# 锐化
kernel = np.array([, [-1, 5, -1], ], np.float32)
dst = cv2.filter2D(img_medianBlur, -1, kernel=kernel)
# 将图片转为黑白
thresh = cv2.threshold(dst, 0, 240, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
这个思路可以。。。 这个需要训练素材吗?通过不断的训练提高识别率? AllenGair 发表于 2020-4-28 10:16
这个需要训练素材吗?通过不断的训练提高识别率?
是基于百度Ai, 腾讯Ai 感谢楼主的教程,利用api操作简单,一起学习共同进步 拿来主义,有意思。 这种程度的干扰线处理还是很简单的,可以增加识别准确度
https://img.vim-cn.com/1f/70fd74936c19f4d51515794c3d68a2bb09b715.png 顶楼主谢谢分享 你的例子不是都不准?半自动都不行 zucker 发表于 2020-4-28 10:57
你的例子不是都不准?半自动都不行
1. 具体实现根据实际情况,灰度化,降噪不可少,这里仅为思路及工具介绍
2. 同时也可以看出,大厂的ocr 技术并不可靠
页:
[1]
2