-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectHandler.py
308 lines (245 loc) · 12.1 KB
/
ProjectHandler.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from copy import deepcopy
from glob import glob
from shutil import copyfile
from PyQt5.QtWidgets import QMessageBox
from pandas.io import pickle
from Utility import show_dialoge
import pickle
from PyQt5.QtCore import QObject, pyqtSignal
import os
import Config
import Class
class ProjectHandler(QObject):
open_project_signal = pyqtSignal(dict, bool);
set_project_name_signal = pyqtSignal(str);
new_project_setup_signal = pyqtSignal();
def __init__(self):
super().__init__();
self.__current_project_name = "";
self.__projects_root = "";
#create file to store most recent project
if not os.path.exists('mrp.uog'):
open('mrp.uog','w');
pass
@property
def current_project_name(self):
return self.__current_project_name;
@current_project_name.setter
def current_project_name(self, name):
self.__current_project_name = name;
''''
Use given path to create a folder for the new project
we assume that the name given is the name of project
'''
def new_project(self, path):
proj_name = os.path.basename(path);
#Create a folder at the given path
os.makedirs(path);
#Create all necessary folders for the new project in all folders
os.makedirs(os.path.sep.join([path,'images']));
os.makedirs(os.path.sep.join([path,'labels']));
os.makedirs(os.path.sep.join([path, 'ckpts']));
os.makedirs(os.path.sep.join([path, 'evaluation']));
os.makedirs(os.path.sep.join([path, 'experiments']));
#Set most recent project to the currently created one
self.__current_project_name = proj_name;
Config.PROJECT_NAME = self.__current_project_name;
Config.PROJECT_ROOT = path;
self.__projects_root = Config.PROJECT_ROOT;
self.new_project_setup_signal.emit();
self.save_project(False);
self.save_project_meta(list());
self.set_project_name_signal.emit(proj_name);
pass
def save_project(self, show_dialoge_bool = True):
pickle.dump(Class.data_pool_handler.data_list,open(os.path.sep.join([self.__projects_root, self.__current_project_name + '.uog']),'wb'));
#df = pd.DataFrame(list(d.items()), columns = ['image_path','state', 'type']);
#df.to_pickle();
if show_dialoge_bool:
show_dialoge(QMessageBox.Icon.Information, f"Successfully saved","Info", QMessageBox.Ok);
self.save_most_recent_project();
pass
def save_project_as(self, path):
proj_name = os.path.basename(path);
base_path = path[:path.rfind('/')];
#Create a folder at the given path
os.makedirs(path);
#Create all necessary folders for the new project in all folders
os.makedirs(os.path.sep.join([path,'images']));
os.makedirs(os.path.sep.join([path,'labels']));
os.makedirs(os.path.sep.join([path, 'ckpts']));
os.makedirs(os.path.sep.join([path, 'evaluation']));
os.makedirs(os.path.sep.join([path, 'experiments']));
#Copy project file name
copyfile(os.path.sep.join([self.__projects_root, self.__current_project_name + '.uog']),
os.path.sep.join([path,proj_name+'.uog']));
copyfile(os.path.sep.join([self.__projects_root, self.__current_project_name + '.meta']),
os.path.sep.join([path,proj_name+'.meta']));
#Copy each folder data to the destination folder
lstdir = os.listdir(os.path.sep.join([self.__projects_root,'images']));
for i in lstdir:
copyfile(os.path.sep.join([self.__projects_root,'images',i]),
os.path.sep.join([path,'images',i]))
lstdir = os.listdir(os.path.sep.join([self.__projects_root,'labels']));
for i in lstdir:
copyfile(os.path.sep.join([self.__projects_root,'labels',i]),
os.path.sep.join([path,'labels',i]))
lstdir = os.listdir(os.path.sep.join([self.__projects_root,'ckpts']));
for i in lstdir:
copyfile(os.path.sep.join([self.__projects_root,'ckpts',i]),
os.path.sep.join([path,'ckpts',i]))
lstdir = os.listdir(os.path.sep.join([self.__projects_root,'evaluation']));
for i in lstdir:
copyfile(os.path.sep.join([self.__projects_root,'evaluation',i]),
os.path.sep.join([path,'evaluation',i]))
lstdir = os.listdir(os.path.sep.join([self.__projects_root,'experiments']));
for i in lstdir:
copyfile(os.path.sep.join([self.__projects_root,'experiments',i]),
os.path.sep.join([path,'experiments',i]))
self.new_project_setup_signal.emit();
self.__current_project_name = proj_name;
Config.PROJECT_NAME = self.__current_project_name;
Config.PROJECT_ROOT = path;
self.__projects_root = Config.PROJECT_ROOT;
self.set_project_name_signal.emit(proj_name);
data_list = pickle.load(open(os.path.sep.join([path,proj_name + '.uog']),'rb'));\
self.open_project_signal.emit(data_list, False);
show_dialoge(QMessageBox.Icon.Information, f"Project successfully saved to new path","Save succeed", QMessageBox.Ok);
pass
def save_project_meta(self, meta):
pickle.dump(meta, open(os.path.sep.join([self.__projects_root, self.__current_project_name + '.meta']),'wb'));
def get_project_meta(self):
meta = pickle.load(open(os.path.sep.join([self.__projects_root,
self.__current_project_name + '.meta']),'rb'));
return meta;
def update_project_meta_slot(self, name):
m = self.get_project_meta();
if len(m) == 0:
m.append([name]);
else:
m[0].append(name);
self.save_project_meta(m);
def get_project_names_slot(self):
m = self.get_project_meta();
if len(m) != 0:
for l in m[0]:
Config.PROJECT_PREDEFINED_NAMES[0].append(l);
pass
def save_most_recent_project(self):
mrp = open('mrp.uog','w');
mrp.write(os.path.sep.join([self.__projects_root, self.__current_project_name + '.uog']));
mrp.close();
pass
def open_project(self, path = None):
self.new_project_setup_signal.emit();
#If no name given, open most recent project
if path == None:
mrp = open('mrp.uog','r');
project_path = mrp.read();
if os.path.exists(project_path):
#First clear every loaded item from previous project
self.new_project_setup_signal.emit();
proj_name = os.path.basename(project_path);
proj_name = proj_name[:proj_name.rfind('.')];
self.__current_project_name = proj_name;
self.__projects_root = project_path[:project_path.rfind('\\')];
Config.PROJECT_NAME = self.__current_project_name;
Config.PROJECT_ROOT = self.__projects_root;
data_list = pickle.load(open(project_path,'rb'));
tmp_datalist, change, unlabelled_new, labelled_new = self.__check_project(data_list);
if change is True:
msgBox = QMessageBox()
msgBox.setIcon(QMessageBox.Icon.Warning)
msg = 'During project loading, ';
if len(unlabelled_new) !=0 :
msg+= 'Found labels for these unlabelled data: ';
for u in unlabelled_new:
msg +=f'\n{u}';
msg+='\n';
if len(labelled_new) !=0:
msg+="Couldn't find labels for these images:";
for u in labelled_new:
msg +=f'\n{u}';
msg += '\nDo you want to update project base?'
msgBox.setText(msg)
msgBox.setWindowTitle("Confirmation")
msgBox.setStandardButtons(QMessageBox.Yes | QMessageBox.No);
return_value = msgBox.exec()
if return_value == QMessageBox.Yes:
data_list = deepcopy(tmp_datalist);
self.save_most_recent_project();
self.open_project_signal.emit(data_list, True);
self.set_project_name_signal.emit(proj_name);
return True;
else:
#If most recent project not found, start a new project.
return False;
else:
if os.path.exists(path):
#First clear every loaded item from previous project
self.new_project_setup_signal.emit();
data_list = pickle.load(open(path,'rb'));
#We assume that the filename is the project name
project_name = os.path.basename(path);
project_name = project_name[:project_name.rfind('.')];
self.__current_project_name = project_name;
Config.PROJECT_NAME = self.__current_project_name;
project_root = path[:path.rfind('/')];
Config.PROJECT_ROOT = project_root;
self.__projects_root = project_root;
#set this project as the most recent project
self.save_most_recent_project();
self.set_project_name_signal.emit(project_name);
self.open_project_signal.emit(data_list, True);
return True;
else:
show_dialoge(QMessageBox.Icon.Critical, f"Project couldn't be found","Error", QMessageBox.Ok);
pass
def __check_project(self, data_list):
'''
Here we check labels folder to check for images that
have not been added to datalist of the project and
ask user about them. Here, we also check if all labelled images
have a respective meta file under labels folder and all unlabelled files don't.
'''
temp_data_list = deepcopy(data_list);
img_lst = glob(os.path.sep.join([Config.PROJECT_ROOT, "images"]) + "\\*");
unlabelled_new = [];
labelled_new = [];
change = False;
for l in data_list:
path_to_meta = os.path.sep.join([Config.PROJECT_ROOT, 'labels', l[:l.rfind(".")] + ".meta"]);
if (os.path.exists(path_to_meta) and data_list[l][0] =='unlabeled'):
temp_data_list[l] = ['labeled', data_list[l][1]];
change = True;
unlabelled_new.append(l);
elif (os.path.exists(path_to_meta) is False and data_list[l][0] =='labeled'):
temp_data_list[l][0] = 'unlabeled';
change = True;
labelled_new.append(l);
return temp_data_list, change, unlabelled_new, labelled_new;
def __relod_dataset(self):
'''
Here we match every image with labels that are availabe.
We also match each label in meta file and correct any naming issues.
'''
temp_data_list = dict();
img_lst = glob(os.path.sep.join([Config.PROJECT_ROOT, "images"]) + "\\*");
for l in img_lst:
file_name = os.path.basename(l);
file_name_we = file_name[:file_name.rfind(".")];
path_to_meta = os.path.sep.join([Config.PROJECT_ROOT, 'labels', file_name_we + ".meta"]);
if (os.path.exists(path_to_meta) is False):
print(f"Could not find meta in reload dataset: {path_to_meta}");
else:
temp_data_list[file_name] = ['labeled', 'image'];
meta_data = pickle.load(open(path_to_meta, 'rb'));
for k in meta_data.keys():
if k != 'rot' and k!= 'exp':
d = meta_data[k];
name = d[2];
tmp_name = name[len(name)-6:];
new_name = file_name_we+tmp_name;
meta_data[k][2] = new_name;
pickle.dump(meta_data, open(path_to_meta, 'wb'));
return temp_data_list;