forked from Rain-shadow/cdr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_verification.py
More file actions
46 lines (41 loc) · 1.56 KB
/
custom_verification.py
File metadata and controls
46 lines (41 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# cython : language_level=3
# @Time : 2021-01-15, 0015 21:38
# @Author: 佚名
# @File : custom_verification.py
import requests
import aiohttp
# 获取词达人配置目录路径
from cdr.config import CONFIG_DIR_PATH
# 获取词达人日志记录实例
from cdr.utils.log import Log
_r = aiohttp.ClientSession()
_logger = Log.get_logger()
# 该类名不得修改
class CustomVerification:
# 该函数名不得修改
@staticmethod
async def get_vc(img_base64: str, img_path: str, task_id):
"""
自定义验证码机制,前三次识别交由用户去实现验证码识别
:param img_base64: 图片的base64编码,格式为png
:param img_path: 图片的本地路径
:param task_id: 任务id,一般不用
:return: 验证码字符串,返回-1将重新生成验证码
"""
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
"image": f"data:image/png;base64,{img_base64}",
"type": "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic",
"detect_direction": False,
"language_type": "ENG"
}
res = await _r.post("https://ai.baidu.com/aidemo", headers=headers, data=data)
json_data = await res.json(content_type="text/json")
if json_data["data"]["words_result_num"] == 0:
return "-1"
code = json_data["data"]["words_result"][0]["words"]
return "".join(code.split())