import tkinter as tk
from tkinter import filedialog
import requests
import json
import base64
API_KEY = "neuBDCNoKMuX991928suXxVf"
SECRET_KEY = "cKxrlhAzdoYwdvhYH4wdrkGtGfSkqu9F"
def main():
root = tk.Tk()
root.withdraw() # 隐藏Tkinter窗口
file_path = filedialog.askopenfilename() # 打开文件选择对话框
if file_path:
classify_image(file_path)
else:
print("未选择文件。")
def classify_image(file_path):
url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant?access_token=" + get_access_token()
with open(file_path, 'rb') as file:
image_data = file.read()
base64_data = base64.b64encode(image_data).decode('utf-8') # 将图像数据转换为Base64编码
payload = {
'image': base64_data,
'baike_num': 1
}
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
response = requests.post(url, headers=headers, data=payload)
if response.status_code == 200:
result = json.loads(response.text)
if 'result' in result:
plant_info = result['result'][0]
plant_name = plant_info['name']
plant_baike = plant_info['baike_info']['description']
print("植物名称:", plant_name)
print("百科信息:", plant_baike)
else:
print("无法识别植物。")
else:
print("识别失败。")
def get_access_token():
url = "https://aip.baidubce.com/oauth/2.0/token"
params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
response = requests.post(url, params=params)
if response.status_code == 200:
access_token = response.json().get("access_token")
return access_token
else:
print("获取访问令牌失败。")
return None
if __name__ == '__main__':
main()