-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI_sim_1.py
185 lines (156 loc) · 7.33 KB
/
GUI_sim_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# coding: utf-8
from time import ctime, localtime
#python version
from sys import version
print('Python version:',version)
#import ttk
if version[0] == '2':
from Tkinter import *
from ttk import Button, Checkbutton, Label, Entry, Combobox
else: #version[0] == '3':
from tkinter import *
from tkinter.ttk import Button, Checkbutton, Label, Entry, Combobox
from PIL import ImageTk, Image
#from subprocess import call
from try_inference_tf import inference, models
class GUI():
vb_dict = {}
def __init__(self, master):
self.master = master
self.row = 0 #for grid
self.all_comp = []
self.get_comp = []
self.vb_name = []
self.buildGUI_1()
#self.show_all_variable()
# --------Mode interface ---------
def buildGUI_1(self): #CNN
self.master.title('build CNN -- load model')
self.label_1to1_text_combobox("Model", models , width=50 ) # models = ("inception_v3","vgg16"...)
self.label_1to1_text_entry(name="ImageUrl", default_text="person.jpg", width=100)
self.vb_dict = self.generate_variable_dict()
self.button(self.click_show_img, "Show Image")
self.button(self.click_inference, "Inference")
self.inference_result_str = StringVar()
self.textvariable_label(textvariable=self.inference_result_str)
def click_show_img(self):
self.vb_dict = self.generate_variable_dict() # cannot skip
img_path = self.vb_dict["ImageUrl"]
self.img_label(path=img_path)
def click_inference(self):
self.vb_dict = self.generate_variable_dict() # cannot skip
str_ = inference(self.vb_dict["ImageUrl"],self.vb_dict["Model"])
self.inference_result_str.set(str(str_))
# --------Component Conbination ---------
def label_1to1_text_combobox(self, name="", values=("1","2"), default_Chosen=0, width=10):
self.vb_name.append(name.split()[0])
self.all_comp.append(Label(self.master, text=name))
#self.all_comp[-1].pack(anchor='w')
self.all_comp[-1].grid(row=self.row, sticky=E) #
self.get_comp.append(StringVar(self.master, values[default_Chosen]))
self.all_comp.append(Combobox(self.master, width=width, textvariable=self.get_comp[-1]))
#self.all_comp[-1].pack()
self.all_comp[-1]['values'] = values
self.all_comp[-1].current(default_Chosen)
self.all_comp[-1].grid(row=self.row, column=1, sticky=W) #
self.row += 1 #
def label_1to1_float_entry(self, name="", default_float=0.01, width=5):
self.vb_name.append(name.split()[0])
self.all_comp.append(Label(self.master, text=name))
#self.all_comp[-1].pack(anchor='w')
self.all_comp[-1].grid(row=self.row, sticky=E) #
self.get_comp.append(DoubleVar(self.master, default_float))
self.all_comp.append(Entry(self.master, width=width, textvariable=self.get_comp[-1]))
#self.all_comp[-1].pack()
self.all_comp[-1].grid(row=self.row, column=1, sticky=W) #
self.row += 1 #
def label_1to1_int_entry(self, name="", default_int=-1, width=10):
self.vb_name.append(name.split()[0])
self.all_comp.append(Label(self.master, text=name))
#self.all_comp[-1].pack(anchor='w')
self.all_comp[-1].grid(row=self.row, sticky=E) #
self.get_comp.append(IntVar(self.master, default_int))
self.all_comp.append(Entry(self.master, width=width, textvariable=self.get_comp[-1]))
#self.all_comp[-1].pack()
self.all_comp[-1].grid(row=self.row, column=1, sticky=W) #
self.row += 1 #
def label_1to1_text_entry(self, name="", default_text="", width=35):
self.vb_name.append(name.split()[0])
self.all_comp.append(Label(self.master, text=name))
#self.all_comp[-1].pack(anchor='w')
self.all_comp[-1].grid(row=self.row, sticky=E) #
self.get_comp.append(StringVar(self.master, default_text))
self.all_comp.append(Entry(self.master, width=width, textvariable=self.get_comp[-1]))
#self.all_comp[-1].pack()
self.all_comp[-1].grid(row=self.row, column=1, sticky=W) #
self.row += 1 #
def label_1to3_int_entry(self, name="", default_int=[-1,-1,-1], width=10):
for i in [1,2,3]:
self.vb_name.append(name.split()[0]+str(i))
self.all_comp.append(Label(self.master, text=name))
#self.all_comp[-1].pack(anchor='w')
self.all_comp[-1].grid(row=self.row, sticky=E) #
for i, int_ in enumerate(default_int):
self.get_comp.append(IntVar(self.master, int_))
self.all_comp.append(Entry(self.master, width=width, textvariable=self.get_comp[-1]))
#self.all_comp[-1].pack()
self.all_comp[-1].grid(row=self.row, column=i+1, sticky=W) #
self.row += 1
def label_1to1_bool_checkbutton(self, name="", default_bool=True ,default_text="Yes"):
self.vb_name.append(name.split()[0])
self.all_comp.append(Label(self.master, text=name))
#self.all_comp[-1].pack(anchor='w')
self.all_comp[-1].grid(row=self.row, sticky=E) #
self.get_comp.append(BooleanVar(self.master, default_bool))
self.all_comp.append(Checkbutton(self.master, text=default_text, variable =self.get_comp[-1], offvalue =False, onvalue =True));
#self.all_comp[-1].pack()
self.all_comp[-1].grid(row=self.row, column=1, sticky=W) #
self.row += 1 #
def textvariable_label(self, textvariable):
self.all_comp.append(Label(self.master, textvariable=textvariable))
#self.all_comp[-1].pack()
self.all_comp[-1].grid(row=self.row, column=1) #
self.row += 1 #
def text_label(self, text="OK"):
self.all_comp.append(Label(self.master, text=text))
#self.all_comp[-1].pack()
self.all_comp[-1].grid(row=self.row, column=1) #
self.row += 1 #
def text_text(self, text="OK"):
text_ = Text(self.master, width=80 , height=10)
text_.insert(INSERT, text)
text_.insert(END, "")
self.all_comp.append(text_)
#self.all_comp[-1].pack()
self.all_comp[-1].grid(row=self.row, column=1) #
self.row += 1 #
def img_label(self, path="cat1.jpeg"):
window = Toplevel()
#window.geometry('400x400')
window.title('image')
img = ImageTk.PhotoImage(Image.open(path))
label = Label(window, image = img)
label.pack()
window.mainloop()
def button(self, command, text="OK"):
self.all_comp.append(Button(self.master, text=text, command = command))
#self.all_comp[-1].pack()
self.all_comp[-1].grid(row=self.row, column=1) #
self.row += 1 #
# ---Show------------------------------------------------------
def show_all_variable(self):
print('show all variable:')
i=0
for c in self.get_comp:
print('{} = {}'.format(self.vb_name[i],c.get()))
i+=1
def generate_variable_dict(self):
i=0
d={}
for c in self.get_comp:
d.update({self.vb_name[i]:c.get()})
i+=1
return d
root = Tk()
my_gui = GUI(root)
root.mainloop()