-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_util.py
215 lines (185 loc) · 7.47 KB
/
gui_util.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import threading
from functools import partial
import webbrowser
import tkinter
import tkinter.ttk
import tkinter.messagebox
from tkinter import CURRENT, END, Text
from src.task.wrapper import BaseWrapper
from src.exceptions import ExceptionWithHyperlink
from src.config import dataset_docs_url
class HyperlinkManager:
def __init__(self, text):
self.text = text
self.text.tag_config("hyper", foreground="blue", underline=1)
self.text.tag_bind("hyper", "<Enter>", self._enter)
self.text.tag_bind("hyper", "<Leave>", self._leave)
self.text.tag_bind("hyper", "<Button-1>", self._click)
self.reset()
def reset(self):
self.links = {}
def add(self, action):
# add an action to the manager. returns tags to use in
# associated text widget
tag = "hyper-%d" % len(self.links)
self.links[tag] = action
return "hyper", tag
def _enter(self, event):
self.text.config(cursor="hand2")
def _leave(self, event):
self.text.config(cursor="")
def _click(self, event):
for tag in self.text.tag_names(CURRENT):
if tag[:6] == "hyper-":
self.links[tag]()
return
class Validator(threading.Thread):
def __init__(self, dataset_type, task_type, train_dir_get, test_dir_get, valid_dir_get, output_dir_get, yaml_path_get, id2label_path_get, x, y):
threading.Thread.__init__(self)
self.daemon = True
self.dataset_type=dataset_type
self.task_type=task_type
self.train_dir_get=train_dir_get
self.test_dir_get=test_dir_get
self.valid_dir_get=valid_dir_get
self.output_dir_get=output_dir_get
self.yaml_path_get=yaml_path_get
self.id2label_path_get=id2label_path_get
self.x=x
self.y=y
return
def run(self):
try:
run(self.dataset_type, self.task_type, self.train_dir_get, self.test_dir_get, self.valid_dir_get, self.output_dir_get, self.yaml_path_get, self.id2label_path_get, self.x, self.y)
return True
except Exception as e:
return e
def open_url(eventObject):
webbrowser.open_new(eventObject.widget.cget("text"))
def run(
dataset_type,
task_type,
train_dir_get,
test_dir_get,
valid_dir_get,
output_dir_get,
yaml_path_get,
id2label_path_get,
x,
y
):
try:
#start progress bar
popup = tkinter.Toplevel()
popup.geometry("+%d+%d" %(x+120,y+100))
popup_label=tkinter.Label(popup, text="Start Validation", width=25)
popup_label.grid(row=0,column=0)
def progress_close():
popup.destroy()
progress = 0
progress_var = tkinter.DoubleVar()
progress_bar = tkinter.ttk.Progressbar(popup, variable=progress_var, maximum=100, mode="determinate", length=100, orient='horizontal') # mode can be indeterminate
progress_bar.grid(row=1, column=0)
progress_close_button = tkinter.ttk.Button(popup, text="cancel", command=progress_close)
progress_close_button.grid(column=0, row=2, sticky='nesw')
popup.pack_slaves()
succeed_list = []
base_wrapper = BaseWrapper(dataset_type, task_type)
popup.update()
popup_label.config(text="Doing validation preprocess.")
base_wrapper.task_wrapper.task_class.preprocess(output_dir_get, valid_dir_get, test_dir_get)
progress += 20
progress_var.set(progress)
popup.update()
popup_label.config(text="Validate train dataset.")
train_zip_path, train_yaml, train_md5, succeed = base_wrapper.dataset_format_wrapper.format_module.validate(
yaml_path=yaml_path_get,
root_path=train_dir_get,
output_dir=output_dir_get,
split_name="train",
id2label_path=id2label_path_get
)
succeed_list.append(succeed)
progress += 20
progress_var.set(progress)
popup.update()
popup_label.config(text="Validate validation dataset.")
if valid_dir_get:
valid_zip_path, valid_yaml, valid_md5, succeed = base_wrapper.dataset_format_wrapper.format_module.validate(
yaml_path=yaml_path_get,
root_path=valid_dir_get,
output_dir=output_dir_get,
split_name="val",
id2label_path=id2label_path_get
)
succeed_list.append(succeed)
else:
valid_yaml=None
valid_md5=None
progress += 20
progress_var.set(progress)
popup.update()
popup_label.config(text="Validate test dataset.")
if test_dir_get :
test_zip_path, test_yaml, test_md5, succeed = base_wrapper.dataset_format_wrapper.format_module.validate(
yaml_path=yaml_path_get,
root_path=test_dir_get,
output_dir=output_dir_get,
split_name="test",
id2label_path=id2label_path_get
)
succeed_list.append(succeed)
else:
test_yaml=None
test_md5=None
progress += 20
progress_var.set(progress)
popup.update()
popup_label.config(text="Doing validate postprocess.")
base_wrapper.task_wrapper.task_class.postprocess(
dataset_type,
train_yaml,
valid_yaml,
test_yaml,
valid_dir_get,
test_dir_get,
train_md5,
test_md5,
valid_md5,
output_dir_get,
succeed_list,
task_type
)
progress += 20
progress_var.set(progress)
if progress_var.get()==100:
popup.destroy()
if all(succeed_list): # success
new_popup = tkinter.Toplevel()
new_popup.geometry("+%d+%d" %(x+120,y+100))
new_popup_label_first=tkinter.Label(new_popup, text="Validation Success!", width=25)
new_popup_label_first.grid(row=0,column=0)
new_popup_label_second=tkinter.Label(new_popup, text="Upload your dataset at ", width=25)
new_popup_label_second.grid(row=1,column=0)
new_popup_label_third = tkinter.Label(new_popup, text=r"https://netspresso.ai/", fg="blue", cursor="hand2")
new_popup_label_third.grid(row=2,column=0)
new_popup_label_third.bind("<Button-1>", open_url)
def close():
new_popup.destroy()
close_button = tkinter.ttk.Button(new_popup, text="Ok", command=close)
close_button.grid(column=0, row=3, sticky='nsew', pady=5, padx=5)
new_popup.update()
else:
tkinter.messagebox.showinfo("Info", f"Validation Fail, Please read validation_result.txt file in {output_dir_get}")
except ExceptionWithHyperlink as e:
popup.destroy()
error_message_popup = tkinter.Toplevel()
error_message_popup.geometry("+%d+%d" %(x+120,y+100))
error_message_popup_first=tkinter.Label(error_message_popup, text=str(e))
error_message_popup_first.grid(row=0,column=0)
error_message_popup_second = tkinter.Label(error_message_popup, text=f"{dataset_docs_url}", fg="blue", cursor="hand2")
error_message_popup_second.grid(row=1,column=0)
error_message_popup_second.bind("<Button-1>", open_url)
except Exception as e:
popup.destroy()
tkinter.messagebox.showerror("Error", e)