You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.4 KiB
48 lines
1.4 KiB
import requests
|
|
import base64
|
|
import json
|
|
|
|
API_KEY = "kbt40nMOF8tLGg5IdZStJc4G"
|
|
SECRET_KEY = "jUoj82HAALIzDWRlrTauoHwLcnB3eAn7"
|
|
|
|
|
|
def main():
|
|
url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant?access_token=" + get_access_token()
|
|
|
|
# 二进制方式打开图片文件
|
|
with open("photos/3.jpg", "rb") as image_file:
|
|
image = base64.b64encode(image_file.read()).decode('utf-8')
|
|
|
|
payload = {
|
|
"image": image,
|
|
"baike_num": 0 # 返回百科信息的数量
|
|
}
|
|
headers = {
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
'Accept': 'application/json'
|
|
}
|
|
|
|
response = requests.post(url, headers=headers, data=payload)
|
|
result = response.json()
|
|
|
|
# 提取并打印植物名称和置信度
|
|
if 'result' in result:
|
|
print("可能的识别结果为:")
|
|
for item in result['result']:
|
|
print(f"植物名称: {item['name']}, 概率: {item['score']}")
|
|
else:
|
|
print("识别失败或未找到植物")
|
|
|
|
|
|
def get_access_token():
|
|
"""
|
|
使用 AK,SK 生成鉴权签名(Access Token)
|
|
:return: access_token,或是None(如果错误)
|
|
"""
|
|
url = "https://aip.baidubce.com/oauth/2.0/token"
|
|
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
|
|
return str(requests.post(url, params=params).json().get("access_token"))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main() |