-
Notifications
You must be signed in to change notification settings - Fork 0
/
survey.py
executable file
·212 lines (195 loc) · 6.21 KB
/
survey.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
#!/usr/bin/python
from os import *
import os
import sys
from pathlib import Path
from PIL import ImageTk
import PIL.Image
import glob
import random
import shutil
import Tkinter as tk
from Tkinter import *
import tkMessageBox
from argparse import ArgumentParser
import argparse
cur_photo, img_list, cur_id = None, None, None
root, photo_id = None, None
res_dir = './res/'
best_dir = './res/best/'
good_dir = './res/good/'
bad_dir = './res/bad/'
ok_dir = './res/ok/'
img_dir = './img/'
result_file = './result_file'
username, name = None, None
w, m, img_l, img_t = None, None, None, None
progress = None
total_cnt = 0
def build_parser():
parser = ArgumentParser()
parser.add_argument('--username', metavar='INTRA ID', type=str, nargs=1,\
help='Intra user id')
parser.add_argument('--name', metavar='NAME', type=str, nargs='+', \
help='Name')
return (parser)
def updateImage():
global cur_photo, img_list, cur_id, progress
img_list_size = len(img_list)
if (total_cnt):
percent = int((1 - img_list_size / float(total_cnt)) * 100)
else:
percent = 100
if (img_list_size):
cur_id = random.randint(0, img_list_size - 1)
cur_photo = ImageTk.PhotoImage(PIL.Image.open(img_list[cur_id]))
img_l.configure(image=cur_photo, compound=CENTER, width=600)
img_l.image = cur_photo
base_name = os.path.basename(img_list[cur_id])
img_t.configure(text="Rate Image #" + str(photo_id[cur_id]) + " : " + base_name, compound=CENTER, width=600)
w.configure(text="Press ['left' for Bad] ['down' for Ok] ['right' for Good] [<esc> to exit]", compound=CENTER, width=600)
progress.configure(text="Progress: " + str(percent) + "%", width=600)
else:
img_l.configure(image='', borderwidth=0, highlightthickness=0, width=600)
img_t.configure(text="", width=600)
w.configure(text="Completed Set! Thank You\n<esc> to exit", width=600)
m.configure(text="", width=600)
progress.configure(text="", width=600)
def movefile(press):
global img_list, photo_id
if (len(img_list)):
old_path = img_list[cur_id]
old_id = photo_id[cur_id]
base_name = os.path.basename(img_list[cur_id])
del photo_id[cur_id]
del img_list[cur_id]
if (press == 'Left'):
new_path = bad_dir + base_name
message = "Image " + str(old_id) + " moved to Bad"
m.configure(text=message)
shutil.move(old_path, new_path)
updateImage()
elif (press == 'Down'):
new_path = ok_dir + base_name
message = "Image " + str(old_id) + " moved to Ok"
m.configure(text=message)
shutil.move(old_path, new_path)
updateImage()
elif (press == 'Right'):
new_path = good_dir + base_name
message = "Image " + str(old_id) + " moved to Good"
m.configure(text=message)
shutil.move(old_path, new_path)
updateImage()
def keypress(event):
if (event.keysym == 'Escape'):
# with open(result_file, 'r') as res_file:j
if (tkMessageBox.askokcancel("Quit", "Quit? If so, a result file will be generated. Please Slack DM @achan or @dgonchar your result file,\nresult_file.result")):
cmd = 'echo "" > ' + result_file
if (username):
cmd = 'echo ' + username + ' >> ' + result_file
os.system(cmd)
if (name):
cmd = 'echo ' + name + ' >> ' + result_file
os.system(cmd)
cmd = 'ls -lR ' + res_dir + ' >> ' + result_file
os.system(cmd)
root.destroy()
else:
pass
press = event.keysym
if (press in ['Left', 'Down', 'Right']):
movefile(press)
else:
pass
def main():
global cur_photo, img_list, cur_id, photo_id, root
global m, w, img_t, img_l, progress, total_cnt, result_file
global username, name
parser = build_parser()
args = parser.parse_args()
if (len(sys.argv) > 1):
if (args.username):
user_app = '_'.join(args.username)
username = user_app
if (args.name):
name_app = '_'.join(args.name)
name = name_app
result_file += '.result'
if (os.path.exists(img_dir)):
if (not os.path.isdir(img_dir)):
print ("Unable to find img folder")
quit()
else:
print ("Unable to find img folder")
quit()
#check directory exists else make it
if (os.path.exists(res_dir)):
if (not os.path.isdir(res_dir)):
print ("Unable to setup 'res' folder")
quit()
try:
if (not os.path.exists(best_dir)):
os.mkdir(best_dir)
if (not os.path.exists(good_dir)):
os.mkdir(good_dir)
if (not os.path.exists(bad_dir)):
os.mkdir(bad_dir)
if (not os.path.exists(ok_dir)):
os.mkdir(ok_dir)
except:
print ("Unable to setup 'res' folder")
else:
try:
os.mkdir(res_dir)
os.mkdir(best_dir)
os.mkdir(bad_dir)
os.mkdir(good_dir)
os.mkdir(ok_dir)
except:
print ("Unable to setup file/folders")
quit()
img_list = glob.glob(img_dir + '*.jpg')
cur_photo = None
cur_id = None
root = tk.Tk()
root.title("Image Classifier")
root.geometry("600x700")
root.configure(bg="grey")
img_list_size = len(img_list)
total_cnt = len(img_list)
bad_list = glob.glob(bad_dir + '*.jpg')
ok_list = glob.glob(ok_dir + '*.jpg')
good_list = glob.glob(good_dir + '*.jpg')
best_list = glob.glob(best_dir + '*.jpg')
total_cnt += (len(bad_list) + len(good_list) + len(best_list) + len(ok_list))
if (not img_list_size):
w = Label(root, text="Classification of image set has been completed. Thank you for your help :)", width=600)
w.pack()
if (img_list_size):
photo_id = [i for i in range(1, img_list_size + 1)]
cur_id = random.randint(0, img_list_size - 1)
cur_photo = ImageTk.PhotoImage(PIL.Image.open(img_list[cur_id]))
img_l = Label(root, image=cur_photo, compound=CENTER, width=600)
img_l.image = cur_photo
img_l.pack()
base_name = os.path.basename(img_list[cur_id])
img_t = Label(root, text="Rate Image #" + str(photo_id[cur_id]) + " : " + base_name, compound=CENTER, width=600)
img_t.pack()
w = Label(root, text="Press ['left' for Bad] ['down' for Ok] ['right' for Good] [<esc> to exit]", compound=CENTER, width=600)
w.pack()
m = Label(root, text="", width=600)
m.pack()
if (total_cnt):
percent = int((1 - img_list_size / float(total_cnt)) * 100)
else:
percent = 100
progress = Label(root, text="Progress: " + str(percent) + "%", width=600)
progress.pack()
root.bind_all('<Key>', keypress);
root.mainloop()
else:
print ("Usage: ./survey.py --username <username> --name <name>")
return (0)
if __name__ == "__main__":
main()