Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Images/
Labels/
Binary file removed Images/001/test.JPEG
Binary file not shown.
Binary file removed Images/001/test2.JPEG
Binary file not shown.
Binary file removed Images/001/test3.JPEG
Binary file not shown.
2 changes: 0 additions & 2 deletions Labels/001/test.txt

This file was deleted.

2 changes: 0 additions & 2 deletions Labels/001/test2.txt

This file was deleted.

2 changes: 0 additions & 2 deletions Labels/001/test3.txt

This file was deleted.

50 changes: 21 additions & 29 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ def __init__(self, master):
self.outDir = ''
self.cur = 0
self.total = 0
self.category = 0
self.category = ''
self.imagename = ''
self.labelfilename = ''
self.tkimg = None
self.imagescale = 1.0

# initialize mouse state
self.STATE = {}
Expand Down Expand Up @@ -123,54 +124,45 @@ def loadDir(self, dbg = False):
if not dbg:
s = self.entry.get()
self.parent.focus()
self.category = int(s)
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', '%03d' %(self.category))
self.imageList = glob.glob(os.path.join(self.imageDir, '*.JPEG'))
self.imageDir = os.path.join(r'./Images', '%s' %(self.category))
self.imageList = glob.glob(os.path.join(self.imageDir, '*'))
if len(self.imageList) == 0:
print 'No .JPEG images found in the specified dir!'
print 'No .jpg 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'./Labels', '%03d' %(self.category))
self.outDir = os.path.join(r'./Labels', '%s' %(self.category))
if not os.path.exists(self.outDir):
os.mkdir(self.outDir)

# 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, '*.JPEG'))
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 loadImage(self):
# load image
imagepath = self.imageList[self.cur - 1]
self.img = Image.open(imagepath)
self.tkimg = ImageTk.PhotoImage(self.img)

self.imagescale = 1.0

# calculate the scale factor.
if max(self.img.height, self.img.width) > 1280:
self.imagescale = max(self.img.height, self.img.width) / 1280.0

scaledimg = self.img.resize((int(self.img.width / self.imagescale), int(self.img.height / self.imagescale)), Image.ANTIALIAS)

self.tkimg = ImageTk.PhotoImage(scaledimg)
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))
Expand All @@ -190,8 +182,8 @@ def loadImage(self):
tmp = [int(t.strip()) for t in line.split()]
## print tmp
self.bboxList.append(tuple(tmp))
tmpId = self.mainPanel.create_rectangle(tmp[0], tmp[1], \
tmp[2], tmp[3], \
tmpId = self.mainPanel.create_rectangle(tmp[0] / self.imagescale, tmp[1] / self.imagescale, \
tmp[2] / self.imagescale, tmp[3] / self.imagescale, \
width = 2, \
outline = COLORS[(len(self.bboxList)-1) % len(COLORS)])
self.bboxIdList.append(tmpId)
Expand All @@ -212,10 +204,10 @@ def mouseClick(self, event):
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.bboxList.append((int(x1 * self.imagescale), int(y1 * self.imagescale), int(x2 * self.imagescale), int(y2 * self.imagescale)))
self.bboxIdList.append(self.bboxId)
self.bboxId = None
self.listbox.insert(END, '(%d, %d) -> (%d, %d)' %(x1, y1, x2, y2))
self.listbox.insert(END, '(%d, %d) -> (%d, %d)' %(x1 * self.imagescale, y1 * self.imagescale, x2 * self.imagescale, y2 * self.imagescale))
self.listbox.itemconfig(len(self.bboxIdList) - 1, fg = COLORS[(len(self.bboxIdList) - 1) % len(COLORS)])
self.STATE['click'] = 1 - self.STATE['click']

Expand Down