-
Notifications
You must be signed in to change notification settings - Fork 2
/
annotation-toolbox.py
146 lines (108 loc) · 4.27 KB
/
annotation-toolbox.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import os
from tkinter import *
from drawing import Map
# Inizializza la mappa
tkmaster = Tk(className="annotation-toolbox")
map = Map(tkmaster, scale=18, width=400, height=400, bg="#FFFFFF")
map.pack(expand=True, fill="both", side="right")
global args
########################################################################################################################
# LEGEND
########################################################################################################################
def show_legend():
map.clear_log()
map.log(txt="0:\tDataset loading\n")
map.log(txt="L:\tShow legend\n")
map.log(txt="1:\tPositive (hand with product)\n")
map.log(txt="2:\tNegative (other)\n")
map.log(txt="3:\tNeutral (only hand)\n")
map.log(txt="4:\tSkip\n")
map.log(txt="Esc:\tExit\n")
########################################################################################################################
########################################################################################################################
# FUNCTIONS #
########################################################################################################################
def load_dataset(event):
map.clear_log()
map.log(txt=">> 1: Dataset loading\n\n")
global images, dataset
images = [k for k in sorted(os.listdir(os.path.join(args.images))) if '.jpg' in k]
dataset = []
try:
with open(args.data, 'rb') as features:
data = features.readlines()
for i, line in enumerate(data):
if (i != 0):
dataset.append(line.decode().split(',')[-2].split('/')[-1])
features.close()
except FileNotFoundError:
map.log(txt="Wrong data file or file path!\n")
dataset = []
f = open(args.data, 'a')
f.write('images' + ',' + 'target' + '\n')
f.close()
images = [x for x in images if x not in dataset]
map.log(txt="Data Loaded.\n")
show_image()
def show_image():
map.log(txt=">> {0}\n".format(images[0]))
map.draw_image(os.path.join(args.images, images[0]))
def legend(event):
show_legend()
pass
def positive(event):
f = open(args.data, 'a')
f.write(os.path.join(args.images, images[0]) + ',' + 'positive' + '\n')
f.close()
del images[0]
map.clear_log()
map.log(txt=">> 1: {0}: Positive\n\n".format(images[0]))
show_image()
def negative(event):
f = open(args.data, 'a')
f.write(os.path.join(args.images, images[0]) + ',' + 'negative' + '\n')
f.close()
del images[0]
map.clear_log()
map.log(txt=">> 2: {0}: Negative\n\n".format(images[0]))
show_image()
def neutral(event):
f = open(args.data, 'a')
f.write(os.path.join(args.images, images[0]) + ',' + 'neutral' + '\n')
f.close()
del images[0]
map.clear_log()
map.log(txt=">> 3: {0}: Neutral\n\n".format(images[0]))
show_image()
def skip(event):
f = open(args.data, 'a')
f.write(os.path.join(args.images, images[0]) + ',' + 'skip' + '\n')
f.close()
del images[0]
map.clear_log()
map.log(txt=">> 4: {0}: Skip\n\n".format(images[0]))
show_image()
def close(event):
tkmaster.withdraw() # if you want to bring it back
sys.exit() # if you want to exit the entire thing
########################################################################################################################
def main():
p = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description="")
p.add_argument('-images', dest='images', action='store', default='images', help='images path file *.png')
p.add_argument('-data', dest='data', action='store', default='data.csv', help='data path file *.csv')
global args
args = p.parse_args()
show_legend()
tkmaster.bind("0", load_dataset)
tkmaster.bind("l", legend)
tkmaster.bind("1", positive)
tkmaster.bind("2", negative)
tkmaster.bind("3", neutral)
tkmaster.bind("4", skip)
tkmaster.bind('<Escape>', close)
mainloop()
if __name__ == '__main__':
main()