接口调用准备
1.进入网站:
https://console.bce.baidu.com/?_=1535519624081&fromai=1#/aip/overview
2.选择产品服务里的文字识别,点击创建应用
3.选择文字识别,创建完成
4.创建后选择该应用,可以看到里面有AppID、API Key、Secret Key三个参数,将三个参数放入下面实例对应的三个地方
5.参考实例,然后根据自己的需求来写程序
6.参考这里也行:http://ai.baidu.com/forum/topic/show/867951
#高精度
from aip import AipOcr
import json
""" 你的 APPID AK SK """
APP_ID = 'xxxxxxx'
API_KEY = 'yyyyyyyy'
SECRET_KEY = 'zzzzzzzzz'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 的方法"""
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
#定义图片的绝对路径
files = r"xxxxxxxx"
image = get_file_content(files)
""" 调用通用文字识别(高精度版) """
restu1 = client.basicAccurate(image)
""" 调用通用文字识别普通版 """
#restu1 = client.general(image)
""" 调用通用文字识别普通版 """
#restu1 = client.basicGeneral(image)
##需要在百度ai中新建应用
##https://blog.csdn.net/dcf869009372/article/details/81270143
# encoding:utf-8
##https://www.jb51.net/article/151450.htm
#https://blog.csdn.net/qq_15029743/article/details/86129690
#https://blog.csdn.net/u013032852/article/details/89216219
#https://blog.csdn.net/qq_38486203/article/details/82897137
#https://zdran.com/20190420.html
import base64
import urllib.parse
import urllib.request
'''
动物识别
'''
request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal"
# 二进制方式打开图片文件
f = open('f.jpg', 'rb')
img = base64.b64encode(f.read())
params = {"image":img,"top_num":6}
params = urllib.parse.urlencode(params).encode(encoding='UTF8')
access_token = a['access_token']
#access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token
request = urllib.request.Request(url=request_url, data=params)
request.add_header('Content-Type', 'application/x-www-form-urlencoded')
response = urllib.request.urlopen(request)
content = response.read()
if content:
print(bytes(content).decode('utf-8'))
https://blog.csdn.net/weixin_34216107/article/details/87163010