forked from puzzledqs/BBox-Label-Tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
756 lines (632 loc) · 30.3 KB
/
main.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#-------------------------------------------------------------------------------
# Name: Object bounding box label tool
# Purpose: Label object bboxes for ImageNet Detection data
# Author: Qiushi
# Created: 06/06/2014
#
#-------------------------------------------------------------------------------
from __future__ import division
# from Tkinter import *
from tkinter import *
from tkinter import messagebox
from PIL import Image, ImageTk
import os, shutil, glob, random, sys
# colors for the bboxes
COLORS = ['red', 'blue', 'pink', 'cyan', 'green', 'yellow', 'black' ]
# image sizes for the examples
# SIZE = 256, 256
CLASSES_FILE_PATH = './classes.txt'
class LabelTool():
def __init__(self, master):
# set up the main frame
self.parent = master
self.parent.title("LabelTool")
self.frame = Frame(self.parent)
self.frame.pack(fill=BOTH, expand=1)
self.parent.resizable(width = True, height = True)
self.hivedir = './thehive'
self.hivecount = 0
# initialize global state
self.imageDir = ''
self.imageList= []
self.egDir = ''
self.egList = []
self.currLayoutRow = -1
# self.outDir = ''
# self.darknetOutDir = ''
self.cur = 0
self.total = 0
self.category = 0
self.imagename = ''
self.labelfilename = ''
self.tkimg = None
self.orderImageList = BooleanVar()
self.orderImageList.set(True)
self.noBboxes = BooleanVar()
self.hideOtherBboxes = BooleanVar()
self.bboxSelector = BooleanVar()
# initialize mouse state
self.STATE = {}
self.STATE['click'] = 0
self.STATE['x'], self.STATE['y'] = 0, 0
# reference to bbox
self.bboxIdList = []
self.bboxId = None
self.bboxList = []
self.classLabelIdList = []
self.hl = None
self.vl = None
# ----------------- GUI stuff ---------------------
# dir entry & load
self.label = Label(self.frame, text = "Image Dir:")
self.label.grid(row = 0, column = 0, sticky = E)
self.entry = Entry(self.frame)
self.entry.grid(row = 0, column = 1, sticky = W+E)
self.entry.insert(END, '1')
self.ldBtn = Button(self.frame, text = "Load", command = self.loadDir)
self.ldBtn.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+E)
self.checkbutton = Checkbutton(self.frame, text = "Order Image list", variable = self.orderImageList)
self.checkbutton.grid(row = self.getNextLayoutRow(), column = 2, sticky = W)
# main panel for labeling
self.mainPanel = Canvas(self.frame, cursor='tcross')
self.mainPanel.bind("<Button-1>", self.mouseClick)
self.mainPanel.bind("<Motion>", self.mouseMove)
self.parent.bind("<MouseWheel>", self.mouseWheel) #windows
self.parent.bind("<Button-4>", self.mouseWheel) #linux
self.parent.bind("<Button-5>", self.mouseWheel) #linux
self.parent.bind("<Key>", self.keyPressed)
self.parent.bind("<Escape>", self.cancelBBox) # press <Espace> to cancel current bbox
self.parent.bind("s", self.cancelBBox)
self.parent.bind("a", self.prevImage) # press 'a' to go backforward
self.parent.bind("d", self.nextImage) # press 'd' to go forward
self.parent.bind("<Left>", self.prevImage)
self.parent.bind("<Right>", self.nextImage)
self.parent.bind("z", self.deleteLastBBox) #delete the last bbox from the list
# self.parent.bind("h", self.sendToTheHive)
self.parent.bind("v", self.nextBBoxOverlap)
self.parent.bind("p", self.toggleNoBboxes)
self.parent.bind("f", self.toggleHideOtherBboxes)
self.parent.bind("r", self.toggleBboxSelector)
self.parent.bind("m", self.setManualBBox)
self.parent.bind("o", self.nextNoBboxImage)
self.parent.bind("x", self.delBBox)
self.mainPanel.grid(row = 1, column = 1, rowspan = 10, sticky = W+N)
# showing bbox info & delete bbox
self.lbCurrClass = Label(self.frame, text = 'New Annot as: None')
self.lbCurrClass.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+N)
self.lb1 = Label(self.frame, text = 'Bounding boxes:')
self.lb1.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+N)
self.checkbuttonNobboxes = Checkbutton(self.frame, text = "No bboxes", variable = self.noBboxes)
self.checkbuttonNobboxes.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+N)
self.listbox = Listbox(self.frame, width = 25, height = 12)
self.listbox.grid(row = self.getNextLayoutRow(), column = 2, sticky = N)
self.listbox.bind('<<ListboxSelect>>', self.listboxSelected)
self.btnSetAllBboxClasses = Button(self.frame, text='Update Classes', command = self.setAllBboxClasses)
self.btnSetAllBboxClasses.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+E+N)
self.lbClassesList = Label(self.frame, text = 'Class List:')
self.lbClassesList.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+N)
self.listboxClasses = Listbox(self.frame, width = 22, height = 8)
self.listboxClasses.grid(row = self.getNextLayoutRow(), column = 2, sticky = N)
self.listboxClasses.bind('<<ListboxSelect>>', self.listboxClassesSelected)
self.checkbHideOthersBboxes = Checkbutton(self.frame, text = "Hide other bboxes", variable = self.hideOtherBboxes)
self.checkbHideOthersBboxes.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+N)
self.checkbBboxSelector = Checkbutton(self.frame, text = "Bbox selector", variable = self.bboxSelector)
self.checkbBboxSelector.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+N)
self.setManualBBoxEntry = Entry(self.frame, width = 20)
self.setManualBBoxEntry.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+N)
self.btnSetManualBbox = Button(self.frame, text='Manual BBox', command = self.setManualBBox)
self.btnSetManualBbox.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+E+N)
self.btnNextNoBBox = Button(self.frame, text='Next No-BBox', command = self.nextNoBboxImage)
self.btnNextNoBBox.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+E+N)
self.btnDel = Button(self.frame, text = 'Delete', command = self.delBBox)
self.btnDel.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+E+N)
self.btnClear = Button(self.frame, text = 'ClearAll', command = self.clearBBox)
self.btnClear.grid(row = self.getNextLayoutRow(), column = 2, sticky = W+E+N)
# control panel for image navigation
self.ctrPanel = Frame(self.frame)
self.ctrPanel.grid(row = self.getNextLayoutRow(), column = 1, columnspan = 2, sticky = W+E)
self.prevBtn = Button(self.ctrPanel, text='<< Prev', width = 10, command = self.prevImage)
self.prevBtn.pack(side = LEFT, padx = 5, pady = 3)
self.nextBtn = Button(self.ctrPanel, text='Next >>', width = 10, command = self.nextImage)
self.nextBtn.pack(side = LEFT, padx = 5, pady = 3)
self.progLabel = Label(self.ctrPanel, text = "Progress: / ")
self.progLabel.pack(side = LEFT, padx = 5)
self.tmpLabel = Label(self.ctrPanel, text = "Go to (Frame Nr or Img Name)")
self.tmpLabel.pack(side = LEFT, padx = 5)
self.idxEntry = Entry(self.ctrPanel, width = 5)
self.idxEntry.pack(side = LEFT)
self.goBtn = Button(self.ctrPanel, text = 'Go', command = self.gotoImage)
self.goBtn.pack(side = LEFT)
# display mouse position
self.disp = Label(self.ctrPanel, text='')
self.disp.pack(side = RIGHT)
self.frame.columnconfigure(1, weight = 1)
self.frame.rowconfigure(4, weight = 1)
self.classes = self.loadClasses(CLASSES_FILE_PATH)
self.currDefaultBboxClassId = 0
self.updateMarkingClass(self.currDefaultBboxClassId)
self.topViewLine = None
self.smallLine = None
# self.mainPanel.create_line(15, 25, 200, 25)
def iou(self, boxA, boxB):
# https://www.pyimagesearch.com/2016/11/07/intersection-over-union-iou-for-object-detection/
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
# compute the area of intersection rectangle
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = interArea / float(boxAArea + boxBArea - interArea)
# return the intersection over union value
return iou
def mouseWheel(self, event):
if event.num == 5 or event.delta == -120:
self.prevImage()
if event.num == 4 or event.delta == 120:
self.nextImage()
def getNextLayoutRow(self):
self.currLayoutRow += 1
return self.currLayoutRow
def loadDir(self, dbg = False):
if not dbg:
s = self.entry.get()
self.parent.focus()
self.category = int(s)
else:
s = r'D:\workspace\python\labelGUI'
# if not os.path.isdir(s):
# tkMessageBox.showerror("Error!", message = "The specified dir doesn't exist!")
# return
# get image list
self.imageDir = os.path.join(r'./Images', '%03d' %(self.category))
#self.imageList = glob.glob(os.path.join(self.imageDir, '**/*.jpg'), recursive=True)
import fnmatch
self.imageList = []
for root, dirnames, filenames in os.walk(self.imageDir):
for filename in fnmatch.filter(filenames, '*.jpg'):
self.imageList.append(os.path.join(root, filename))
if len(self.imageList) == 0:
print('No .jpg images found in the specified dir!')
return
if(self.orderImageList.get()):
self.imageList.sort()
# print(self.imageList)
# default to the 1st image in the collection
self.cur = 1
self.total = len(self.imageList)
# set up output dir
self.labelsdir = './Labels'
if not os.path.exists(self.labelsdir):
os.mkdir(self.labelsdir)
# self.outDir = os.path.join(self.labelsdir, '%03d' %(self.category))
# if not os.path.exists(self.outDir):
# os.mkdir(self.outDir)
# darknet labels dir
self.darknetdir = './Labels-darknet'
if not os.path.exists(self.darknetdir):
os.mkdir(self.darknetdir)
# self.darknetOutDir = os.path.join(self.darknetdir, '%03d' %(self.category))
# if not os.path.exists(self.darknetOutDir):
# os.mkdir(self.darknetOutDir)
# darknet labels dir
# self.metadir = './meta'
# if not os.path.exists(self.metadir):
# os.mkdir(self.metadir)
# # load example bboxes
# self.egDir = os.path.join(r'./Examples', '%03d' %(self.category))
# if not os.path.exists(self.egDir):
# return
# filelist = glob.glob(os.path.join(self.egDir, '*.jpg'))
# self.tmp = []
# self.egList = []
# random.shuffle(filelist)
# for (i, f) in enumerate(filelist):
# if i == 3:
# break
# im = Image.open(f)
# r = min(SIZE[0] / im.size[0], SIZE[1] / im.size[1])
# new_size = int(r * im.size[0]), int(r * im.size[1])
# self.tmp.append(im.resize(new_size, Image.ANTIALIAS))
# self.egList.append(ImageTk.PhotoImage(self.tmp[-1]))
# self.egLabels[i].config(image = self.egList[-1], width = SIZE[0], height = SIZE[1])
self.loadImage()
print('%d images loaded from %s' %(self.total, s))
def toggleNoBboxes(self, event=None):
self.noBboxes.set( not self.noBboxes.get() )
def toggleHideOtherBboxes(self, event=None):
self.saveImage()
self.hideOtherBboxes.set( not self.hideOtherBboxes.get() )
self.loadImage()
def toggleBboxSelector(self, event=None):
self.bboxSelector.set(not self.bboxSelector.get())
if self.bboxSelector.get():
self.mainPanel.config(cursor='arrow')
else:
self.mainPanel.config(cursor='tcross')
def setManualBBox(self, event=None):
tmp = [int(t.strip()) for t in self.setManualBBoxEntry.get().split()]
if(len(tmp) == 4):
tmp.append(self.currDefaultBboxClassId)
self.bboxList.append(tuple(tmp))
tmpId = self.mainPanel.create_rectangle(tmp[0], tmp[1], \
tmp[2], tmp[3], \
width = 1, \
outline = COLORS[(len(self.bboxList)-1) % len(COLORS)])
self.bboxIdList.append(tmpId)
textId = self.mainPanel.create_text(tmp[0]+1, tmp[1]+7, activefill='#000fff000', fill='#fff', anchor=W, text=str(self.currDefaultBboxClassId+1))
self.classLabelIdList.append(textId)
self.listbox.insert(END, '%d:(%d,%d,%d,%d)[%d]' %(len(self.bboxIdList), tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]+1))
self.listbox.itemconfig(len(self.bboxList) - 1, fg = COLORS[(len(self.bboxList) - 1) % len(COLORS)])
def keyPressed(self, event=None):
key = event.char
#handle class bindings
class_keys = [str(x) for x in range(1,11)]
if key in class_keys:
self.setClassPressed(class_keys.index(key))
def listboxSelected(self, event):
selections = self.listbox.curselection()
if len(selections) == 1:
currBboxId = int(selections[0])
classId = self.bboxList[currBboxId][4]
self.reloadClassList(classId)
elif len(selections) > 1:
raise Exception("We do not support multi-selection")
else:
#deselecting
self.reloadClassList()
def listboxClassesSelected(self, event):
selections = self.listboxClasses.curselection()
if len(selections) == 1:
self.updateMarkingClass(int(self.listboxClasses.curselection()[0]))
elif len(selections) > 1:
raise Exception("We do not support multi-selection")
else:
#deselecting
self.reloadClassList()
def updateMarkingClass(self, classId):
self.currDefaultBboxClassId = classId
# print('Changing current class for annotations to', classId)
self.lbCurrClass.config(text='New Annot as: {}'.format(self.classes[classId]))
def setClassPressed(self, classId):
if classId >= 0 and classId < self.listboxClasses.size() and self.listbox.size() > 0:
self.updateMarkingClass(classId)
self.setBBoxClass(classId)
def reloadClassList(self, class_id=None):
self.listboxClasses.delete(0,END)
for id, cl in enumerate(self.classes):
if id == class_id:
self.listboxClasses.insert(END, '->[{}]'.format(id+1) + cl)
else:
self.listboxClasses.insert(END, '[{}]'.format(id+1) + cl)
def setBBoxClass(self, classId):
if classId >= self.listboxClasses.size():
raise Exception('The class does not exist.')
if self.listbox.size() == 0:
return
selections = self.listbox.curselection()
if len(selections) == 1:
bbox_index = int(selections[0])
bbox = list(self.bboxList[bbox_index])
self.bboxList.pop(bbox_index)
bbox[4] = classId
self.bboxList.insert(bbox_index, tuple(bbox))
self.saveImage()
self.reloadClassList(classId)
self.reloadBboxListbox()
def setAllBboxClasses(self):
if self.currDefaultBboxClassId >= self.listboxClasses.size():
raise Exception('The class does not exist.')
if self.listbox.size() == 0:
return
for bbox_index in range(self.listbox.size()):
bbox = list(self.bboxList[bbox_index])
self.bboxList.pop(bbox_index)
bbox[4] = self.currDefaultBboxClassId
self.bboxList.insert(bbox_index, tuple(bbox))
# print(self.bboxList)
self.saveImage()
self.reloadClassList()
self.reloadBboxListbox()
def loadClasses(self, classes_path):
if not os.path.exists(classes_path):
message = "Classes file not found."
print(message)
messagebox.showerror("Classes", message)
sys.exit()
classes = []
with open(classes_path, 'r') as f:
for cl in f:
cl = cl.strip()
classes.append(cl)
self.listboxClasses.insert(END,cl)
if len(classes) == 0:
message = "Classes file is empty."
print(message)
messagebox.showerror("Classes", message)
sys.exit()
print('Loaded classes:',classes)
return classes
def loadImage(self):
# load image
self.imagepath = self.imageList[self.cur - 1]
print('Loading image {}: '.format(self.cur), self.imagepath)
self.img = Image.open(self.imagepath)
self.tkimg = ImageTk.PhotoImage(self.img)
self.mainPanel.config(width = max(self.tkimg.width(), 400), height = max(self.tkimg.height(), 400))
self.mainPanel.create_image(0, 0, image = self.tkimg, anchor=NW)
self.progLabel.config(text = "%04d/%04d" %(self.cur, self.total))
# load labels
self.imagename = os.path.split(self.imagepath)[-1].split('.')[0]
labelname = self.imagename + '.txt'
self.dirspath = os.path.split(self.imagepath)[0]
self.labelfilename = os.path.join(self.labelsdir, self.dirspath, labelname)
self.darknetfilename = os.path.join(self.darknetdir, self.dirspath, labelname)
self.reloadClassList()
# topViewLineCoords = (0,379,639,379)
# if not self.topViewLine:
# self.topViewLine = self.mainPanel.create_line(topViewLineCoords, width = 1, dash=(4, 4))
# else:
# self.mainPanel.delete(self.topViewLine)
# self.topViewLine = self.mainPanel.create_line(topViewLineCoords, width = 1, dash=(4, 4))
#
# smallPedestriansCoords = (0,50,639,50)
# if not self.smallLine:
# self.smallLine = self.mainPanel.create_line(smallPedestriansCoords, width = 1, dash=(4, 4))
# else:
# self.mainPanel.delete(self.smallLine)
# self.smallLine = self.mainPanel.create_line(smallPedestriansCoords, width = 1, dash=(4, 4))
return self.reloadBboxListbox()
def reloadBboxListbox(self):
self.clearBBox()
bbox_cnt = 0
if os.path.exists(self.labelfilename):
with open(self.labelfilename) as f:
for (i, line) in enumerate(f):
if i == 0:
bbox_cnt = int(line.strip())
if bbox_cnt > 0:
self.noBboxes.set(False)
else:
self.noBboxes.set(True)
continue
tmp = [int(t.strip()) for t in line.split()]
if len(tmp) == 4:
#Seems like the current label file is in the old format. We need to add the class to it, which the default is 0.
defaultClass = 0
tmp.append(defaultClass) #adding default class zero as last element.
x1, y1, x2, y2, classId = tmp
x1 = 0 if x1 < 0 else x1
y1 = 0 if y1 < 0 else y1
x2 = self.tkimg.width()-1 if x2 >= self.tkimg.width() else x2
y2 = self.tkimg.height()-1 if y2 >= self.tkimg.height() else y2
self.bboxList.append(tuple([x1,y1,x2,y2,classId]))
if not self.hideOtherBboxes.get():
tmpId = self.mainPanel.create_rectangle(x1, y1, \
x2, y2, \
width = 1, \
outline = COLORS[(len(self.bboxList)-1) % len(COLORS)])
self.bboxIdList.append(tmpId)
textId = self.mainPanel.create_text(x1+1, y1+7, activefill='#000fff000', fill='#fff', anchor=W, text=str(classId+1))
self.classLabelIdList.append(textId)
self.listbox.insert(END, '%d:(%d,%d,%d,%d)[%d]' %(i, x1,y1,x2,y2,classId+1))
self.listbox.itemconfig(len(self.bboxList) - 1, fg = COLORS[(len(self.bboxList) - 1) % len(COLORS)])
else:
self.noBboxes.set(False)
return bbox_cnt > 0
def convert(self, bbox):
'''
References:
- https://github.com/Guanghan/darknet/blob/master/scripts/convert.py
- https://github.com/AlexeyAB/darknet
'''
im_w, im_h = self.img.size
classId = bbox[4]
xmin = float(bbox[0])
xmax = float(bbox[2])
if xmax >= im_w:#handles annotation tool bug which permits annotating out of the image boundaries.
xmax = im_w -1
ymin = float(bbox[1])
ymax = float(bbox[3])
if ymax >= im_h:#handles annotation tool bug which permits annotating out of the image boundaries.
ymax = im_h -1
dw = 1./im_w
dh = 1./im_h
x = (xmin + xmax)/2.0
y = (ymin + ymax)/2.0
w = xmax - xmin
h = ymax - ymin
x = x*dw
w = w*dw
y = y*dh
h = h*dh
# print(x,y,w,h)
return '{} {} {} {} {}'.format(classId,x,y,w,h)
def saveMeta(self):
#register image by class count
fileName = "classcount_{}.txt".format(len(self.bboxList))
classCountFile = os.path.join(self.metadir, fileName)
with open(classCountFile, 'a') as f:
f.write("{}\n".format(self.imagepath))
def prepareFileDirs(self):
labelsdirpath = os.path.join(self.labelsdir, self.dirspath)
os.makedirs(labelsdirpath, exist_ok=True)
darknetdirspath = os.path.join(self.darknetdir, self.dirspath)
os.makedirs(darknetdirspath, exist_ok=True)
def saveImage(self):
if len(self.bboxList) > 0 or self.noBboxes.get():
self.prepareFileDirs();
with open(self.labelfilename, 'w') as f:
f.write('%d\n' % len(self.bboxList))
for bbox in self.bboxList:
f.write(' '.join(map(str, bbox)) + '\n')
with open(self.darknetfilename, 'w') as f:
for bbox in self.bboxList:
f.write(self.convert(bbox=bbox) + '\n')
# self.saveMeta()
print('Image {} label saved. Path: {}'.format(self.cur, self.imagepath))
else:
print('Skipping: Image {}. You should draw bboxes or mark as [No BBoxes]. Path: {}.'.format(self.cur, self.imagepath))
if os.path.exists(self.labelfilename):
print('Also: Removing the existing label file.')
os.remove(self.labelfilename)
if os.path.exists(self.darknetfilename):
print('Also: Removing the existing darknet label file.')
os.remove(self.darknetfilename)
def selectBestBboxByIou(self, x, y):
iou_scores = []
for bbox in self.bboxList:
iou_scores.append(self.iou([x, y, x, y], bbox))
best_iou = max(iou_scores)
if best_iou == 0.0:
return
best_bbox_id = iou_scores.index(best_iou)
self.listbox.selection_clear(0, END)
self.listbox.selection_set(best_bbox_id)
def mouseClick(self, event):
if self.bboxSelector.get():
self.selectBestBboxByIou(event.x, event.y)
return
if self.STATE['click'] == 0:
self.STATE['x'], self.STATE['y'] = event.x, event.y
else:
x1, x2 = min(self.STATE['x'], event.x), max(self.STATE['x'], event.x)
y1, y2 = min(self.STATE['y'], event.y), max(self.STATE['y'], event.y)
x1 = 0 if x1 < 0 else x1
y1 = 0 if y1 < 0 else y1
x2 = self.tkimg.width()-1 if x2 >= self.tkimg.width() else x2
y2 = self.tkimg.height()-1 if y2 >= self.tkimg.height() else y2
self.bboxList.append((x1, y1, x2, y2, self.currDefaultBboxClassId))
self.bboxIdList.append(self.bboxId)
self.bboxId = None
self.listbox.insert(END, '%d:(%d,%d,%d,%d)[%d]' %(len(self.bboxIdList),x1, y1, x2, y2, self.currDefaultBboxClassId+1))
self.listbox.itemconfig(len(self.bboxIdList) - 1, fg = COLORS[(len(self.bboxIdList) - 1) % len(COLORS)])
textId = self.mainPanel.create_text(self.STATE['x']+1, self.STATE['y']+7, activefill='#000fff000', fill='#fff', anchor=W, text=str(self.currDefaultBboxClassId+1))
self.classLabelIdList.append(textId)
self.STATE['click'] = 1 - self.STATE['click']
def mouseMove(self, event):
self.disp.config(text = 'x: %d, y: %d' %(event.x, event.y))
if self.tkimg:
if self.hl:
self.mainPanel.delete(self.hl)
self.hl = self.mainPanel.create_line(0, event.y, self.tkimg.width(), event.y, width = 1)
if self.vl:
self.mainPanel.delete(self.vl)
self.vl = self.mainPanel.create_line(event.x, 0, event.x, self.tkimg.height(), width = 1)
if 1 == self.STATE['click']:
if self.bboxId:
self.mainPanel.delete(self.bboxId)
self.bboxId = self.mainPanel.create_rectangle(self.STATE['x'], self.STATE['y'], \
event.x, event.y, \
width = 1, \
outline = COLORS[len(self.bboxList) % len(COLORS)])
def cancelBBox(self, event):
if 1 == self.STATE['click']:
if self.bboxId:
self.mainPanel.delete(self.bboxId)
self.bboxId = None
self.STATE['click'] = 0
def deleteLastBBox(self, event):
bboxlen = len(self.bboxIdList)
if bboxlen == 0:
return
idx = bboxlen - 1
self.delBBoxByIndex(idx)
def delBBoxByIndex(self, idx):
self.mainPanel.delete(self.bboxIdList[idx])
self.bboxIdList.pop(idx)
self.bboxList.pop(idx)
self.listbox.delete(idx)
self.mainPanel.delete(self.classLabelIdList[idx])
self.classLabelIdList.pop(idx)
def delBBox(self, event=None):
sel = self.listbox.curselection()
if len(sel) != 1 :
return
idx = int(sel[0])
self.delBBoxByIndex(idx)
def nextBBoxOverlap(self, event = None):
pass
def sendToTheHive(self, event = None):
if not os.path.exists(self.hivedir):
os.mkdir(self.hivedir)
new_image_name = self.imagepath.replace('./','')
new_image_name = new_image_name.replace('/','_')
new_path = os.path.join(self.hivedir, new_image_name)
shutil.copy(self.imagepath, new_path)
self.hivecount += 1
print("{}. Sent {} to {}.".format(self.hivecount, new_path, self.hivedir))
def clearBBox(self):
for idx in range(len(self.bboxIdList)):
self.mainPanel.delete(self.bboxIdList[idx])
self.listbox.delete(0, len(self.bboxList))
self.bboxIdList = []
self.bboxList = []
for id in self.classLabelIdList:
self.mainPanel.delete(id)
self.classLabelIdList = []
def prevImage(self, event = None):
if self.noBboxes.get() and len(self.bboxList) > 0:
print('Conflict: Marked as [No BBoxes] but has bboxes in the list.')
return
self.saveImage()
if self.cur > 1:
self.cur -= 1
self.loadImage()
def checkNoBboxConflict(self):
if self.noBboxes.get() and len(self.bboxList) > 0:
print('Conflict: Marked as [No BBoxes] but has bboxes in the list.')
return True
return False
def nextImage(self, event = None):
if self.checkNoBboxConflict():
return
self.saveImage()
if self.cur < self.total:
self.cur += 1
self.loadImage()
def nextNoBboxImage(self, event = None):
self.btnNextNoBBox.config(state="disabled")
while True:
self.nextImage()
if len(self.bboxList) == 0 or self.cur >= self.total or self.checkNoBboxConflict():
self.btnNextNoBBox.config(state="normal")
return
def findImageIdxByName(self,name):
print('Searching...')
for index, img_path in enumerate(self.imageList):
if name in img_path:
print('Found', img_path)
return index+1
print('Couldnt find by', name)
return -1
def gotoImage(self):
if self.noBboxes.get() and len(self.bboxList) > 0:
print('Conflict: Marked as [No BBoxes] but has bboxes in the list.')
return
try:
idx = int(self.idxEntry.get()) #check if this is an integer
except ValueError:
idx = self.findImageIdxByName(self.idxEntry.get())
if 1 <= idx and idx <= self.total:
self.saveImage()
self.cur = idx
self.loadImage()
## def setImage(self, self.imagepath = r'test2.jpg'):
## self.img = Image.open(self.imagepath)
## self.tkimg = ImageTk.PhotoImage(self.img)
## self.mainPanel.config(width = self.tkimg.width())
## self.mainPanel.config(height = self.tkimg.height())
## self.mainPanel.create_image(0, 0, image = self.tkimg, anchor=NW)
if __name__ == '__main__':
root = Tk()
tool = LabelTool(root)
root.resizable(width = True, height = True)
root.mainloop()