验证码如下如所示,提供了三个方向剪头。根据剪头顺序,点击大图中图标的方向完成验证。
经过我们的努力,正确率达到了90%左右。下面提供了验证码识别的代码。
import base64
import requests
import datetime
from io import BytesIO
from PIL import Image
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\OpenAPI接口测试\test_img\47-1.jpg')
img2 = Image.open(r'E:\Python\lixin_project\OpenAPI接口测试\test_img\47-2.png')
img3 = Image.open(r'E:\Python\lixin_project\OpenAPI接口测试\test_img\47-3.png')
img4 = Image.open(r'E:\Python\lixin_project\OpenAPI接口测试\test_img\47-4.png')
# 图片转base64
img1_base64 = PIL_base64(img1)
img2_base64 = PIL_base64(img2)
img3_base64 = PIL_base64(img3)
img4_base64 = PIL_base64(img4)
# 验证码识别接口
url = "http://www.detayun.cn/openapi/verify_code_identify/"
data = {
# 用户的key
"key":"uvSC7fNzb797tMvgpB2F",
# 验证码类型
"verify_idf_id":"47",
# 大图
"img1": img1_base64,
# 小图1(顺序决定点击顺序)
"img2": img2_base64,
# 小图2(顺序决定点击顺序)
"img3": img4_base64,
# 小图3(顺序决定点击顺序)
"img4": img4_base64,
}
header = {"Content-Type": "application/json"}
# 发送请求调用接口
response = requests.post(url=url, json=data, headers=header)
# 获取响应数据,识别结果
print(response.text)
print("耗时:", datetime.datetime.now() - t1)
想了解更多验证码识别内容,请访问:得塔云