示例代码:
提示一:使用示例代码前,请记得替换其中的示例Token、图片地址或Base64信息。
提示二:部分语言依赖的类或库,请在代码注释中查看下载地址。
链接:https://cloud.baidu.com/doc/IMAGERECOGNITION/s/Zk3bcxdfr
import requests
import base64
'''
动物识别
'''
request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal"
# 二进制方式打开图片文件
f = open('C://Users/Lenovo/Desktop/2021MCM_ProblemC_Files/2021MCM_ProblemC_Files/ATT2_HoireRDBeekeeperReport.jpg', 'rb')
img = base64.b64encode(f.read())
params = {"image":img}
access_token = '24.7722068937aeaa6cf0b694cde102c482.2592000.1615272386.282335-23647982'
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())
access_token获取:
链接:https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu
grant_type: 必须参数,固定为client_credentials;
client_id: 必须参数,应用的API Key;
client_secret: 必须参数,应用的Secret Key;
# encoding:utf-8
import requests
# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=YYZ8sYiRshqx1nocSvQfV8zH&client_secret=6uNZvcnlqHGfdx2WvW9agALkwItiH7Os'
response = requests.get(host)
if response:
print(response.json())
应用API Key获取:
图片.png
使用链接:https://www.bilibili.com/video/av42432618
https://cloud.baidu.com/product/imagerecognition/animal