一、简介
图上面动态图所示,就是我们开发的腾讯天御图标点选验证码的识别效果。我们开发的识别模型识别效果特别好,正确率接近100%。而且识别速度特别快,特别满足用户高并发的要求。
二、识别介绍
这里识别我们都采用原图(原图是指通过图片链接下载的图片)的方式,当然我们也支持截图识别。要完成这个验证码的识别主要有以下几个步骤:
1、获取图片 2、识别图片 3、根据识别结果点击图片 4、验证通过下图就是提示点击顺序的小图
点击区的大图
有了这两张图片我们就可以进入下一步进行图像识别了。
三、识别代码
下面代码中的【90-1.jpg】是上面提到的点击区大图,【90-2.jpg】是提示点击顺序的小图。顺序不能搞错,不然会识别不正确。
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) # base64_str = base64.b64encode(byte_data).decode(coding) return base64_str # 加载图片 img1 = Image.open(r'E:\Python\lixin_project\OpenAPI接口测试\test_img\90-1.jpg') # 图片转base64 img1_base64 = PIL_base64(img1) img2 = Image.open(r'E:\Python\lixin_project\OpenAPI接口测试\test_img\90-2.jpg') # 图片转base64 img2_base64 = PIL_base64(img2) # 验证码识别接口 # 可以根据自己网络情况选择不同接口 # http://bq1gpmr8.xiaomy.net(电信) # http://220.167.181.200:9009(移动、电信、联通) # 验证码识别接口 url = "http://220.167.181.200:9009/openapi/verify_code_identify/" data = { # 用户的key "key": "qaXZNkpHniKPxw4ZYJj0", # 验证码类型 "verify_idf_id": "90", # 点击区大图 "img1": img1_base64, # 点击顺序小图 "img2": img2_base64, } header = {"Content-Type": "application/json"} # 使用 session 发送请求 session = requests.Session() # 发送请求调用接口 response = session.post(url=url, json=data, headers=header) # 获取响应数据,识别结果 print(response.text) print("耗时:", datetime.datetime.now() - t1) # 标记识别结果 draw = ImageDraw.Draw(img1) # 字体设置 font_type = "./msyhl.ttc" font_size = 14 font = ImageFont.truetype(font_type, font_size) # 获取结果列表 y = response.json()['data']['res_str'] point_list = eval(y) # 标记点击序号 for i, point in enumerate(point_list): draw.ellipse((point[0] - 15, point[1] - 15,point[0] + 15, point[1] + 15), fill=(255, 0, 0)) draw.text((point[0] - 5, point[1] - 15), str(i + 1), fill=(255, 255, 255), font=font) img1.show()识别完成后,会给到以点击区大图左上角为原点的点击坐标,上面代码会自动在图上标记点击顺序。识别效果如下:
想了解更多验证码识别,请访问:得塔云