-
Notifications
You must be signed in to change notification settings - Fork 20
/
euclid.py
600 lines (516 loc) · 27 KB
/
euclid.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
#-------------------------------------------------------------------------------
# Euclid - Labelling tool
# Create and label bounding boxes
# prabindh@yahoo.com, 2017
# Initial code taken from github.com/puzzledqs/BBox-Label-Tool
# Significantly modified to add more image types, image folders, labelling saves, and format, and format selection
# Currently supports Kitti and YOLO(darknet) output formats
# Python 2.7
# pip install pillow
# pip install image
# Python 3
# Python 3 + Pillow on Ubuntu, do the below
# sudo apt-get install python-imaging-tk
# sudo apt-get install python3-pil.imagetk
#
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# The DetectNet/ Kitti Database format
# Taken from https://github.com/NVIDIA/DIGITS/blob/master/digits/extensions/data/objectDetection/README.md#label-format
# All values (numerical or strings) are separated via spaces,
# each row corresponds to one object. The 15 columns represent:
#
#Values Name Description
#----------------------------------------------------------------------------
# 1 type Describes the type of object: 'Car', 'Van', 'Truck',
# 'Pedestrian', 'Person_sitting', 'Cyclist', 'Tram',
# 'Misc' or 'DontCare'
# 1 truncated Float from 0 (non-truncated) to 1 (truncated), where
# truncated refers to the object leaving image boundaries
# 1 occluded Integer (0,1,2,3) indicating occlusion state:
# 0 = fully visible, 1 = partly occluded
# 2 = largely occluded, 3 = unknown
# 1 alpha Observation angle of object, ranging [-pi..pi]
# 4 bbox 2D bounding box of object in the image (0-based index):
# contains left, top, right, bottom pixel coordinates
# 3 dimensions 3D object dimensions: height, width, length (in meters)
# 3 location 3D object location x,y,z in camera coordinates (in meters)
# 1 rotation_y Rotation ry around Y-axis in camera coordinates [-pi..pi]
# 1 score Only for results: Float, indicating confidence in
# detection, needed for p/r curves, higher is better.
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
# The YOLO format
# All values (numerical or strings) are separated via spaces,
# each row corresponds to one object. The 5 columns represent:
#
#Values Name Description
#----------------------------------------------------------------------------
# 1 Class ID Describes the class number of object, as an integer number (0 based)
# 1 Center_X Float from 0 to 1, X coordinate of b-box center, normalised to image width
# 1 Center_Y Float from 0 to 1, Y coordinate of b-box center, normalised to image height
# 1 Bbox_Width Float from 0 to 1, Width of b-box, normalised to image width
# 1 Bbox_Height Float from 0 to 1, Height of b-box, normalised to image height
#-------------------------------------------------------------------------------
import sys
if sys.version_info[0] < 3:
from Tkinter import *
import tkMessageBox
import tkFileDialog
else:
from tkinter import *
from tkinter import messagebox as tkMessageBox
from tkinter import filedialog as tkFileDialog
from PIL import Image, ImageTk
import os
import glob
import random
# Usage
USAGE = " \
1. Select a Directory of images in bottom control panel \n \
2. Click Load \n \
3. The first image in the directory should load, along with existing labels if any. \n \
4. Select a Class label to be used for next bounding box, in class-control panel \n \
5. After the image is loaded, press x key, or mouseclick to draw bounding boxes over the image \n \
6. Click Save in the File Navigation panel in bottom, to save the bounding boxes \n \
7. Labels are saved in folder named LabelData in same directory as the images \n \
8. Can use Left/Right arrows for navigating prev/next images \n \
Note: Default is KITTI format \
"
class Euclid():
#set class label
def setClassN(self, N):
self.currClassLabel=N;
self.currClassLabelDisplayString.set('Current Class = '+ str(N))
def askDirectory(self):
self.imageDir = tkFileDialog.askdirectory()
self.SavePathToConfig(self.imageDir)
self.entry.insert(0, self.imageDir)
self.loadDir(self)
def SavePathToConfig(self, newPath):
#config file
configFile = open(os.path.join(sys.path[0], "euclidconfig.txt"), "a+")
lines = configFile.readlines()
if(len(lines) > 10):
configFile.close()
configFile = open(os.path.join(sys.path[0], "euclidconfig.txt"), "w+")
configFile.write(newPath + "\n")
configFile.close()
else:
configFile.write(newPath + "\n")
configFile.close()
def AddFileToTrainingList(self, newFile):
#training file
trainfile = open(os.path.join(sys.path[0], "train.txt"), "a+")
trainfile.write(newFile + "\n")
trainfile.close()
def TestClassEntry(self,inStr,i,acttyp):
ind=int(i)
if acttyp == '1': #insert
if not inStr[ind].isdigit():
return False
self.setClassN(int(inStr))
return True
def loadDir(self, dbg = False):
self.imageDir = self.entry.get()
self.parent.focus()
if not os.path.isdir(self.imageDir):
tkMessageBox.showerror("Folder error", message = "The specified directory doesn't exist!")
return
self.SavePathToConfig(self.imageDir)
#get image list
imageFileTypes = ('*.JPEG', '*.JPG', '*.PNG') # the tuple of file types
self.imageList = []
for files in imageFileTypes:
self.imageList.extend(glob.glob(os.path.join(self.imageDir, files.lower())) )
if (False == self.is_windows):
self.imageList.extend(glob.glob(os.path.join(self.imageDir, files)) )
if len(self.imageList) == 0:
tkMessageBox.showerror("File not found", message = "No images (png, jpeg, jpg) found in folder!")
self.updateStatus( 'No image files found in the specified dir!')
return
# Change title
self.parent.title("Euclid Labeller (" + self.imageDir + ") " + str(len(self.imageList)) + " images")
# default to the 1st image in the collection
self.cur = 1
self.total = len(self.imageList)
# set up output dir
self.outDir = os.path.join(self.imageDir + '/LabelData')
if not os.path.exists(self.outDir):
os.mkdir(self.outDir)
self.updateStatus( '%d images loaded from %s' %(self.total, self.imageDir))
self.loadImageAndLabels()
def __init__(self, master):
# set up the main frame
self.parent = master
self.parent.title("Euclid Labeller (Press F1 for Help)")
self.frame = Frame(self.parent)
self.frame.pack(fill=BOTH, expand=1)
self.parent.resizable(width = TRUE, height = TRUE)
self.is_windows = hasattr(sys, 'getwindowsversion')
self.cancelKnownBox = False;
# initialize global state
self.imageDir = ''
self.imageList= []
self.outDir = ''
self.cur = 0
self.total = 0
self.imagename = ''
self.labelfilename = ''
self.currLabelMode = 'YOLO' #'KITTI' #'YOLO' # Other modes TODO
self.imagefilename = ''
self.tkimg = None
# initialize mouse state
self.STATE = {}
self.STATE['click'] = 0
self.STATE['x'], self.STATE['y'] = 0, 0
self.STATE['prevX'], self.STATE['prevY'] = 0, 0
self.currentMouseX = 0;
self.currentMouseY = 0;
#colors
self.redColor = self.blueColor = self.greenColor = 128
# reference to bbox
self.bboxIdList = []
self.bboxId = None
self.bboxList = []
self.currClassLabel = 0
self.classLabelList = []
self.hl = None
self.vl = None
# ----------------- GUI stuff ---------------------
# main panel for labeling
self.imagePanelFrame = Frame(self.frame)
self.imagePanelFrame.grid(row = 0, column = 0, rowspan = 4, padx = 5, sticky = W+N)
self.imageLabel = Label(self.imagePanelFrame, text = 'Image View')
self.imageLabel.grid(row = 0, column = 0, sticky = W+N)
self.mainPanel = Canvas(self.imagePanelFrame, cursor='tcross', borderwidth=2, background='light blue')
self.mainPanel.bind("<Button-1>", self.mouseClick)
self.mainPanel.bind("<Motion>", self.mouseMove)
self.parent.bind("n", self.nextImage)
self.parent.bind("x", self.selectPointXY)
self.parent.bind("<Escape>", self.cancelBBox) # press <Escape> to cancel current bbox
self.parent.bind("<F1>", self.showHelp) # press <F1> to show help
self.parent.bind("<Left>", self.prevImage) # press 'Left Arrow' to go backforward
self.parent.bind("<Right>", self.nextImage) # press 'Right Arrow' to go forward
self.parent.bind("c", self.cancelKnownBoxFunc) # Cancel knownbox
self.mainPanel.grid(row = 1, column = 0, rowspan = 4, sticky = W+N)
# Boundingbox info panel
self.bboxControlPanelFrame = Frame(self.frame)
self.bboxControlPanelFrame.grid(row = 0, column = 1, sticky = E)
self.lb1 = Label(self.bboxControlPanelFrame, text = 'Bounding box / Label list')
self.lb1.grid(row = 0, column = 0, sticky = W+N)
self.listbox = Listbox(self.bboxControlPanelFrame, width = 40, height = 22, background='white')
self.listbox.grid(row = 1, column = 0, sticky = N)
self.btnDel = Button(self.bboxControlPanelFrame, text = 'Delete', command = self.delBBox)
self.btnDel.grid(row = 2, column = 0, sticky = W+E+N)
self.btnClear = Button(self.bboxControlPanelFrame, text = 'Clear All', command = self.clearBBox)
self.btnClear.grid(row = 3, column = 0, sticky = W+E+N)
#Class labels selection
# control panel for label navigation
self.labelControlPanelFrame = Frame(self.frame)
self.labelControlPanelFrame.grid(row = 0, column = 2, padx = 5, sticky = N+E)
self.classLabelText = Label(self.labelControlPanelFrame, text = '3. Select class before drawing box')
self.classLabelText.grid(row = 0, column = 0, sticky = W+E+N)
count = 0
#Provide entry fields
self.ClassEntry = Entry(self.labelControlPanelFrame, validate="key")
self.ClassEntry.grid(row = 1+count, column = 0, sticky = N)
self.ClassEntry['validatecommand'] = (self.ClassEntry.register(self.TestClassEntry),'%P','%i','%d')
#Display current label
self.currClassLabelDisplayString = StringVar()
self.currClassLabelDisplayString.set('Current Class = 0')
self.currClassLabelDisplay = Label(self.labelControlPanelFrame, textvariable = self.currClassLabelDisplayString)
self.currClassLabelDisplay.grid(row = 2+count, column = 0, sticky = W+E+N)
# dir entry & load File control panel
self.FileControlPanelFrame = Frame(self.frame)
self.FileControlPanelFrame.grid(row = 5, column = 0, sticky = W)
self.FileControlPanelLabel = Label(self.FileControlPanelFrame, text = '1. Select a directory (or) Enter input path, and click Load')
self.FileControlPanelLabel.grid(row = 0, column = 0, sticky = W+N)
self.browserBtn = Button(self.FileControlPanelFrame, text = "Select Dir", command = self.askDirectory)
self.browserBtn.grid(row = 1, column = 0, sticky = N)
self.entry = Entry(self.FileControlPanelFrame)
self.entry.grid(row = 1, column = 1, sticky = N)
self.ldBtn = Button(self.FileControlPanelFrame, text = "Load", command = self.loadDir)
self.ldBtn.grid(row = 1, column = 2, sticky = N)
self.FormatLabel = Label(self.FileControlPanelFrame, text = '2. Format Selection')
self.FormatLabel.grid(row = 2, column = 0, sticky = W+N)
self.isYoloCheckBox = IntVar()
self.isYoloCheckBox.set(0)
self.yoloCheckBox = Radiobutton(self.FileControlPanelFrame, variable=self.isYoloCheckBox, value=1, text="Yolo Format")
self.yoloCheckBox.grid(row = 3, column = 0, sticky = N)
self.kittiCheckBox = Radiobutton(self.FileControlPanelFrame, variable=self.isYoloCheckBox, value=0, text="KITTI Format")
self.kittiCheckBox.grid(row = 3, column = 1, sticky = N)
# control panel for image navigation
self.ctrPanel = Frame(self.frame)
self.ctrPanel.grid(row = 6, column = 0, columnspan = 2, sticky = W+N)
self.navLabel = Label(self.ctrPanel, text = '4. File Navigation')
self.navLabel.pack(side = LEFT, padx = 5, pady = 3)
self.prevBtn = Button(self.ctrPanel, text='<< Prev', width = 10, command = self.prevImage)
self.prevBtn.pack(side = LEFT, padx = 5, pady = 3)
self.saveLabelBtn = Button(self.ctrPanel, text = 'Save Current Boxes/Labels', command = self.saveLabel)
self.saveLabelBtn.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.tmpLabel = Label(self.ctrPanel, text = "Go to Image No.")
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)
self.progLabel = Label(self.ctrPanel, text = "Progress: [ 0 / 0 ]")
self.progLabel.pack(side = LEFT, padx = 5)
# Status panel for image navigation
self.statusPanel = Frame(self.frame)
self.statusPanel.grid(row = 7, column = 0, columnspan = 3, sticky = W)
self.statusText = StringVar()
self.statusLabel = Label(self.statusPanel, textvariable = self.statusText)
self.statusLabel.grid(row = 0, column = 0, sticky = W+E+N)
self.updateStatus("Directory not selected.")
# 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)
def loadImageAndLabels(self):
# load image
imagepath = self.imageList[self.cur - 1]
self.imagefilename = imagepath
self.img = Image.open(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 = "Progress: [ %04d / %04d ]" %(self.cur, self.total))
self.updateStatus("Loaded file " + imagepath)
if self.tkimg.width() > 1024 or self.tkimg.height() > 1024:
tkMessageBox.showwarning("Too large image", message = "Image dimensions not suited for Deep Learning frameworks!")
# load labels
self.clearBBox()
self.classLabelList = []
lastPartFileName, lastPartFileExtension = os.path.splitext(os.path.split(imagepath)[-1])
self.imagename = lastPartFileName
labelname = self.imagename + '.txt'
self.labelfilename = os.path.join(self.outDir, labelname)
bbox_cnt = 0
if os.path.exists(self.labelfilename):
with open(self.labelfilename) as f:
for (i, line) in enumerate(f):
bbox_cnt = len(line)
tmp = [elements.strip() for elements in line.split()]
if(len(tmp) > 5):
self.currLabelMode='KITTI'
bbTuple = (int(float(tmp[4])),int(float(tmp[5])), int(float(tmp[6])),int(float(tmp[7])) )
self.classLabelList.append('Class'+tmp[0])
else:
self.currLabelMode='YOLO'
bbTuple = self.GetBoundariesFromYoloFile(float(tmp[1]),float(tmp[2]), float(tmp[3]),float(tmp[4]),
self.tkimg.width(), self.tkimg.height() )
self.classLabelList.append(tmp[0])
self.bboxList.append( bbTuple )
#color set
currColor = '#%02x%02x%02x' % (self.redColor, self.greenColor, self.blueColor)
self.greenColor = (self.greenColor + 45) % 255
tmpId = self.mainPanel.create_rectangle(int(bbTuple[0]), int(bbTuple[1]), \
int(bbTuple[2]), int(bbTuple[3]), \
width = 2, \
outline = currColor)
self.bboxIdList.append(tmpId)
self.listbox.insert(END, '(%d, %d) -> (%d, %d) [%s]' %(int(bbTuple[0]), int(bbTuple[1]), \
int(bbTuple[2]), int(bbTuple[3]), tmp[0]))
self.listbox.itemconfig(len(self.bboxIdList) - 1, fg = currColor)
def GetBoundariesFromYoloFile(self, centerX, centerY, width, height, imageWidth, imageHeight):
topLeftX = (int)(centerX*imageWidth - (width*imageWidth)/2)
topLeftY = (int)(centerY*imageHeight - (height*imageHeight)/2)
bottomRightX = (int)(centerX*imageWidth + (width*imageWidth)/2)
bottomRightY = (int)(centerY*imageHeight + (height*imageHeight)/2)
return topLeftX, topLeftY, bottomRightX, bottomRightY
def convert2Yolo(self, image, boxCoords):
invWidth = 1./image[0]
invHeight = 1./image[1]
x = invWidth * (boxCoords[0] + boxCoords[2])/2.0
y = invHeight * (boxCoords[1] + boxCoords[3])/2.0
boxWidth = invWidth * (boxCoords[2] - boxCoords[0])
boxHeight = invHeight * (boxCoords[3] - boxCoords[1])
return (x,y,boxWidth,boxHeight)
def saveLabel(self):
if self.labelfilename == '':
return
if(len(self.bboxList) == 0):
return
if self.isYoloCheckBox.get() == 0:
self.currLabelMode = 'KITTI'
else:
self.currLabelMode = 'YOLO'
if self.currLabelMode == 'KITTI':
with open(self.labelfilename, 'w') as f:
labelCnt=0
##class1 0 0 0 x1,y1,x2,y2 0,0,0 0,0,0 0 0
# fields ignored by DetectNet: alpha, scenario, roty, occlusion, dimensions, location.
for bbox in self.bboxList:
f.write('%s' % 'Class'+ str(self.classLabelList[labelCnt]))
f.write(' 0.0 0 0.0 ')
#f.write(str(bbox[0])+' '+str(bbox[1])+' '+str(bbox[2])+' '+str(bbox[3]))
f.write('%.2f %.2f %.2f %.2f' % (bbox[0], bbox[1], bbox[2], bbox[3]))
f.write(' 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 ')
f.write('\n')
labelCnt = labelCnt+1
self.updateStatus ('Label Image No. %d saved' %(self.cur))
elif self.currLabelMode == 'YOLO':
with open(self.labelfilename, 'w') as f:
labelCnt=0
##class1 center_box_x_ratio center_box_y_ratio width_ratio height_ratio
for bbox in self.bboxList:
yoloOut = self.convert2Yolo(
[self.tkimg.width(), self.tkimg.height()],
[bbox[0], bbox[1], bbox[2], bbox[3]]
);
f.write('%s' %self.classLabelList[labelCnt])
f.write(' %.7f %.7f %.7f %.7f' % (yoloOut[0], yoloOut[1], yoloOut[2], yoloOut[3]))
f.write('\n')
#tkMessageBox.showinfo("Save Info", message = self.classLabelList[labelCnt])
labelCnt = labelCnt+1
self.updateStatus ('Label Image No. %d saved' %(self.cur))
self.AddFileToTrainingList(self.imagefilename);
else:
tkMessageBox.showerror("Labelling error", message = 'Unknown Label format')
def selectPointXY(self, event):
self.handleMouseOrXKey(self.currentMouseX, self.currentMouseY)
def mouseClick(self, event):
self.handleMouseOrXKey(event.x, event.y)
def handleMouseOrXKey(self, xCoord, yCoord):
if self.imagefilename == '':
return
if self.STATE['click'] == 0:
self.STATE['x'], self.STATE['y'] = xCoord, yCoord
else:
#Got a new BB, store the class label also
x1, x2 = min(self.STATE['x'], xCoord), max(self.STATE['x'], xCoord)
y1, y2 = min(self.STATE['y'], yCoord), max(self.STATE['y'], yCoord)
self.bboxList.append((x1, y1, x2, y2))
self.bboxIdList.append(self.bboxId)
self.classLabelList.append(self.currClassLabel)
self.bboxId = None
self.listbox.insert(END, '(%d, %d) -> (%d, %d)[Class %d]' %(x1, y1, x2, y2 , self.currClassLabel))
#color set
currColor = '#%02x%02x%02x' % (self.redColor, self.greenColor, self.blueColor)
self.redColor = 25 + (self.redColor + 25) % 200
self.listbox.itemconfig(len(self.bboxIdList) - 1, fg = currColor)
self.STATE['click'] = 1 - self.STATE['click']
def handleMouseOrXKeyKnownBox(self, xCoord, yCoord):
knownBoxWidth = 36; #22 #38 #25 #50(old yt top hud) #40,23 (new yt top hud) #20,20 ult #38,23
knownBoxHeight = 24; #22 #28 #25 #23
if self.imagefilename == '':
return
self.STATE['x'], self.STATE['y'] = xCoord, yCoord
self.STATE['prevX'], self.STATE['prevY'] = xCoord, yCoord
x1 = xCoord;
y1 = yCoord;
x2 = xCoord + knownBoxWidth;
y2 = yCoord +knownBoxHeight;
#Show bbox
if self.bboxId:
self.mainPanel.delete(self.bboxId)
#color set
self.blueColor = 15
currColor = '#%02x%02x%02x' % (self.redColor, self.greenColor, self.blueColor)
self.redColor = 255
self.greenColor = 200+(self.greenColor + 5) % 55
self.bboxId = self.mainPanel.create_rectangle(x1, y1, x2, y2, \
width = 2, \
outline = currColor)
#Got a new BB, store the class label also
self.bboxList.append((x1, y1, x2, y2))
self.bboxIdList.append(self.bboxId)
self.classLabelList.append(self.currClassLabel)
self.bboxId = None
self.listbox.insert(END, '(%d, %d) -> (%d, %d)[Class %d]' %(x1, y1, x2, y2 , self.currClassLabel))
#color set
self.listbox.itemconfig(len(self.bboxIdList) - 1, fg = currColor)
def mouseMove(self, event):
if self.imagefilename == '':
return
self.disp.config(text = 'x: %d, y: %d' %(event.x, event.y))
if self.tkimg:
if event.x > self.tkimg.width():
return
if event.y > self.tkimg.height():
return
if self.hl:
self.mainPanel.delete(self.hl)
self.hl = self.mainPanel.create_line(0, event.y, self.tkimg.width(), event.y, width = 2)
if self.vl:
self.mainPanel.delete(self.vl)
self.vl = self.mainPanel.create_line(event.x, 0, event.x, self.tkimg.height(), width = 2)
if 1 == self.STATE['click']:
if self.bboxId:
self.mainPanel.delete(self.bboxId)
#color set
currColor = '#%02x%02x%02x' % (self.redColor, self.greenColor, self.blueColor)
self.blueColor = 10 + (self.blueColor + 1) % 230
self.bboxId = self.mainPanel.create_rectangle(self.STATE['x'], self.STATE['y'], \
event.x, event.y, \
width = 2, \
outline = currColor)
#Save current xy
self.currentMouseX = event.x;
self.currentMouseY = event.y;
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 showHelp(self, event):
tkMessageBox.showinfo("Help", USAGE)
def delBBox(self):
sel = self.listbox.curselection()
if len(sel) != 1 :
return
idx = int(sel[0])
self.mainPanel.delete(self.bboxIdList[idx])
self.bboxIdList.pop(idx)
self.bboxList.pop(idx)
self.classLabelList.pop(idx)
self.listbox.delete(idx)
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 = []
self.classLabelList = []
def prevImage(self, event = None):
self.STATE['prevX'] = 0
self.STATE['prevY'] = 0
self.saveLabel()
if self.cur > 1:
self.cur -= 1
self.loadImageAndLabels()
else:
self.updateStatus("No more previous files!")
tkMessageBox.showwarning("Labelling complete", message = "No previous file to label!")
def cancelKnownBoxFunc(self, event = None):
self.STATE['prevX'] = 0;
self.STATE['prevY'] = 0;
def nextImage(self, event = None):
if((self.STATE['prevX']+self.STATE['prevY']) > 0):
self.handleMouseOrXKey(self.STATE['prevX'], self.STATE['prevY'])
self.saveLabel()
if self.cur < self.total:
self.cur += 1
self.loadImageAndLabels()
else:
self.updateStatus("No more next files!")
tkMessageBox.showwarning("Labelling complete", message = "No next file to label!")
def gotoImage(self):
if self.idxEntry.get() == '':
return
idx = int(self.idxEntry.get())
if 1 <= idx and idx <= self.total:
self.cur = idx
self.loadImageAndLabels()
def updateStatus(self, newStatus):
self.statusText.set("Status: " + newStatus)
if __name__ == '__main__':
root = Tk()
tool = Euclid(root)
root.mainloop()