-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathannotate.py
89 lines (72 loc) · 1.93 KB
/
annotate.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
import numpy as np
import matplotlib.pyplot as plt
import cv2, os
from roipoly import roipoly
import pdb
############################
## MODIFY THESE VARIABLES ##
############################
inFolder = "2018Proj1_train/"
outFolder = "labeled_data/"
colorClass = "RedBarrel/"
############################
retry = True
rois = []
def on_keypress(event, datafilename, img):
global retry
global rois
if event.key == 'n':
# save
imgSize = np.shape(img)
mask = np.zeros(imgSize[0:2], dtype=bool)
for roi in rois:
mask = np.logical_or(mask, roi.getMask2(imgSize))
np.save(datafilename, mask)
print("Saving " + datafilename)
plt.close()
elif event.key == 'q':
print("Quitting")
exit()
elif event.key == 'r':
# retry
print("Retry annotation")
rois = []
retry = True
plt.close()
elif event.key == 'a':
# add
print("Add another annotation")
retry = True
plt.close()
if __name__ == '__main__':
inFolderPath = inFolder
outFolderPath = outFolder + colorClass
for filename in os.listdir(inFolderPath):
basename, extension = os.path.splitext(filename)
if (extension != ".png"):
continue
textfile = outFolderPath + basename + ".npy"
if os.path.isfile(textfile):
continue
bgrImage = cv2.imread(inFolderPath + filename)
rgbImg = cv2.cvtColor(bgrImage, cv2.COLOR_BGR2RGB)
rois = []
retry = True
while (retry):
retry = False
plt.cla()
# draw region of interest
plt.imshow(rgbImg, interpolation='none')
for roi in rois:
roi.displayROI()
plt.title(basename)
rois.append(roipoly(roicolor='r')) #let user draw ROI
fig = plt.gcf()
fig.canvas.mpl_connect('key_press_event', \
lambda event: on_keypress(event, outFolderPath + basename, rgbImg))
plt.cla()
plt.imshow(rgbImg, interpolation='none')
for roi in rois:
roi.displayROI()
plt.title("press \'n\' to save and go to next picture, \'r\' to retry \n \'q\' to quit, \'a\' to add another region")
plt.show()