-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabeling.py
executable file
·187 lines (162 loc) · 4.94 KB
/
labeling.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
#! /usr/bin/env python3
import cv2
import numpy as np
import math
from time import sleep
import copy
import iterate_images
im = np.array
cmask = np.array
# im = cv2.imread('depth_images/slika1_Color.png')
# cmask = np.zeros([im.shape[0], im.shape[1], 3], dtype=np.uint8)
initial_tags = {
'buoy': {
'red': [],
'green': []
},
'dock': {
'red': [],
'green': []
}
}
initial_color = 'red'
initial_action = 'dock'
tags = copy.deepcopy(initial_tags)
color = initial_color
action = initial_action
done = False
def toggle_color():
global color
color = 'green' if color == 'red' else 'red'
def toggle_action():
global action
action = 'dock' if action == 'buoy' else 'buoy'
def controls() -> np.array:
global color, action, tags
controls = np.full([100, im.shape[1], 3], (32, 32, 32), dtype=np.uint8)
if not done:
count = len(tags[action][color])
c = (16, 30,187) if color == 'red' else (67, 111, 0)
controls = cv2.circle(controls, (60, 50), 40, c, thickness=-1)
controls = cv2.putText(
controls, '%s [next: %i]' % (action, count),
(120, 70), cv2.QT_FONT_NORMAL, 2,
(255, 255, 255), thickness=2
)
else:
controls = cv2.putText(
controls,
'done, press [V] for next picture',
(10, 70), cv2.QT_FONT_NORMAL, 2,
(255, 255, 255), thickness=2
)
return controls
undo_stack = []
def undo():
global tags
if len(undo_stack) == 0:
return
last = undo_stack.pop()
if len(tags[last[0]][last[1]]) == 0:
return
tags[last[0]][last[1]].pop()
auto_change()
render_tags()
def clicker(event, x, y, flags, param):
global tags
if event == cv2.EVENT_LBUTTONDOWN and not done:
tags[action][color].append((x, y))
undo_stack.append((action, color))
elif event == cv2.EVENT_RBUTTONDOWN and not done:
tags[action][color].append(None)
undo_stack.append((action, color))
auto_change()
render_tags()
def auto_change():
return
global tags, action, color, done
done = False
if len(tags['dock']['red']) < 4:
action = 'dock'
color = 'red'
elif len(tags['dock']['red']) == 4:
action = 'dock'
color = 'green'
if len(tags['dock']['green']) < 4:
action = 'dock'
color = 'green'
elif len(tags['dock']['green']) == 4:
action = 'buoy'
color = 'red'
done = True
# if len(tags['buoy']['red']) < 2:
# action = 'buoy'
# color = 'red'
# elif len(tags['buoy']['red']) == 2:
# action = 'buoy'
# color = 'green'
# if len(tags['buoy']['green']) < 2:
# action = 'buoy'
# color = 'green'
# else:
# done = True
output = np.array
def render_tags():
global tags, cmask
cmask = np.zeros([im.shape[0], im.shape[1], 3], dtype=np.uint8)
for dkey in tags:
for ckey, color in tags[dkey].items():
for tag in range(len(color)):
if color[tag] is None:
continue
x, y = color[tag]
c = (16, 30,187) if ckey == 'red' else (67, 111, 0)
if dkey == 'buoy':
cmask = cv2.circle(cmask, (x, y), 20, c, thickness=-1)
cmask = cv2.circle(cmask, (x, y), 15, (255, 255, 255), thickness=2)
elif dkey == 'dock':
cmask = cv2.rectangle(cmask, (x - 17, y - 17), (x + 17, y + 17), c, thickness=-1)
cmask = cv2.rectangle(cmask, (x - 12, y - 12), (x + 12, y + 12), (255, 255, 255), thickness=2)
cmask = cv2.putText(
cmask,
str(tag),
(x - 5 - math.floor(math.log(tag if tag > 0 else 1, 10)) * 8, y + 5),
cv2.QT_FONT_NORMAL,
0.5, (255, 255, 255), thickness=2
)
cv2.namedWindow('image', cv2.WINDOW_GUI_NORMAL)
cv2.setMouseCallback('image', clicker)
if __name__ == '__main__':
for rgb, depth in iterate_images.next_image(7, 1000):
im = rgb
cmask = np.zeros([im.shape[0], im.shape[1], 3], dtype=np.uint8)
output = np.zeros([im.shape[0] + 100, im.shape[1], 3], dtype=np.uint8)
undo_stack = []
while True:
output[0:im.shape[0],:,:] = im.copy()
output[im.shape[0]:,:,:] = controls()
bmask = cv2.add(cmask[:,:,0], cv2.add(cmask[:,:,1], cmask[:,:,2]))
bmask[bmask > 0] = 255
bmask = cv2.bitwise_not(bmask)
output[0:im.shape[0],:,0] = cv2.bitwise_and(output[0:im.shape[0],:,0], bmask)
output[0:im.shape[0],:,1] = cv2.bitwise_and(output[0:im.shape[0],:,1], bmask)
output[0:im.shape[0],:,2] = cv2.bitwise_and(output[0:im.shape[0],:,2], bmask)
output[0:im.shape[0],:,:] = cv2.add(output[0:im.shape[0],:,:], cmask)
cv2.imshow('image', output)
key = cv2.waitKey(1) & 0xFF
if key == ord('c'):
toggle_color()
if key == ord('x'):
toggle_action()
if key == ord('z'):
undo()
if key == ord('v') or done:
print(tags)
tags = copy.deepcopy(initial_tags)
print(initial_tags)
color = initial_color
action = initial_action
done = False
break
if key == ord('q'):
exit()