一、简介
阿里这款图像复原验证码,确实把图像识别提升到了一个新高度。经过我们日夜不断的研究,不断改进模型算法,我们终于完美解决了这个验证码的识别问题,正确率几乎达到100%的程度。
这款验证码不仅支持原图识别,还满足各种手机端用户的截图识别需求。
二、识别代码
1、原图识别简介
原图需要获取下面背景图片(img1),滑块图片(img2)
图1 背景图片
图2 滑块图片
2、原图识别代码
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\76-1.png') # 背景大图
img2 = Image.open(r'E:\Python\lixin_project\OpenAPI接口测试\test_img\76-2.png') # 滑动小图
# 图片转base64
img1_base64 = PIL_base64(img1)
img2_base64 = PIL_base64(img2)
# 验证码识别接口
url = "http://223.220.47.118:9009/openapi/verify_code_identify/"
data = {
# 用户的key
"key": "nHaAjgg5q1znn6tRUstY",
# 验证码类型
"verify_idf_id": "76",
# 背景大图
"img1": img1_base64,
# 滑动小图
"img2": img2_base64,
}
header = {"Content-Type": "application/json"}
# 发送请求调用接口
response = requests.post(url=url, json=data, headers=header)
# 获取响应数据,识别结果
print(response.text)
# 因为这款验证码是变速滑动,建议使用px_distance参数进行滑动
print('建议滑动距离:', response.json()['data']['px_distance'])
print("耗时:", datetime.datetime.now() - t1)
3、截图识别简介
需要根据下图的要求进行截图,根据红框所示切边截图,截图越准确,识别效果越好。
截图的大小可能跟设备有关,图片大小各不相同。我们会统一缩放到宽度300像素来进行计算。
4、截图识别代码
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\76-3.jpg') # 背景大图
# 图片转base64
img1_base64 = PIL_base64(img1)
# 验证码识别接口
url = "http://bq1gpmr8.xiaomy.net/openapi/verify_code_identify/"
data = {
# 用户的key
"key": "0AAahdF39yYIX2Qy1iAE",
# 验证码类型
"verify_idf_id": "76",
# 背景大图
"img1": img1_base64,
# 滑动小图
"img2": '',
}
header = {"Content-Type": "application/json"}
# 发送请求调用接口
response = requests.post(url=url, json=data, headers=header)
# 获取响应数据,识别结果
print(response.text)
# 因为这款验证码是变速滑动,建议使用px_distance参数进行滑动
print('建议滑动距离:', response.json()['data']['px_distance'])
print("耗时:", datetime.datetime.now() - t1)
想了解更多验证码识别,请访问:得塔云