1、首先打开百度文字识别官网,输入百度账号登陆
2、找到文字识别,然后进入后点击创建应用
3、应用名称,随便填写一个即可
文件识别包名选择 不需要即可,描述随便填一些
4、点击立即创建,出现如下图,点击查看应用详情
如下图已经申请到API key和Secret Key
拿到上面的apikey和Secret Key我们就可以做事情了。
5、附上图片验证码(数字+字母)识别案列(用于某柠打卡):
现在大多数网站登录时候我们只能获取加密后的图片地址,因此案例传输的是图片加密后的字符串,如果传输png、jpg…等格式图片,直接按照代码注释中的提示修改即可。
API列表,提供给我们许多免费请求地址
复制代码
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
42import os import requests import urllib def get_pic(imgbase64str): try: temp_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【API KEY】&client_secret=【SECRET KEY】' temp_res = requests.post(temp_url) temp_token = eval(temp_res.text)['access_token'] #https://aip.baidubce.com/rest/2.0/ocr/v1/accurate #高精度含位置识别方案 #https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic #高精度不含位置识别方案 temp_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token=' + temp_token temp_headers = {'Content-Type': 'application/x-www-form-urlencoded'} # temp_file = open(image, 'rb') #直接传图片的话去掉这里的注释就好了 # temp_image = temp_file.read() # temp_file.close() # temp_data = { # 'image': base64.b64encode(temp_image) # } image = imgbase64str.split(",")[1] temp_data={ 'image':image } temp_data = urllib.parse.urlencode(temp_data) temp_res = requests.post(url=temp_url, data=temp_data, headers=temp_headers) code = eval(temp_res.text)['words_result'][0]['words'] resultj = re.sub(u"([^u4e00-u9fa5u0030-u0039u0041-u005au0061-u007a])", "", code) print("识别的验证码为:"+resultj) #正则去掉识别出的特殊字符 #result_four = resultj[0:4] # 只获取前4个字符 return resultj except Exception as e: return e print('验证码识别异常,请联系管理员') def convertBase642LocalImg(imgbase64str): #将imgbase64转换为本地图片存储 img = imgbase64str.split(",")[1] with open("E:\PushCard\1.png", 'wb') as f: f.write(base64.b64decode(img)) if __name__=="__main__": result=get_pic("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAAAeCAYAAAC7Q5mxAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAHLSURBVGhD7ZfRcYQgEIZtx6vEGqzDBnxOCzyaEu75Xu46uAIcO7CCDSggwqLgmmRM9pthJhO58fdjWbQAhkQgsCg+9F9MCmgFssR0WCCRaA9kiWl4AnsQVSHl7Y9K9Po3OGNXw+ftFo66g1HPgWcbXm+f+qIiIU/z0HN3wO4VG2ULg/7ZHpEKNMEbwOI9mn2BhqEtbbB7h8Ua4F3LOZuh8TwqxywSz7lCCXQXT2IW+eWu2dDBvazhnWjwkEAZHUSiQIWV6D3ABPJgIbE8ToXuVaK8z0qUBBUoGbv2uwXmoqvMr8JptVO2SzxPL6pZYCXkrDxiAnPIFCj/Lw4qnWTNEufAT3glb5W/IrAXUKU2bQy3kWf0mS2Btg8eyPUDApFBESjZ7IdRcIHLIVJBRku2XK8CJSa0GvipjLGxoAe2ruFaPVChtrB6zzP98IQeSOEXBBJQ0pxtayuReApTuJBAdeKGomw/XH19YPxrgVuvK+oa8n4YcFmBx043i3lt2Thx3U+9+IOclMfD3PtEgSaoP3JXfvnysCOowqX61vPcrX5WnjW2/3rjiMhIBTKpsEAiLJAICyTCAomwQCIskAgLJMICibBAIiyQBMAX52VauxEZANgAAAAASUVORK5CYII=")
参数为图片的案例:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25def getVertifyCode(image): try: temp_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【API KEY】&client_secret=【SECRET KEY】' temp_res = requests.post(temp_url) temp_token = eval(temp_res.text)['access_token'] temp_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=' + temp_token temp_headers = {'Content-Type': 'application/x-www-form-urlencoded'} temp_file = open('图片地址', 'rb') temp_image = temp_file.read() temp_file.close() temp_data = { 'image': base64.b64encode(temp_image) } temp_data = urllib.parse.urlencode(temp_data) temp_res = requests.post(url=temp_url, data=temp_data, headers=temp_headers) code = int(eval(temp_res.text)['words_result'][0]['words']) except Exception as e: print(e) print('验证码识别异常,请联系管理员') if __name__=="__main__": images = os.listdir("E:/pic/") for image_name in images: image="E:/pic/%s" % image_name getVertifyCode(image)
最后还可以通过百度提供的baidu-aip库进行识别:
需要安装baidu-aip库,直接pip Install baidu-aip
即可
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24from aip import AipOcr import os """你的百度AppID, API Key, Secret Key""" APP_ID = '16545975' API_KEY = 'qbK2kKKtrXTo0rE1rg4M6Tl6' SECRET_KEY = 'xxxxxxxxxxx' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) """打开文件,读取图片""" def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() PROJECT_ROOT = os.path.dirname(os.path.realpath(__file__))#获取项目根目录 path = os.path.join(PROJECT_ROOT,"images") #文件路径 for r, ds, fs in os.walk(path): for fn in fs: fname = os.path.join(r, fn) image = get_file_content(fname) ret = client.basicGeneral(image) for item in ret['words_result']: print(item['words'])
最后
以上就是温柔棒棒糖最近收集整理的关于申请百度文字识别APIkey和Secret Key+文字验证码识别案例的全部内容,更多相关申请百度文字识别APIkey和Secret内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复