ADD file via upload

main
p9kh64cfp 8 months ago
parent 5084fdc12c
commit 441968436b

200
gui.py

@ -0,0 +1,200 @@
from warnings import simplefilter
simplefilter(action='ignore', category=FutureWarning)
from tkinter import ttk
import tkinter
import cv2
import numpy as np
from tkinter import *
from PIL import Image,ImageTk
from model_improved_alexnet import ImprovedAlexNet#调用模型
from tkinter.filedialog import askopenfilename
import tkinter.messagebox
import torch
from predict_improved_alexnet import improved_alexnet_predict#调用预测函数
import warnings
import os
warnings.filterwarnings("ignore")
from tkinter import *
import tkinter.simpledialog
import tkinter as tk
import tkinter.simpledialog as sd
from PIL import Image,ImageTk
import matplotlib.pyplot as plt#画图库
plt.rcParams['font.sans-serif']=['PingFang']
#plt.rcParams['axes.unicode_minus'] = False
class CustomDialog(sd.Dialog):
def __init__(self, parent, title=None, background_image='bg.png', user_icon_path='user.gif', password_icon_path='hmenu-lock.gif'):
self.background_image = background_image
self.user_icon_path = user_icon_path
self.password_icon_path = password_icon_path
super().__init__(parent, title)
def body(self, master):
# 加载背景图
bg_image = Image.open(self.background_image)
bg_photo = ImageTk.PhotoImage(bg_image)
new_width = 400
new_height = 150
style = ttk.Style()
style.configure('ExtraLarge.TButton', font=('Arial', 16), padding=15)
self.bg_label = tk.Label(master, image=bg_photo)
self.bg_label.image = bg_photo # 保持对图片的引用
self.bg_label.place(x=0, y=0, relwidth=10, relheight=10) # 背景标签覆盖整个对话框区域
# 调整对话框尺寸
self.geometry(f"{new_width}x{new_height}")
# 加载用户名图标
user_icon_image = Image.open(self.user_icon_path)
user_icon_photo = ImageTk.PhotoImage(user_icon_image)
# 加载密码图标
password_icon_image = Image.open(self.password_icon_path)
password_icon_photo = ImageTk.PhotoImage(password_icon_image)
# 创建带用户名图标的标签
label_with_user_icon = tk.Label(master, text="用户名:", image=user_icon_photo, compound='left', font=('Arial', 16))
label_with_user_icon.grid(row=0)
label_with_user_icon.image = user_icon_photo # 保持对用户名图标图片的引用
# 创建带密码图标的标签
label_with_password_icon = tk.Label(master, text="密码:", image=password_icon_photo, compound='left', font=('Arial', 16))
label_with_password_icon.grid(row=1)
label_with_password_icon.image = password_icon_photo # 保持对密码图标图片的引用
#创建用于输入用户名和密码的文本框,字体为arial大小为16
self.entry1 = tk.Entry(master, font=('Arial', 16))
self.entry2 = tk.Entry(master, font=('Arial', 16), show='*')#密码显示为*
#确定文本框位置
self.entry1.grid(row=0, column=1)
self.entry2.grid(row=1, column=1)
return self.entry1 # 设置初始焦点
#获取用户输入的两个值并将它们作为一个元组存储在self.result变量中
def apply(self):
first_value = self.entry1.get()
second_value = self.entry2.get()
self.result = (first_value, second_value)
# 设置窗体
root = Tk()#创建主窗口
root.withdraw()#隐藏主窗口
dialog = CustomDialog(root, "登录")#显示登陆窗口
result = dialog.result
#print("输入框1的值", result[0])
#print("输入框2的值", result[1])
if result is not None and str(result[0])=='12345' and str(result[1]) == '12345':
tkinter.messagebox.showinfo('提示:', '欢迎进入 中药识别')
root = Toplevel()
else:
tkinter.messagebox.showinfo('提示:', '登录失败')
exit(-1)
# 设置窗体
#root = Tk()
root.title("")
# 图片对话框信息
path = StringVar()
file_entry = Entry(root, state='readonly', text=path)
# 初始化
global now_img, alexnet_model
alexnet_model = None
#训练结果展示
def btn_pc():
image = plt.imread('./logs/0005.png')
plt.figure()#创建图形窗口
plt.imshow(image)#将读取的图片显示在图形窗口上
plt.axis('off')#关闭了图形的坐标轴显示
plt.title('模型训练曲线01-70')#标题
image = plt.imread('./logs/0006.png')
plt.figure()
plt.imshow(image)
plt.axis('off')
plt.title('模型混淆矩阵01-70')
image = plt.imread('./logs/0007.png')
plt.figure()
plt.imshow(image)
plt.axis('off')
plt.title('模型混淆矩阵71-100')
image = plt.imread('./logs/0008.png')
plt.figure()
plt.imshow(image)
plt.axis('off')
plt.title('模型混淆矩阵71-100')
plt.show()
# 选择图片
def choosepic():
default_dir = r"./test/"
path_ = askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser(default_dir)))
if len(path_) < 1:#若所选文件路径长度小于1即代表没有这个文件
return
path.set(path_)#路径赋值给path
global now_img
# 设置图片路径
now_img = file_entry.get()
#print(now_img)
img_open = Image.open(file_entry.get())#打开图片
img_open = img_open.resize((1100, 700))#调整大小
# 显示图片到窗体
img = ImageTk.PhotoImage(img_open)#将打开的图片转换为Tkinter可用的
image_label.config(image=img)#将图片显示在窗口中
image_label.image = img#将转换后的图片对象保存在image_label中显示在窗口中
# 退出软件
def btn_exit():
exit(0)
# 调用模型
def btn_ai():
global now_img
global alexnet_model
if alexnet_model is None:
# create model
alexnet_model = ImprovedAlexNet(num_classes=11)
# load model weights
model_weight_path = "./models/se-inc-alexnet.pth"#调用模型
alexnet_model.load_state_dict(torch.load(model_weight_path,map_location='cpu'))
alexnet_model.eval()
res=improved_alexnet_predict(now_img,alexnet_model)#调用预测函数进行并预测
s=str(res)#转化为字符形式
tkinter.messagebox.showinfo('提示:',s)#显示一个提示框,包含预测结果
# 显示识别率
def btn_acc():
res = ''
with open('./logs/model.txt', 'r', encoding='utf-8') as fb:
s = fb.read()
res = res + '识别率是:%.2f%%'%(float(s)*100)
tkinter.messagebox.showinfo('提示:', res)
# 设置窗体 可调整 主窗口
mainframe = ttk.Frame(root, padding="5 4 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)#使得当窗口调整大小时,该列/行也会相应地调整大小
# 配置按钮,联系上述对应功能
style = ttk.Style()
style.configure('ExtraLarge.TButton', font=('Arial', 16), padding=15)
#创建按钮
ttk.Button(mainframe, text="模型训练", command=btn_pc, style='ExtraLarge.TButton').grid(column=1, row=4, sticky=W)#左对齐
ttk.Button(mainframe, text="选择图片", command=choosepic, style='ExtraLarge.TButton').grid(column=2, row=4, sticky=W)
ttk.Button(mainframe, text="图像识别", command=btn_ai, style='ExtraLarge.TButton').grid(column=4, row=4, sticky=W)
ttk.Button(mainframe, text="模型准确率", command=btn_acc, style='ExtraLarge.TButton').grid(column=6, row=4, sticky=W)
ttk.Button(mainframe, text="关闭软件", command=btn_exit, style='ExtraLarge.TButton').grid(column=7, row=4, sticky=W)
# 显示初始化
image_label = ttk.Label(root,compound=CENTER)
image_label.grid(column=0,row=5, sticky=W)
bg = "./bg2.PNG"#封面
pil_image = Image.open(bg)#打开图片
pil_image = pil_image.resize((1100, 700))
img = ImageTk.PhotoImage(pil_image)
image_label.configure(image=img)
root.mainloop()#进入Tkinter主事件循环显示应用程序的界面并等待用户操作
Loading…
Cancel
Save