test-1

import tkinter as tk
from PIL import Image,ImageTk
from tkinter import filedialog
import json
import requests
import base64
import pandas as pd
 
# EasyDL之图片分类API接口实现-基于python tkinter实现
# 打开图片文件
def OpenFile():
    root = tk.Tk()
    root.withdraw()  # 隐藏主窗口
    canvas.delete(tk.ALL)
    default_color = root.cget("bg")
    canvas.configure(bg=default_color)
    global file_path
    file_path = filedialog.askopenfilename()
    image = Image.open(file_path)
    
    width, height = image.size
    aspect_ratio = width / height
    target_width, target_height = (canvas_width, canvas_height)
    target_aspect_ratio = target_width / target_height
 
    # 计算调整后图像的实际大小
    if target_aspect_ratio < aspect_ratio:
        # 以目标宽度为准进行调整
        new_width = target_width
        new_height = int(target_width / aspect_ratio)
    else:
        # 以目标高度为准进行调整
        new_width = int(target_height * aspect_ratio)
        new_height = target_height
 
    # 调整图像的大小并保存
    resized_image = image.resize((new_width, new_height))
    
    global image_file
    image_file= ImageTk.PhotoImage(resized_image)  #注:如果显示图片这个功能定义在一个方法里,image_file  一定要申明为全局变量,窗口才会显示图片
    image = canvas.create_image(0,0,anchor='nw',image=image_file)
 
# 提交检测
def Submit():
    # client_id 为官网获取的AK, client_secret 为官网获取的SK
    # 有关介绍可以参考:https://blog.csdn.net/cyh0503/article/details/83796694
    API_KEY =HfMxYv13YbjnRkL4PJa372Hk
    SECRET_KEY =gauRc4W6kYwacn46zZZ5SgTkCwQ2xmfz
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials' \
           "&client_id={}&client_secret={}".format(API_KEY, SECRET_KEY)
 
    response = requests.get(host)
    content = response.json()
    access_token = content["access_token"]
 
    image = open(file_path, 'rb').read()
    data = {'image': base64.b64encode(image).decode()}
 
    request_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/henannongye"+"?access_token=" + access_token
    response = requests.post(request_url, data=json.dumps(data))
    content = response.json()
 
    df = pd.DataFrame(content)
    results = df['results']
    print(results)
    
    score_key = 0
    score_cup = 0
    if results[0]['name'] == labelStrKey:
        score_key = results[0]['score']
        score_cup = results[1]['score']
    else:
        score_key = results[1]['score']
        score_cup = results[0]['score']
        
    res_key= '{:.2f}'.format(score_key * 100)+"%"    
    res_cup= '{:.2f}'.format(score_cup * 100)+"%"
 
    label5.config(text=res_key)
    label7.config(text=res_cup)  
 
# 创建窗口
# tkinter介绍可参考:https://c.biancheng.net/tkinter/what-is-gui.html    
root = tk.Tk()
root.title("静态物体分类")
root.geometry("800x600")
 
# 创建画布用于图片显示
canvas_height=400
canvas_width=600
canvas = tk.Canvas(root, bg='#CCE8CF', height=canvas_height,width=canvas_width)
canvas.grid(row=0,column =0,columnspan = 2)
 
# 创建按钮并排列
button1 = tk.Button(root, text="打开图片文件",command=OpenFile)
button1.grid(row=1, column=0)
 
button2 = tk.Button(root, text="提交检测",command=Submit)
button2.grid(row=1, column=1)
 
# 创建标签用于显示结果
label1 = tk.Label(root, text="识别结果:")
label1.grid(row=2, column=0)
 
label2 = tk.Label(root, text="名称")
label2.grid(row=3, column=0)
 
label3 = tk.Label(root, text="置信度")
label3.grid(row=3, column=1)
 
labelStrKey = "枸杞"
labelStrCup = "百合"
labelStr1  =  "槐花"
labelStr2  =  "金银花"
labelStr3  =  "党参"
 
label4 = tk.Label(root, text=labelStrKey)
label4.grid(row=4, column=0)
 
label5 = tk.Label(root, text="0%")
label5.grid(row=4, column=1)
 
label6 = tk.Label(root, text=labelStrCup)
label6.grid(row=5, column=0)
 
label7 = tk.Label(root, text="0%")
label7.grid(row=5, column=1)
 
label8 = tk.Label(root, text=labelStr1)
label8.grid(row=6, column=0)
 
label9 = tk.Label(root, text="0%")
label9.grid(row=6, column=1)
 
labe20 = tk.Label(root, text=labelStr2)
labe20.grid(row=7, column=0)
 
labe21 = tk.Label(root, text="0%")
labe21.grid(row=7, column=1)
 
labe22 = tk.Label(root, text=labelStr3)
labe22.grid(row=8, column=0)
 
labe23 = tk.Label(root, text="0%")
labe23.grid(row=8, column=1)
 
# 运行窗口
root.mainloop()(row=5, column=1)
 
# 运行窗口
root.mainloop()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值