第一步还是引库,确定自己安装过这些库了,没有安装的需要自行安装
import requests
import base64
import os
#os库是下面多张图片进行识别的,如果一次识别一张图片就不需要用os库
接下来看代码
def A_Token(access_url):
'''获取access_token'''
pa = {"grant_type": "client_credentials", # 固定值
"client_id": "*******************", # 找到自己的API Key
"client_secret": "***************"} # 找到自己的Secret Key
re = requests.get(access_url, params=pa).json()
# if response:
# print(response.json())
atoken = re.get("access_token") # 提取access_token的值
return atoken
def getHtml(url, img):
'''请求植物识别API接口'''
hd = {'content-type': 'application/x-www-form-urlencoded'}
par = {"image": img}
response = requests.post(url, data=par, headers=hd)
if response:
response.json()
plant = response.json()
print(plant)
print("植物的可能性从大到小为:", plant["result"][0]["name"],end=",")
print(plant["result"][1]["name"],end=",")
print(plant["result"][2]["name"])
access_url = "https://aip.baidubce.com/oauth/2.0/token"
url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant"
accesstoken = A_Token(access_url)
'''获取access_token'''的步骤
获取access_token:首先需要自己去百度云创建一个账号,点开右上角的管理控制台,进去之后把鼠标放在产品服务上