-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
430 lines (377 loc) · 18.2 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import division
from Tkinter import *
import tkMessageBox, tkFileDialog
from PIL import Image, ImageTk
import ttk
import os
import glob
import random
import xml.etree.ElementTree as ET
import io
import convert
import re
# colors for the bboxes
COLORS = ['red', 'blue', 'olive', 'teal', 'cyan', 'green', 'black']
# image sizes for the examples
SIZE = 256, 256
class LabelTool():
def __init__(self, master):
# set up the main frame
self.parent = master
self.parent.title("Yolo Annotator")
self.frame = Frame(self.parent)
self.frame.pack(fill=BOTH, expand=1)
self.parent.resizable(width = FALSE, height = FALSE)
# initialize global state
self.imageDir = ''
self.imageList= []
self.egDir = ''
self.egList = []
self.outDir = ''
self.xmlOutDir =''
self.cur = 0
self.total = 0
self.category = ''
self.imagename = ''
self.labelfilename = ''
self.tkimg = None
self.currentLabelclass = ''
self.cla_can_temp = []
self.classcandidate_filename = 'class.txt'
self.classcnt = 0
# 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.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.ldProjBtn = Button(self.frame, text = "Load Image", command = self.loadDir)
self.ldProjBtn.grid(row = 0, column = 0,sticky = W+E, padx=5)
#self.ldImgBtn = Button(self.frame, text = "Load XML", command = self.loadXML)
#self.ldImgBtn.grid(row = 0, column = 1,sticky = W, padx=5)
# 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("<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("r", self.clearBBoxShortcut)
self.mainPanel.grid(row = 1, column = 1, columnspan = 3, rowspan = 4, sticky = W+N)
# choose class
self.classname = StringVar()
self.classcandidate = ttk.Combobox(self.frame,state='readonly',textvariable=self.classname)
self.classcandidate.grid(row=1,column=4)
if os.path.exists(self.classcandidate_filename):
with open(self.classcandidate_filename) as cf:
for line in cf.readlines():
# print line
self.cla_can_temp.append(line.strip('\n'))
self.classcnt +=1
self.classcandidate['values'] = self.cla_can_temp
self.classcandidate.current(0)
self.parent.bind('<Key>', self.setClassShortcut)
#self.parent.bind("1", lambda e: self.setClassShortcut(0))
#self.parent.bind("2", lambda e: self.setClassShortcut(1))
#2self.parent.bind("3", lambda e: self.setClassShortcut(2))
self.currentLabelclass = self.classcandidate.get() #init
self.classcandidate.bind('<<ComboboxSelected>>', self.setClass)
#self.btnclass = Button(self.frame, text = 'ComfirmClass', command = self.setClass)
#self.btnclass.grid(row=2,column=2,sticky = W+E)
# showing bbox info & delete bbox
self.lb1 = Label(self.frame, text = 'Bounding boxes:')
self.lb1.grid(row = 3, column = 4, sticky = W+N)
self.listbox = Listbox(self.frame, width = 22, height = 12)
self.listbox.grid(row = 4, column = 4, sticky = N+S)
self.btnDel = Button(self.frame, text = 'Clear', command = self.delBBox)
self.btnDel.grid(row = 5, column = 4, sticky = W+E+N)
self.btnClear = Button(self.frame, text = 'ClearAll', command = self.clearBBox)
self.btnClear.grid(row = 6, column = 4, sticky = W+E+N)
# control panel for image navigation
self.ctrPanel = Frame(self.frame)
self.ctrPanel.grid(row = 7, column = 1, columnspan = 4, sticky = W+E)
self.conv2YoloBtn = Button(self.ctrPanel, text='Convert YOLO', width = 15, command = self.convert2Yolo)
self.conv2YoloBtn.pack(side = LEFT, padx = 5, pady = 3)
self.resetChkBtn = Button(self.ctrPanel, text='ResetCheckpoint', width = 15, command = self.resetCheckpoint)
self.resetChkBtn.pack(side = LEFT, padx = 5, pady = 3)
self.loadChkBtn = Button(self.ctrPanel, text='LoadCheckpoint', width = 15, command = self.loadCheckpoint)
self.loadChkBtn.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.skipBtn = Button(self.ctrPanel, text ='Skip', width = 10, command = self.skipImage)
self.skipBtn.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 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)
# example pannel for illustration
self.egPanel = Frame(self.frame, border = 10)
self.egPanel.grid(row = 1, column = 0, rowspan = 5, sticky = N)
self.tmpLabel2 = Label(self.egPanel, text = "Key Shortcut :\na : Prev\nd : Next\ns : Cancel\nr : Delete BB\n1-9 : Select Class")
self.tmpLabel2.pack(side = TOP)
self.tmpLabel3 = Label(self.egPanel, text = "\nBasic Usage :\n1.Load Image\n2.Annotate\n3.Convert Yolo")
self.tmpLabel3.pack(side = TOP)
self.egLabels = []
for i in range(3):
self.egLabels.append(Label(self.egPanel))
self.egLabels[-1].pack(side = TOP)
# 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)
# for debugging
## self.setImage()
## self.loadDir()
def loadDir(self, dbg = False):
self.imageList = []
if not dbg:
#s = self.entry.get()
self.parent.focus()
#self.category = int(s)
s = str(tkFileDialog.askdirectory(initialdir=os.getcwd())).split('/')[-1]
self.category = 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', '%s' %(self.category))
#print self.imageDir
#print self.category
for ext in ('*.png', '*.jpg'):
self.imageList.extend(glob.glob(os.path.join(self.imageDir, ext)))
#print self.imageList
if len(self.imageList) == 0:
tkMessageBox.showinfo("Error", "No JPG/PNG images found in the specified dir!")
print 'No JPG/PNG images found in the specified dir!'
return
# 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(r'./Result', '%s' %(self.category))
if not os.path.exists(self.outDir):
os.mkdir(self.outDir)
self.loadImage()
print '%d images loaded from %s' %(self.total, self.category)
tkMessageBox.showinfo("Info", "%d images loaded from %s" %(self.total, self.category))
def loadImage(self):
# load image
imagepath = self.imageList[self.cur - 1]
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 = "%04d/%04d" %(self.cur, self.total))
# load labels
self.clearBBox()
self.imagename = os.path.split(imagepath)[-1]
#self.imagename = os.path.split(imagepath)[-1].split('.jpg')[0]
#self.imagename = re.split('.jpg|.png',os.path.split(imagepath)[-1])[0]
labelname = self.imagename + '.txt'
self.labelfilename = os.path.join(self.outDir, labelname)
bbox_cnt = 0
self.loadBBox()
def saveImage(self):
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')
print 'Image No. %d saved' %(self.cur)
def mouseClick(self, event):
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)
self.bboxList.append((x1, y1, x2, y2, self.currentLabelclass))
self.bboxIdList.append(self.bboxId)
self.bboxId = None
self.listbox.insert(END, '%s : (%d, %d) -> (%d, %d)' %(self.currentLabelclass,x1, y1, x2, y2))
self.listbox.itemconfig(len(self.bboxIdList) - 1, fg = COLORS[(len(self.bboxIdList) - 1) % len(COLORS)])
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 = 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)
self.bboxId = self.mainPanel.create_rectangle(self.STATE['x'], self.STATE['y'], \
event.x, event.y, \
width = 2, \
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 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.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 = []
def clearBBoxShortcut(self, event):
for idx in range(len(self.bboxIdList)):
self.mainPanel.delete(self.bboxIdList[idx])
self.listbox.delete(0, len(self.bboxList))
self.bboxIdList = []
self.bboxList = []
'''def loadXML(self, event = None):
if (self.category == ''):
tkMessageBox.showinfo("Error", "Please Load Image first")
else:
basePath = "C:/DeepEye/Data/Result/request"
xmlFiles = os.listdir(basePath)
for xml in xmlFiles:
if not(os.path.isfile(self.imageDir+'/'+xml.split('.xml.req')[0]+'_C.jpg')):
continue
fullPath = os.path.join(basePath, xml)
if os.path.isfile(fullPath):
f = io.open(fullPath,'r', encoding = 'euc-kr')
text = f.read().encode('utf-8')
f.close()
root = ET.fromstring(text)
for detect in root.findall('./Detections/detect'):
xmin = int(detect.find('rectx').text)
ymin = int(detect.find('recty').text)
xmax = int(detect.find('rectx').text) + int(detect.find('rectw').text)
ymax = int(detect.find('recty').text) + int(detect.find('recth').text)
classCode = detect.find('class').text
classLabel = ''
if (classCode == '0'):
classLabel = 'pothole'
elif (classCode == '1'):
classLabel = 'patchdamaged'
elif (classCode == '2'):
classLabel = 'spalling'
saveName = xml.split('.xml.req')[0] + '_C.txt'
savePath = os.path.join(self.outDir, saveName)
with open(savePath, 'w') as f:
f.write('1\n')
f.write('%d %d %d %d %s' %(xmin,ymin,xmax,ymax,classLabel))
tkMessageBox.showinfo("Info", "XML parsing complete")
self.clearBBox()
self.loadBBox()'''
def loadBBox(self):
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())
continue
# tmp = [int(t.strip()) for t in line.split()]
tmp = line.split()
#print tmp
self.bboxList.append(tuple(tmp))
tmpId = self.mainPanel.create_rectangle(int(tmp[0]), int(tmp[1]), \
int(tmp[2]), int(tmp[3]), \
width = 2, \
outline = COLORS[(len(self.bboxList)-1) % len(COLORS)])
# print tmpId
self.bboxIdList.append(tmpId)
self.listbox.insert(END, '%s : (%d, %d) -> (%d, %d)' %(tmp[4],int(tmp[0]), int(tmp[1]), \
int(tmp[2]), int(tmp[3])))
self.listbox.itemconfig(len(self.bboxIdList) - 1, fg = COLORS[(len(self.bboxIdList) - 1) % len(COLORS)])
def loadCheckpoint(self, event = None):
checkpoint = 0
with open("log/checkpoint.txt","r") as checkpointFile:
checkpoint = checkpointFile.read()
if 1 <= int(checkpoint) and int(checkpoint) <= self.total:
self.cur = int(checkpoint)
self.loadImage()
def resetCheckpoint(self, event = None):
with open("log/checkpoint.txt","w") as checkpointFile:
checkpointFile.write("1")
def prevImage(self, event = None):
self.saveImage()
if self.cur > 1:
self.cur -= 1
self.loadImage()
def nextImage(self, event = None):
self.saveImage()
if self.cur < self.total:
self.cur += 1
self.loadImage()
with open("log/checkpoint.txt","w") as checkpointFile:
checkpointFile.write("{}".format(self.cur))
print "Current image : "+self.imageList[self.cur-1]
def skipImage(self, event = None):
#os.remove(self.imageList[self.cur - 1])
print self.imageList[self.cur - 1]+" is skipped."
with open("log/skipped.txt",'a') as skippedFile:
skippedFile.write("{}\n".format(self.imageList[self.cur - 1]))
if self.cur < self.total:
self.cur += 1
self.loadImage()
def gotoImage(self):
idx = int(self.idxEntry.get())
if 1 <= idx and idx <= self.total:
self.saveImage()
self.cur = idx
self.loadImage()
def setClass(self, event):
self.currentLabelclass = self.classcandidate.get()
print 'set label class to :',self.currentLabelclass
def setClassShortcut(self, event):
if (event.char.isdigit()):
idx = int(event.char) - 1
print idx
self.classcandidate.current(idx)
self.currentLabelclass = self.classcandidate.get()
print 'set label class to :',self.currentLabelclass
def convert2Yolo(self, event = None):
if (self.category == ''):
tkMessageBox.showinfo("Error", "Please Annotate Image first")
else:
outpath = "./Result_YOLO/" + self.category +'/'
convert.Convert2Yolo(self.outDir+'/', outpath, self.category, self.cla_can_temp)
tkMessageBox.showinfo("Info", "YOLO data format conversion done")
## def setImage(self, imagepath = r'test2.png'):
## self.img = Image.open(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()