from os import listdir
from tkinter import *
from tkinter import filedialog
from PIL import Image,ImageTk
from tkinter import messagebox
files = {}
img_open = None
img = None
help_text="""
1.打开文件
2.选择打开文件或文件夹
3.单击显示图片
"""
root = Tk()
root.title("叶片计数")
root.geometry("600x600")
#-------------------命令----------------------#
def open_file():
file_paths = filedialog.askopenfilenames()
list1.delete(0,END)
files.clear()
file_list = []
for file_path in file_paths:
name = file_path.split("/")[-1]
file_list.append(name)
files[name] = file_path
list1.insert(END,name)
def open_dir():
dir_path = filedialog.askdirectory()
list1.delete(0,END)
files.clear()
file_names = []
target_file_name = ["jpg","png","gif"]
for name in listdir(dir_path):
if name.split(".")[-1].lower() in target_file_name:
file_names.append(name)
files[name] = dir_path+"/"+name
list1.insert(END,name)
def show_view(event):
global img_open
global img
try:
back.delete(ALL)
get_file = list1.get(ACTIVE)
file_path = files[get_file]
except:
return
back_width = back.winfo_width()
back_height = back.winfo_height()
img_open = Image.open(file_path)
img_open.thumbnail((back_width,back_height),Image.ANTIALIAS)
img = ImageTk.PhotoImage(img_open)
back.create_image(back_width / 2, back_height / 2,anchor=CENTER,image=img)
def refresh(event):
root_width = root.winfo_width()
root_height = root.winfo_height()
back.config(width=root_width - 200, height=root_height - 10)
show_view(1)
def train():
pop1 = Toplevel()
pop1.title("测试")
pop1.geometry("200x200")
Label(pop1,text="batch").grid(row=0,sticky=E)
Label(pop1,text="learning_rate").grid(row=1,sticky=E)
Label(pop1,text="width").grid(row=2, sticky=E)
Label(pop1, text="height").grid(row=3, sticky=E)
Label(pop1, text="decay").grid(row=4, sticky=E)
Label(pop1, text="steps").grid(row=5, sticky=E)
Label(pop1, text="max_batches").grid(row=6, sticky=E)
Entry(pop1).grid(row=0,column=1)
Entry(pop1).grid(row=1,column=1)
Entry(pop1).grid(row=2, column=1)
Entry(pop1).grid(row=3, column=1)
Entry(pop1).grid(row=4, column=1)
Entry(pop1).grid(row=5, column=1)
Entry(pop1).grid(row=6, column=1)
def over():
messagebox.showinfo("训练", "训练完毕")
Button(pop1,text="开始训练",command=over).grid(row=7,columnspan=4,pady=5)
pop1.mainloop()
def test():
pop2 = Toplevel()
pop2.title("测试")
pop2.geometry("200x200")
frame = LabelFrame(pop2,text=" 测试结果 ",padx=5,pady=5)
frame.pack()
Label(frame, text="mAP:60.8%",justify=LEFT).grid(row=0,column=0)
Label(frame, text="Precision:81.3%",justify=LEFT).grid(row=1, column=0)
Label(frame, text="Recall:76.1%",justify=LEFT).grid(row=2, column=0)
Label(frame, text="F1-score:78.6%",justify=LEFT).grid(row=3, column=0)
pop2.mainloop()
def val():
pop3 = Toplevel()
pop3.title("检测")
pop3.geometry("200x200")
frame = LabelFrame(pop3, text=" 检测结果 ", padx=5, pady=5)
frame.pack()
pop3.mainloop()
#-------------------菜单栏--------------------#
menubar = Menu(root)
file_menu = Menu(menubar,tearoff=0)
file_menu.add_command(label="打开文件",command=open_file)
file_menu.add_command(label="打开文件夹",command=open_dir)
file_menu.add_separator()
file_menu.add_command(label="退出",command=root.quit)
menubar.add_cascade(label="文件",menu=file_menu)
menubar.add_command(label="训练",command=train)
#test_menu = Menu(menubar,tearoff=0)
#menubar.add_cascade(label="测试",menu=test_menu)
menubar.add_command(label="测试",command=test)
menubar.add_command(label="检测",command=val)
#detect_menu = Menu(menubar,tearoff=0)
#menubar.add_cascade(label="检测",menu=detect_menu)
root.config(menu=menubar)
#------------------画布-----------------------#
back = Canvas(root,bg="white",width=400,height=600)
back.pack(side=RIGHT,fill=BOTH,expand=True)
back.create_text(200,300,anchor=CENTER,text=help_text)
#------------------滚动条---------------------#
sb = Scrollbar(root)
sb.pack(side=RIGHT,anchor=CENTER,fill=Y)
#-------------------列表框--------------------#
list1 = Listbox(root,yscrollcommand=sb.set)
list1.pack(side=LEFT,fill=Y)
list1.bind("<Button-1>",show_view)
sb.config(command=list1.yview)
root.bind("<Configure>",refresh)
root.mainloop()