-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.py
348 lines (257 loc) · 12.3 KB
/
display.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
from enum import Enum
from tkinter import *
from tkinter import ttk
from PIL import ImageTk, Image
import os
import random
import numpy as np
from numpy.random import default_rng
from functools import partial
from datetime import datetime
def get_timestamp_string():
dt = datetime.now()
return '{}-{}-{}_{}:{}:{}'.format(dt.year,dt.month,dt.day,dt.hour,dt.minute,dt.second)
class DisplayState(Enum):
DIR_LEVEL = 0
SUBDIR_LEVEL = 1
class Display:
def __init__(self, master_dir):
self.master_dir = master_dir
self.seen = set()
name = self.master_dir.split('/')[-1]
self.filename = 'data/{}.txt'.format(name)
# create our file
try:
with open(self.filename, 'x'):
pass
except:
self.seen = self._load_from_file(self.filename)
self.state = DisplayState.DIR_LEVEL
self.directory = self._choose_random_directory(self.master_dir)
if self.directory == False:
raise RuntimeError('Directory has no available clusters!')
else:
self.seen.add(self.directory)
self.width = 600
self.height = 600
self.num_rows = 5
self.num_cols = 5
self._set_mainframe()
self._set_dirframe()
self.root.protocol('WM_DELETE_WINDOW', self._handle_close)
self.root.mainloop()
def _load_from_file(self, fname):
with open(fname) as f:
l = f.readlines()
return set([x for x in l if x[0] == '*'])
def _choose_random_directory(self, master_dir):
cluster_dirs = ['{}/{}'.format(self.master_dir, d) for d in next(os.walk(master_dir))[1]]
available = set(cluster_dirs).difference(self.seen)
if len(available) == 0: # no more available folders!
return False
return random.choice(list(available))
def _handle_close(self):
# TODO save all
self.root.destroy()
def _set_mainframe(self):
"""
Sets the root and main frame of the GUI
"""
self.root = Tk()
self.title = 'Cluster Checker'
self.root.minsize(width=self.width, height=self.height)
self.mainframe = ttk.Frame(self.root, padding="3 3 12 12")
self.mainframe.grid(column=0, row=0, sticky=NS)
def _get_files_from_dir(self, d):
return [f for f in os.listdir(d) if os.path.isfile(os.path.join(d, f))]
def _get_image_spread(self, upper_bound=25):
rng = default_rng()
ims = self._get_files_from_dir(self.directory)
self.images = ['{}/{}'.format(self.directory, im) for im in ims]
idx = rng.choice(len(ims), size=min(len(ims),upper_bound), replace=False)
return list(np.array(self.images)[idx])
def _start_eval(self, filepath):
self.model_face = filepath
self.state = DisplayState.SUBDIR_LEVEL
self.results = list()
self.root.title('Are these from the same cluster?')
self._set_subdirframe(0)
def _set_subdirframe(self, ix):
"""
Sets the subdirectory view frame of the GUI
"""
if ix >= len(self.images):
self._new_cluster()
return
imwidth = self.width
imheight = self.height
self.subdirframe = ttk.Frame(master=self.mainframe, borderwidth=2, relief=GROOVE)
self.subdirframe.grid(column=0, row=0, sticky=NSEW)
model_img = ImageTk.PhotoImage(Image.open(self.model_face).resize((imwidth, imheight)))
model = ttk.Label(master=self.subdirframe, image=model_img)
model.image = model_img
model.grid(column=0, row=0, sticky=NSEW)
comp_img = ImageTk.PhotoImage(Image.open(self.images[ix]).resize((imwidth, imheight)))
comp = ttk.Label(master=self.subdirframe, image=comp_img)
comp_img.image = comp_img
comp.grid(column=1, row=0, sticky=NSEW)
no = Button(master=self.subdirframe, text='NO', height=10, command=partial(self._no, ix))
no.grid(column=0, row=1, sticky=NSEW)
self.subdirframe.bind("n", partial(self._no, ix))
yes = Button(master=self.subdirframe, text='YES', height=10, command=partial(self._yes, ix))
yes.grid(column=1, row=1, sticky=NSEW)
self.subdirframe.bind("y", partial(self._yes, ix))
def _yes(self, ix):
self.results.append(True)
self._set_subdirframe(ix+1)
def _no(self, ix):
self.results.append(False)
self._set_subdirframe(ix+1)
def _save_eval(self, ims, res):
wrong = [x[0] for x in zip(self.images, self.results) if not x[1]]
with open(self.filename, 'a') as f:
f.write('*' + self.directory + '\n')
f.writelines('\n'.join(wrong))
f.write('\n')
def _new_cluster(self):
# SAVE WORK
self._save_eval(self.images, self.results)
self.state = DisplayState.DIR_LEVEL
self.directory = self._choose_random_directory(self.master_dir)
if self.directory == False:
print('No new clusters to classify')
self._handle_close()
return
# raise RuntimeError('Directory has no available clusters!')
else:
self.seen.add(self.directory)
self._set_dirframe()
def _set_dirframe(self):
"""
Sets the directory view frame of the GUI
"""
num_cols = self.num_cols
num_rows = self.num_rows
imwidth = self.width // num_cols
imheight = self.width // num_rows
self.root.title('Please select the image that you believe represents this cluster')
self.dirframe = ttk.Frame(master=self.mainframe, borderwidth=2, relief=GROOVE)
self.dirframe.grid(column=0, row=0, sticky='NSEW')
self.imgs = ttk.Frame(master=self.dirframe, borderwidth=1)
self.imgs.grid(column=0, columnspan=num_cols, row=0, rowspan=num_rows, sticky=NSEW)
impaths = self._get_image_spread(upper_bound = num_cols * num_rows)
fdict = {}
for row in range(num_rows):
for col in range(num_cols):
ix = int(row * num_rows + col)
if ix >= len(impaths):
setattr(self, 'grid_{}{}'.format(row, col), ttk.Button(master=self.imgs))
getattr(self, 'grid_{}{}'.format(row, col)).grid(column=col, columnspan=1, row=row, rowspan=1, sticky=NSEW)
else:
img_path = impaths[ix]
image = ImageTk.PhotoImage(Image.open(img_path).resize((imwidth, imheight)))
setattr(self, 'grid_{}{}'.format(row, col), ttk.Button(master=self.imgs, image=image, command=partial(self._start_eval, img_path)))
getattr(self, 'grid_{}{}'.format(row, col)).grid(column=col, columnspan=1, row=row, rowspan=1, sticky=NSEW)
getattr(self, 'grid_{}{}'.format(row, col)).image=image
class MetaDisplay:
def __init__(self, cluster_interface, trust=75):
self.ci = cluster_interface
self.trust = trust
self.width = 600
self.height = 600
self.num_rows = 5
self.num_cols = 5
self._set_mainframe()
self._set_subdirframe()
self.root.protocol('WM_DELETE_WINDOW', self._handle_close)
self.root.mainloop()
def _choose_random_directory(self, master_dir):
cluster_dirs = ['{}/{}'.format(self.master_dir, d) for d in next(os.walk(master_dir))[1]]
available = set(cluster_dirs).difference(self.seen)
if len(available) == 0: # no more available folders!
return False
return random.choice(list(available))
def _handle_close(self):
try:
os.mkdir('metadata')
except:
pass
t = f'metadata/{get_timestamp_string()}'
self.ci.save(t)
print('Saved to {}.json'.format(t))
self.root.destroy()
def _set_mainframe(self):
"""
Sets the root and main frame of the GUI
"""
self.root = Tk()
self.title = 'Cluster Checker'
self.root.minsize(width=self.width, height=self.height)
self.mainframe = ttk.Frame(self.root, padding="3 3 12 12")
self.mainframe.grid(column=0, row=0, sticky=NS)
def _set_subdirframe(self):
"""
Sets the subdirectory view frame of the GUI
"""
check_self = random.randint(0, 100) > self.trust
c = self.ci.clusters
self.ci.set_potency()
self.root.title('Do these belong to the same cluster? Potency: {}'.format(self.ci.potency))
if check_self:
cluster,_ = self.ci.suggest_intra_pairing()
imwidth = self.width
imheight = self.height
im1,im2 = self.ci.get_node_name_from_cluster(cluster),self.ci.get_node_name_from_cluster(cluster)
self.subdirframe = ttk.Frame(master=self.mainframe, borderwidth=2, relief=GROOVE)
self.subdirframe.grid(column=0, row=0, sticky=NSEW)
model_img = ImageTk.PhotoImage(Image.open(im1).resize((imwidth, imheight)))
model = ttk.Label(master=self.subdirframe, image=model_img)
model.image = model_img
model.grid(column=0, row=0, sticky=NSEW)
comp_img = ImageTk.PhotoImage(Image.open(im2).resize((imwidth, imheight)))
comp = ttk.Label(master=self.subdirframe, image=comp_img)
comp_img.image = comp_img
comp.grid(column=1, row=0, sticky=NSEW)
no = Button(master=self.subdirframe, text='NO', height=10, command=partial(self.ci.problem_with_cluster, cluster, im1, im2, self._set_subdirframe))
no.grid(column=0, row=1, sticky=NSEW)
self.root.bind('n', lambda event: self.ci.problem_with_cluster(cluster, im1, im2, self._set_subdirframe))
yes = Button(master=self.subdirframe, text='YES', height=10, command=self._set_subdirframe)
yes.grid(column=1, row=1, sticky=NSEW)
self.root.bind('y', lambda event: self._set_subdirframe())
else:
pairing = self.ci.suggest_pairing()
if pairing is False:
self.subdirframe = ttk.Frame(master=self.mainframe, borderwidth=2, relief=GROOVE)
self.subdirframe.grid(column=0,row=0,sticky=NSEW)
self.label = ttk.Label(master=self.subdirframe, text='No more available clusters. Please exit, (this will save automatically).')
self.label.grid(column=0,row=0,sticky=NSEW)
else:
im1,im2 = pairing
imwidth = self.width
imheight = self.height
self.subdirframe = ttk.Frame(master=self.mainframe, borderwidth=2, relief=GROOVE)
self.subdirframe.grid(column=0, row=0, sticky=NSEW)
model_img = ImageTk.PhotoImage(Image.open(self.ci.get_node_name_from_cluster(im1)).resize((imwidth, imheight)))
model = ttk.Label(master=self.subdirframe, image=model_img)
model.image = model_img
model.grid(column=0, row=0, sticky=NSEW)
comp_img = ImageTk.PhotoImage(Image.open(self.ci.get_node_name_from_cluster(im2)).resize((imwidth, imheight)))
comp = ttk.Label(master=self.subdirframe, image=comp_img)
comp_img.image = comp_img
comp.grid(column=1, row=0, sticky=NSEW)
no = Button(master=self.subdirframe, text='NO', height=10, command=partial(self.ci.is_bad_pairing, im1, im2, self._set_subdirframe))
no.grid(column=0, row=1, sticky=NSEW)
self.root.bind('n', lambda event: self.ci.is_bad_pairing(im1, im2, self._set_subdirframe))
yes = Button(master=self.subdirframe, text='YES', height=10, command=partial(self.ci.is_good_pairing, im1, im2, self._set_subdirframe))
yes.grid(column=1, row=1, sticky=NSEW)
self.root.bind('y', lambda event: self.ci.is_good_pairing(im1, im2, self._set_subdirframe))
if __name__ == "__main__":
# root = Tk()
# canvas = Canvas(root, width = 300, height = 300)
# canvas.pack()
# img = ImageTk.PhotoImage(Image.open("ball.png"))
# canvas.create_image(20, 20, anchor=NW, image=img)
# root.mainloop()
master = '/Users/michaelhiebert/Development/clustercheck/data/test'
d = Display(master)
print('hello')