-
Notifications
You must be signed in to change notification settings - Fork 0
/
labeler_PETA.py
133 lines (105 loc) · 4.05 KB
/
labeler_PETA.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
# img_viewer.py
from layout import *
import os.path
import logging
from utils import *
# Level of warnings
logging.basicConfig(format='[%(levelname)s]: %(message)s', level=logging.CRITICAL)
# Dictionary of labels
labels = {}
# idx to an element in the folder
idx = 0
# Run the Event Loop
def update_img(filename, window):
'''
Update the frame where the image is shown
input: name of the updated file
window where the update is done
output: image window updated
'''
window["-TOUT-"].update(filename)
window["-IMAGE-"].update(data=get_img_data(filename))
while True:
event, values = window.read()
if event == "Exit" or event == sg.WIN_CLOSED:
break
# Folder name was filled in, make a list of files in the folder
if event == "-FOLDER-":
folder = values["-FOLDER-"]
try:
# Get list of files in folder
file_list = os.listdir(folder)
except:
file_list = []
fnames = [
f
for f in file_list
if os.path.isfile(os.path.join(folder, f))
and f.lower().endswith((".png", ".jpg"))
]
filename = os.path.join(
values["-FOLDER-"], fnames[idx]
)
#attributes={fileName:[] for fileName in fnames}
logging.info('Filename at START: {}'.format(filename))
update_img(filename, window)
elif event == 'Next':
try:
logging.info('Filename before NEXT: {}'.format(filename))
logging.info('Values of values_array: {}'.format(values))
attributes=pre_process_dict(values, fnames[idx])
logging.info("Attributes after process in Next: {}".format(attributes))
append_row(attributes, idx)
logging.info("Dataframe df attributes: \n {}".format(df_attributes))
idx += 1
#prueba
#print('Estos son los values: {}'.format(values))
#print('Estos son los values despues: {}'.format(values))
filename = os.path.join(values["-FOLDER-"], fnames[idx])
update_img(filename, window)
logging.info("Values before put them by default: {}".format(values))
try:
row_dict = extract_row(idx)
for key in row_dict:
window[key].update(row_dict[key])
except:
for key in values:
if key!= '-FOLDER-' and key!= 'Browse':
window[key].update(False)
logging.info("Values after put them by befault: {}".format(values))
except:
pass
elif event == 'Back':
# try:
logging.info('Filename before BACK: {}'.format(filename))
logging.info('Values of values_array: {}'.format(values))
attributes=pre_process_dict(values, fnames[idx])
logging.info("Attributes after process in Back: {}".format(attributes))
append_row(attributes, idx)
logging.info("Dataframe df attributes: \n {}".format(df_attributes))
idx -= 1
filename = os.path.join(values["-FOLDER-"], fnames[idx])
update_img(filename, window)
#TODO Extract the data from the dataframe and update values_array
row_dict = extract_row(idx)
for key in row_dict:
window[key].update(row_dict[key])
# except:
# pass
elif event =='Save':
attributes=pre_process_dict(values, fnames[idx])
append_row(attributes, idx)
df_attributes.to_csv('test.txt', index=False, header=False, sep=' ')
try:
logging.critical('Index value: {}'.format(idx))
if (idx+1) > (len(fnames)-1):
window['Next'].update(disabled=True)
elif (idx-1) < 0:
window['Back'].update(disabled=True)
else:
window['Save'].update(disabled=False)
window['Next'].update(disabled=False)
window['Back'].update(disabled=False)
except:
pass
window.close()