-
Notifications
You must be signed in to change notification settings - Fork 8
/
aimodel_setting_screen.py
274 lines (251 loc) · 11.5 KB
/
aimodel_setting_screen.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import datetime
import glob
import os
import pickle
import re
import shutil
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivymd.app import MDApp
from kivymd.toast import toast
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.screen import MDScreen
class AimodelSettingScreen(MDScreen):
def __init__(self, **kwargs):
super(AimodelSettingScreen, self).__init__(**kwargs)
self.app = MDApp.get_running_app()
self.save_aimodel_popup = None
self.settings_list = []
self.inspection_dict = None
self.pickle_path = None
def on_pre_enter(self):
self.clear_text()
self.disp_aimodel_list()
def get_current_inspection_dict(self):
inspections_dict = None
target_dir = "./adfi_client_app_data/current_inspection"
path_list = glob.glob(target_dir + "/*.pkl")
if len(path_list) > 0:
self.pickle_path = path_list[0]
with open(self.pickle_path, "rb") as f:
inspections_dict = pickle.load(f)
return inspections_dict
def disp_aimodel_list(self):
self.ids["name_0"].text = "No Data"
self.inspection_dict = self.get_current_inspection_dict()
if self.inspection_dict is not None:
self.settings_list = self.inspection_dict["PREPROCESSING_LIST"]
if len(self.settings_list) > 0:
for count in range(len(self.settings_list)):
settings_dict = self.settings_list[count]
self.ids["checkbox_" + str(count)].disabled = False
self.ids["name_" + str(count)].text = str(settings_dict["NAME"])
self.ids["camera_num_" + str(count)].text = str(
settings_dict["CAMERA_NUM"]
)
if "LOCAL" in settings_dict and settings_dict["LOCAL"]:
self.ids["plan_" + str(count)].text = self.app.textini[
self.app.lang
]["as_local_plan"]
if "MODEL_PATH" in settings_dict:
self.ids["api_key_" + str(count)].text = str(
settings_dict["MODEL_PATH"]
)
else:
self.ids["api_key_" + str(count)].text = ""
else:
self.ids["plan_" + str(count)].text = self.app.textini[
self.app.lang
]["as_online_plan"]
if "API_KEY" in settings_dict:
self.ids["api_key_" + str(count)].text = str(
settings_dict["API_KEY"]
)
else:
self.ids["api_key_" + str(count)].text = ""
if "MODEL_ID" in settings_dict:
self.ids["model_id_" + str(count)].text = str(
settings_dict["MODEL_ID"]
)
else:
self.ids["model_id_" + str(count)].text = ""
if "MODEL_TYPE" in settings_dict:
self.ids["model_type_" + str(count)].text = str(
settings_dict["MODEL_TYPE"]
)
else:
self.ids["model_type_" + str(count)].text = ""
count += 1
if count > 9:
break
def clear_text(self):
for i in range(10):
self.ids["checkbox_" + str(i)].active = False
self.ids["checkbox_" + str(i)].disabled = True
self.ids["name_" + str(i)].text = ""
self.ids["camera_num_" + str(i)].text = ""
self.ids["api_key_" + str(i)].text = ""
self.ids["model_id_" + str(i)].text = ""
self.ids["model_type_" + str(i)].text = ""
self.ids["plan_" + str(i)].text = ""
def get_checkbox_num(self):
for i in range(10):
if self.ids["checkbox_" + str(i)].active:
return i
return -1
def show_save_aimodel_popup(self):
checkbox_num = self.get_checkbox_num()
if checkbox_num < 0:
toast(self.app.textini[self.app.lang]["as_toast_message_no_check"])
else:
self.save_aimodel_popup = Popup(
title=self.app.textini[self.app.lang]["adfi_app_name"],
content=SaveAimodelContent(
popup_close=self.save_aimodel_popup_close,
save_aimodel=self.save_aimodel,
),
size_hint=(0.4, 0.4),
)
self.save_aimodel_popup.open()
def show_save_local_aimodel_popup(self):
if not os.path.isfile("./adfi_local/adfi.py"):
toast(self.app.textini[self.app.lang]["as_toast_message_no_adfi"])
else:
checkbox_num = self.get_checkbox_num()
if checkbox_num < 0:
toast(self.app.textini[self.app.lang]["as_toast_message_no_check"])
else:
self.save_local_aimodel_popup = Popup(
title=self.app.textini[self.app.lang]["adfi_app_name"],
content=SaveLocalAimodelContent(
popup_close=self.save_local_aimodel_popup_close,
save_aimodel=self.save_local_aimodel,
),
size_hint=(0.6, 0.25),
)
self.save_local_aimodel_popup.open()
def save_aimodel_popup_close(self):
self.save_aimodel_popup.dismiss()
def save_local_aimodel_popup_close(self):
self.save_local_aimodel_popup.dismiss()
def save_aimodel(self, api_key, model_id, model_type):
checkbox_num = self.get_checkbox_num()
if checkbox_num < 0:
toast(self.app.textini[self.app.lang]["as_toast_message_no_check"])
else:
if self._validate_aimodel(api_key, model_id, model_type):
setting_dict = self.settings_list[int(checkbox_num)]
setting_dict.update(
{
"API_KEY": api_key,
"MODEL_ID": model_id,
"MODEL_TYPE": model_type,
"LOCAL": False,
"MODEL_PATH": "",
}
)
self.settings_list[int(checkbox_num)] = setting_dict
self.inspection_dict["PREPROCESSING_LIST"] = self.settings_list
if self.pickle_path is not None:
with open(self.pickle_path, "wb") as f:
pickle.dump(self.inspection_dict, f)
to_dir = "./adfi_client_app_data/inspection_data"
shutil.copy2(self.pickle_path, to_dir)
toast(self.app.textini[self.app.lang]["as_toast_message_save"])
self.disp_aimodel_list()
self.save_aimodel_popup_close()
def save_local_aimodel(self, model_path):
checkbox_num = self.get_checkbox_num()
if checkbox_num < 0:
toast(self.app.textini[self.app.lang]["as_toast_message_no_check"])
else:
if self._validate_local_aimodel(model_path):
setting_dict = self.settings_list[int(checkbox_num)]
setting_dict.update(
{
"MODEL_PATH": model_path,
"LOCAL": True,
"API_KEY": "",
"MODEL_ID": "",
"MODEL_TYPE": "",
}
)
self.settings_list[int(checkbox_num)] = setting_dict
self.inspection_dict["PREPROCESSING_LIST"] = self.settings_list
if self.pickle_path is not None:
with open(self.pickle_path, "wb") as f:
pickle.dump(self.inspection_dict, f)
to_dir = "./adfi_client_app_data/inspection_data"
shutil.copy2(self.pickle_path, to_dir)
toast(self.app.textini[self.app.lang]["as_toast_message_save"])
self.disp_aimodel_list()
self.save_local_aimodel_popup_close()
def _validate_aimodel(self, api_key, model_id, model_type):
re_compile = re.compile(r"^[a-zA-Z0-9_-]+$")
flg = True
if len(api_key) > 40:
flg = False
toast(self.app.textini[self.app.lang]["as_toast_error_message_max_len_key"])
elif len(api_key) > 0 and re_compile.match(api_key) is None:
flg = False
toast(self.app.textini[self.app.lang]["as_toast_error_message_ascii_key"])
if len(model_id) > 40:
flg = False
toast(self.app.textini[self.app.lang]["as_toast_error_message_max_len_id"])
elif len(model_id) > 0 and re_compile.match(model_id) is None:
flg = False
toast(self.app.textini[self.app.lang]["as_toast_error_message_ascii_id"])
if len(model_type) > 3:
flg = False
toast(
self.app.textini[self.app.lang]["as_toast_error_message_max_len_type"]
)
elif len(model_type) > 0 and re_compile.match(model_type) is None:
flg = False
toast(self.app.textini[self.app.lang]["as_toast_error_message_ascii_type"])
return flg
def _validate_local_aimodel(self, model_path):
flg = True
if model_path == "":
return flg
if not os.path.isfile(model_path):
flg = False
toast(
self.app.textini[self.app.lang]["as_toast_error_message_no_model_file"]
)
valid_extensions = ["pca_model", "dml_model", "hr_model"]
file_extension = model_path.split(".")[-1]
if (file_extension in valid_extensions) is False:
flg = False
toast(self.app.textini[self.app.lang]["as_toast_error_message_not_model"])
return flg
class SaveAimodelContent(MDBoxLayout):
popup_close = ObjectProperty(None)
save_aimodel = ObjectProperty(None)
def __init__(self, **kwargs):
super(SaveAimodelContent, self).__init__(**kwargs)
self.app = MDApp.get_running_app()
self.screen = self.app.sm.get_screen("aimodel_setting")
self.check_num = self.screen.get_checkbox_num()
self.ids["api_key"].text = self.screen.ids[
"api_key_" + str(self.check_num)
].text
self.ids["model_id"].text = self.screen.ids[
"model_id_" + str(self.check_num)
].text
self.ids["model_type"].text = self.screen.ids[
"model_type_" + str(self.check_num)
].text
class SaveLocalAimodelContent(MDBoxLayout):
popup_close = ObjectProperty(None)
save_aimodel = ObjectProperty(None)
def __init__(self, **kwargs):
super(SaveLocalAimodelContent, self).__init__(**kwargs)
self.app = MDApp.get_running_app()
self.screen = self.app.sm.get_screen("aimodel_setting")
self.check_num = self.screen.get_checkbox_num()
self.ids["model_path"].text = self.screen.ids[
"api_key_" + str(self.check_num)
].text