-
Notifications
You must be signed in to change notification settings - Fork 0
/
toolbar.py
325 lines (241 loc) · 9.02 KB
/
toolbar.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
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 10 08:19:21 2021
@author: Ronald Kemker
"""
from tkinter import Frame, Label, Button, StringVar, OptionMenu
from PIL import ImageTk, Image, ImageOps
from navigator import Navigator
class Toolbar(object):
def __init__(self, root_app):
'''
Toolbar Class
Parameters
----------
root_app : AnnotationTool object
Pass Parent Object to access global variables
Attributes
----------
root_app : AnnotationTool object
Parent Object
toolbar_width : int
Width of the Toolbar
toolbar_height : int
Height of the Toolbar
toolbar_frame : tkinter Frame object
Fram that contains the entire Toolbar object
Raises
------
None
Returns
-------
None
'''
self.root_app = root_app
self.toolbar_width = self.root_app.window_width
self.toolbar_height = self.root_app.toolbar_height
# Tool Bar Frame
self.toolbar_frame = Frame(self.root_app.background,
bg='gray',
width=self.toolbar_width,
height=self.toolbar_height)
self.toolbar_frame.place(x=0,
y=0,
height = self.toolbar_height,
width = self.toolbar_width)
# Draw Tools
if len(self.root_app.annotations):
self._draw_image_navigator()
self._delete_from_project_button()
if len(self.root_app.class_list) > 0:
self._draw_class_selection_menu()
def _draw_image_navigator(self):
'''
Draws the image navigator inside the Toolbar Frame
Parameters
----------
None
Attributes
----------
None
Raises
------
None
Returns
-------
None
'''
button_size = 40
nav_frame = Frame(self.toolbar_frame,
width = self.root_app.navigator_width,
height = self.toolbar_height)
nav_frame.place(x=self.root_app.canvas_width-20,
y=0,
width = self.root_app.navigator_width,
height = self.toolbar_height
)
nav_text_label = Label(nav_frame,
font='Helvetica 12 bold',
justify='center',
text="Page %d/%d" % (self.root_app.page+1,
self.root_app.num_pages+1))
nav_text_label.place(x=0, y=5,
width=self.root_app.navigator_width,
height=button_size)
# Only load images when not testing
if self.root_app.window.winfo_ismapped():
left_arrow = Image.open('img/left_arrow.png')
right_arrow = ImageOps.mirror(left_arrow)
left_photo = ImageTk.PhotoImage(left_arrow.resize((button_size,
button_size),
Image.ANTIALIAS))
right_photo = ImageTk.PhotoImage(right_arrow.resize((button_size,
button_size),
Image.ANTIALIAS))
else:
left_photo = None
right_photo = None
right_button = Button(nav_frame, image=right_photo,
bg='black', command=self.next_page)
right_button.image = right_photo
right_button.place(x=150,
y=5,
width=button_size,
height=button_size)
left_button = Button(nav_frame, image=left_photo,
bg='black', command=self.previous_page)
left_button.image = left_photo
left_button.place(x=10,
y=5,
width=button_size,
height=button_size)
def next_page(self):
self.root_app.page = min(self.root_app.page + 1, self.root_app.num_pages)
self.root_app._draw_workspace()
def previous_page(self):
self.root_app.page = max(self.root_app.page - 1, 0)
self.root_app._draw_workspace()
def _next_image(self):
'''
Moves to the next image in the project
Parameters
----------
None
Attributes
----------
None
Raises
------
None
Returns
-------
None
'''
self.root_app.current_file = min(self.root_app.current_file + 1,
len(self.root_app.file_list)-1)
self.root_app._load_image_from_file()
self.root_app._draw_workspace()
def _previous_image(self):
'''
Moves to the previous image in the project
Parameters
----------
None
Attributes
----------
None
Raises
------
None
Returns
-------
None
'''
self.root_app.current_file = max(self.root_app.current_file - 1, 0)
self.root_app._load_image_from_file()
self.root_app._draw_workspace()
def _delete_from_project_button(self):
'''
Draw the Remove Image from Project Button
Parameters
----------
None
Attributes
----------
None
Raises
------
None
Returns
-------
None
'''
button_size = 40
if self.root_app.window.winfo_ismapped():
img = Image.open('img/delete.png')
photo = ImageTk.PhotoImage(img.resize((button_size-1,
button_size-1),
Image.ANTIALIAS))
else:
photo = None
button = Button(self.toolbar_frame,
image = photo,
command=self._delete_from_project,
width = button_size,
height = button_size)
button.image = photo
button.place(x=10,
y=5,
width=button_size,
height=button_size)
def _delete_from_project(self):
'''
command action tied to the "Remove from Project" button
Parameters
----------
None
Attributes
----------
None
Raises
------
None
Returns
-------
None
'''
for idx in self.root_app.annotations[self.root_app.current_file].label:
self.root_app.class_count[idx] = self.root_app.class_count[idx] - 1
self.root_app.annotations.pop(self.root_app.current_file)
self.root_app.file_list.pop(self.root_app.current_file)
if self.root_app.current_file == len(self.root_app.file_list):
self.root_app.current_file = self.root_app.current_file - 1
self.root_app._load_image_from_file()
self.root_app._draw_workspace()
def _draw_class_selection_menu(self):
'''
Draw the drop down menu that is used to select which class is being
currently annotated
Parameters
----------
None
Attributes
----------
selected_class : tkinter StringVar object
This is the variable assigned to the object class selection drop
down menu
Raises
------
None
Returns
-------
None
'''
self.root_app.selected_class = StringVar(self.toolbar_frame)
self.root_app.selected_class.set(self.root_app.class_list[0])
option_menu = OptionMenu(self.toolbar_frame, self.root_app.selected_class,
*self.root_app.class_list)
option_menu.place(x=60,
y=5,
width = 100,
height = 40)