百度AI接口 --(通用文字识别-识别验证码)
一 登录百度ai--创建应用
链接 https://console.bce.baidu.com/ai/?_=1625989618048#/ai/ocr/overview/index
二 获取AK,SK
三 (获取 Access Token)
import requests import pprint import base64 # 1、获取Access Token # client_id 为官网获取的AK, client_secret 为官网获取的SK client_Id ="lb5bETZ9xxtU7AMht9LqW50Q" #【官网获取的AK】 client_secret ="官网的SK" # 【官网获取的SK】 host = f\'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={client_Id}&client_secret={client_secret}\' response = requests.get(host) if response: pprint.pprint(response.json()) # access_token =response.json()["access_token"]
四 识别图片验证码
equest_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic" #通用识别API f = open(\'code.png\', \'rb\')# 二进制方式打开图片文件 img = base64.b64encode(f.read())# 加密 params = {"image":img} access_token = \'24.365ff5e54a74ed5a3535174a5c029750.2592000.1628575666.282335-245292\'#用上一步获取的access_token request_url = request_url + "?access_token=" + access_token #网址 headers = {\'content-type\': \'application/x-www-form-urlencoded\'}#头部 response = requests.post(request_url, data=params, headers=headers) if response: print (response.json()) # {\'words_result\': [{\'words\': \'7364\'}], \'log_id\': 1414105624478223025, \'words_result_num\': 1}