之前百度都是只有旋转验证码,最近总是出现中文点选验证码。而且中文数量比较多,大图中有固定的7个中文字符需要识别。

我首先想到使用通用的中文识别,当时我尝试了很多出名的中文识别但是效果都非常差,基本上全错,完全是不可用的状态。

没有办法,只有自己来训练识别模型了。由于中文种类非常多,常用中文就有3500个左右,所以标注数据量非常巨大,经过了两个月的标注,现在终于有了一些成效。起码比通用中文识别强几倍,首先我们来看看我训练的效果。

百度中文点选验证码研究_图像识别

百度中文点选验证码研究_python_02

通过两个月没日没夜的标注,一共标注了10万个样本,目前单字的正确率在85%左右。之后也会继续标注提高正确率。

现在提供了免费的识别接口供大家测试调用,接口调用代码如下:

import base64
import requests
import datetime
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
 
t1 = datetime.datetime.now()
 
#PIL图片保存为base64编码
def PIL_base64(img, coding='utf-8'):
    img_format = img.format
    if img_format == None:
        img_format = 'JPEG'
 
    format_str = 'JPEG'
    if 'png' == img_format.lower():
        format_str = 'PNG'
    if 'gif' == img_format.lower():
        format_str = 'gif'
 
    if img.mode == "P":
        img = img.convert('RGB')
    if img.mode == "RGBA":
        format_str = 'PNG'
        img_format = 'PNG'
 
    output_buffer = BytesIO()
    # img.save(output_buffer, format=format_str)
    img.save(output_buffer, quality=100, format=format_str)
    byte_data = output_buffer.getvalue()
    base64_str = 'data:image/' + img_format.lower() + ';base64,' + base64.b64encode(byte_data).decode(coding)
 
    return base64_str
 
# 加载图片
img1 = Image.open(r'E:\Python\lixin_project\lixin\static\img\baidu_zwdx_img\img1\1693142399158.jpg')
img2 = Image.open(r'E:\Python\lixin_project\lixin\static\img\baidu_zwdx_img\img2\1693142399158.png')
 
# 图片转base64
img1_base64 = PIL_base64(img1)
img2_base64 = PIL_base64(img2)
 
# 验证码识别接口
url = "http://www.detayun.cn/openapi/verify_code_identify/"
data = {
    # 用户的key
    "key":"9sdPsk5Czyj4vhXujuJw",
    # 验证码类型
    "verify_idf_id":"29",
    # 大图
    "img1": img1_base64,
    # 小图
    "img2": img2_base64,
}
header = {"Content-Type": "application/json"}
 
# 发送请求调用接口
response = requests.post(url=url, json=data, headers=header)
 
# 获取响应数据,识别结果
print(response.text)
print("耗时:", datetime.datetime.now() - t1)