forked from Azure/ObjectDetectionUsingCntk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA2_annotateBboxLabels.py
91 lines (74 loc) · 2.8 KB
/
A2_annotateBboxLabels.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
# -*- coding: utf-8 -*-
import cv2, os, sys, time, importlib
from tkinter import *
from PIL import ImageTk
import PARAMETERS
locals().update(importlib.import_module("PARAMETERS").__dict__)
####################################
# Parameters
####################################
imagesToAnnotateDir = "C:/Users/pabuehle/Desktop/newImgs/"
#no need to change these
boxWidth = 10
boxHeight = 2
drawingMaxImgSize = 1000
objectNames = classes[1:]
objectNames = np.sort(objectNames).tolist()
objectNames += ["UNDECIDED", "EXCLUDE"]
####################################
# Helper functions
####################################
def buttonPressedCallback(s):
global tkLastButtonPressed
global tkBoButtonPressed
tkLastButtonPressed = s
tkBoButtonPressed = True
####################################
# Main
####################################
#create UI
tk = Tk()
w = Canvas(tk, width=len(objectNames) * boxWidth, height=len(objectNames) * boxHeight, bd = boxWidth, bg = 'white')
w.grid(row = len(objectNames), column = 0, columnspan = 2)
for objectIndex,objectName in enumerate(objectNames):
b = Button(width=boxWidth, height=boxHeight, text=objectName, command=lambda s = objectName: buttonPressedCallback(s))
b.grid(row = objectIndex, column = 0)
#loop over all images
imgFilenames = getFilesInDirectory(imagesToAnnotateDir, ".jpg")
for imgIndex, imgFilename in enumerate(imgFilenames):
print("imgIndex={}, imgFilename={}".format(imgIndex, imgFilename))
labelsPath = imagesToAnnotateDir + "/" + imgFilename[:-4] + ".bboxes.labels.tsv"
if os.path.exists(labelsPath):
continue
#load image and bboxes
imgPath = imagesToAnnotateDir + "/" + imgFilename
print("imgIndex = {}, imgPath = {}".format(imgIndex, imgPath))
img = imread(imgPath)
rectsPath = imgPath = imagesToAnnotateDir + "/" + imgFilename[:-4] + ".bboxes.tsv"
rects = readTable(rectsPath)
rects = [ToIntegers(rect) for rect in rects]
#annotate each rectangle in turn
labels = []
for rectIndex,rect in enumerate(rects):
imgCopy = img.copy()
drawRectangles(imgCopy, [rect], thickness = 15)
#draw image in tk window
imgTk, _ = imresizeMaxDim(imgCopy, drawingMaxImgSize)
imgTk = imconvertCv2Pil(imgTk)
imgTk = ImageTk.PhotoImage(imgTk)
label = Label(tk, image=imgTk)
label.grid(row=0, column=1, rowspan=drawingMaxImgSize)
tk.update_idletasks()
tk.update()
#busy-wait until button pressed
tkBoButtonPressed = False
tkLastButtonPressed = None
while not tkBoButtonPressed:
tk.update_idletasks()
tk.update()
#store result
print("tkLastButtonPressed", tkLastButtonPressed)
labels.append(tkLastButtonPressed)
writeFile(labelsPath, labels)
tk.destroy()
print("DONE.")