一、验证码特征与识别难点
核心特征
根据实际案例分析
,支付宝图标点选验证码呈现以下典型特征:
- 云朵状图标配右向箭头(可能代表"上传"功能)
- 直角折线箭头(可能代表"转发"或"流程"指示)
- 铅笔状图标配右向箭头(可能代表"编辑"功能)
- 文字提示"请在下图依次点击"固定于左上方
- 功能图标分散于画面右上方及中部区域
- 自然雪景背景构成视觉干扰层
- 箭头方向暗示操作逻辑
- 图标造型对应具体功能语义
- 点击坐标严格绑定图标中心区域
技术突破难点
图标语义解析 需建立图标造型与功能语义的映射关系库(如铅笔图标=编辑功能)
动态干扰处理 自然背景纹理(雪地/水面)与图标边缘的精准分割
多图标协同验证 需同时识别3个图标的相对位置关系与点击顺序
缩放比例适配 页面显示尺寸与原始图片分辨率的坐标换算(示例中300px宽度需按2:1比例处理)
二、识别前置条件
必备素材规范
图标特征库 需建立包含箭头方向、造型轮廓的特征数据库(推荐使用SVG格式矢量图)
截图处理要求
- 原始图片尺寸:建议≥600×400像素
- 截图范围:需完整包含文字提示区与图标阵列区
- 坐标计算:页面显示宽度与原始宽度的比例换算(示例中300px对应600px原图)
测试样本集 建议准备200+张不同场景的验证码样本用于模型训练
三、核心识别代码样例
这里支持原图识别和截图识别两种,如果是原图识别就需要下面点击是顺序小图和点击区大图
1、点击顺序小图
2、点击区大图
如果是截图也按照上面的形式截图两张图
3、识别代码
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\\79-1.jpg')
# 图片转base64
img1_base64 = PIL_base64(img1)
img2 = Image.open(r'E:\\Python\\lixin_project\\OpenAPI接口测试\\test_img\\79-2.jpg')
# 图片转base64
img2_base64 = PIL_base64(img2)
# 验证码识别接口
url = "http://223.220.47.118:9009/openapi/verify_code_identify/"
data = {
# 用户的key
"key": "nHaAjgg5q1znn6tRUstY",
# 验证码类型
"verify_idf_id": "79",
# 点击区大图
"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)
# 标记识别结果
draw = ImageDraw.Draw(img1)
# 字体设置
font_type = "./msyhl.ttc"
font_size = 20
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()
运行上面代码会自动标记点击位置,点击效果如下:
四、更多图像识别
想了解更多验证码识别请访问:得塔云
评论前必须登录!
注册