-
Notifications
You must be signed in to change notification settings - Fork 2
/
YOLOLabeller.py
267 lines (242 loc) · 10 KB
/
YOLOLabeller.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
import cv2
import numpy as np
import os
import os.path as osp
from glob import glob1
import copy
import pickle
import re
def sorted_nicely( l ):
""" Sort the given iterable in the way that humans expect."""
convert = lambda text: int(text) if text.isdigit() else text
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(l, key = alphanum_key)
global sbox, ebox, img, colors, drawing
colors = [(0,0,255),(255,0,255),(255,0,0),(0,255,255)]
def on_mouse(event, x, y, flags, params):
global sbox, ebox, img, drawing
if event == cv2.EVENT_LBUTTONDOWN:
sbox = (x, y)
drawing = True
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
ebox = (x, y)
img2 = img.copy()
cv2.rectangle(img2,sbox,ebox,colors[classIdx],1)
cv2.imshow("video", img2)
elif event == cv2.EVENT_LBUTTONUP:
ebox = (x, y)
img2 = img.copy()
cv2.rectangle(img2,sbox,ebox,colors[classIdx],1)
cv2.imshow("video", img2)
drawing = False
if __name__ == '__main__':
global img, drawing
drawing = False
path = "/Users/martonszemenyei/Projects/ROBO/data/YOLO/sydney/"
names = sorted_nicely(glob1(path, "*.png"))
cv2.namedWindow("video")
cv2.setMouseCallback("video",on_mouse)
BBLists = []
classIdx = 0
for frameCntr,name in enumerate(names):
img = cv2.imread(path+name)
if img.shape[0] != 384:
img = cv2.resize(img,(512,384))
cv2.imwrite(path+name,img)
img = cv2.cvtColor(img,cv2.COLOR_RGB2BGR)
img = cv2.cvtColor(img,cv2.COLOR_YUV2BGR)
orig = img.copy()
print(frameCntr)
if len(BBLists) <= frameCntr:
BBLists.append([])#copy.deepcopy(BBLists[-1]) if len(BBLists) else [])
if osp.exists(path + name.split(".")[0] + ".txt"):
file = open(path + name.split(".")[0] + ".txt", "r")
BBLists[frameCntr] = []
while True:
line = file.readline().split(" ")
if len(line) < 5:
break
BB = []
xc = int(float(line[1])*img.shape[1])
yc = int(float(line[2])*img.shape[0])
w = int(float(line[3])*img.shape[1])
h = int(float(line[4])*img.shape[0])
BB.append((xc-w//2,yc-h//2))
BB.append((xc+w//2,yc+h//2))
BB.append(int(line[0]))
BBLists[frameCntr].append(BB)
for BB in BBLists[frameCntr]:
cv2.rectangle(img, BB[0], BB[1], colors[BB[2]], 1)
BBSel = -1
BBNum = len(BBLists[frameCntr])
drawing = False
cv2.imshow("video", img)
while True:
key = cv2.waitKey(20)
if key == 27:
exit(0)
elif key == 13:
cv2.rectangle(img, sbox, ebox, colors[classIdx], 1)
cv2.imshow("video", img)
BBLists[frameCntr].append([sbox, ebox, classIdx])
BBNum = len(BBLists[frameCntr])
# k = next image
elif key == 107:
classIdx = 0
BBSel = -1
break
# x = del all BBs
elif key == 120:
BBLists[frameCntr] = []
img = orig.copy()
cv2.imshow("video", img)
elif key == 48:
classIdx = 0
elif key == 49:
classIdx = 1
elif key == 50:
classIdx = 2
elif key == 51:
classIdx = 3
# n = next BB
elif key == 110:
if BBNum > 0:
BBSel += 1
if BBSel == BBNum:
BBSel = 0
sbox = BBLists[frameCntr][BBSel][0]
ebox = BBLists[frameCntr][BBSel][1]
img2 = img.copy()
cv2.rectangle(img2, sbox, ebox, (255,255,255), 1)
cv2.imshow("video", img2)
# w = widen vertically
elif key == 119:
if BBSel >= 0:
sbox = BBLists[frameCntr][BBSel][0]
ebox = BBLists[frameCntr][BBSel][1]
ebox = (ebox[0], ebox[1]+1)
BBLists[frameCntr][BBSel][1] = ebox
img = orig.copy()
for BB in BBLists[frameCntr]:
cv2.rectangle(img, BB[0], BB[1], colors[BB[2]], 1)
img2 = img.copy()
cv2.rectangle(img2, sbox, ebox, (255,255,255), 1)
cv2.imshow("video", img2)
# s = compress vertically
elif key == 115:
if BBSel >= 0:
sbox = BBLists[frameCntr][BBSel][0]
ebox = BBLists[frameCntr][BBSel][1]
ebox = (ebox[0], ebox[1]-1)
BBLists[frameCntr][BBSel][1] = ebox
img = orig.copy()
for BB in BBLists[frameCntr]:
cv2.rectangle(img, BB[0], BB[1], colors[BB[2]], 1)
img2 = img.copy()
cv2.rectangle(img2, sbox, ebox, (255,255,255), 1)
cv2.imshow("video", img2)
# a = widen horizontally
elif key == 97:
if BBSel >= 0:
sbox = BBLists[frameCntr][BBSel][0]
ebox = BBLists[frameCntr][BBSel][1]
ebox = (ebox[0]+1, ebox[1])
BBLists[frameCntr][BBSel][1] = ebox
img = orig.copy()
for BB in BBLists[frameCntr]:
cv2.rectangle(img, BB[0], BB[1], colors[BB[2]], 1)
img2 = img.copy()
cv2.rectangle(img2, sbox, ebox, (255,255,255), 1)
cv2.imshow("video", img2)
# d = compress horizontally
elif key == 100:
if BBSel >= 0:
sbox = BBLists[frameCntr][BBSel][0]
ebox = BBLists[frameCntr][BBSel][1]
ebox = (ebox[0]-1, ebox[1])
BBLists[frameCntr][BBSel][1] = ebox
img = orig.copy()
for BB in BBLists[frameCntr]:
cv2.rectangle(img, BB[0], BB[1], colors[BB[2]], 1)
img2 = img.copy()
cv2.rectangle(img2, sbox, ebox, (255,255,255), 1)
cv2.imshow("video", img2)
# t = move up
elif key == 116:
if BBSel >= 0:
sbox = BBLists[frameCntr][BBSel][0]
ebox = BBLists[frameCntr][BBSel][1]
sbox = (sbox[0], sbox[1]+1)
BBLists[frameCntr][BBSel][0] = sbox
img = orig.copy()
for BB in BBLists[frameCntr]:
cv2.rectangle(img, BB[0], BB[1], colors[BB[2]], 1)
img2 = img.copy()
cv2.rectangle(img2, sbox, ebox, (255,255,255), 1)
cv2.imshow("video", img2)
# g = move down
elif key == 103:
if BBSel >= 0:
sbox = BBLists[frameCntr][BBSel][0]
ebox = BBLists[frameCntr][BBSel][1]
sbox = (sbox[0], sbox[1]-1)
BBLists[frameCntr][BBSel][0] = sbox
img = orig.copy()
for BB in BBLists[frameCntr]:
cv2.rectangle(img, BB[0], BB[1], colors[BB[2]], 1)
img2 = img.copy()
cv2.rectangle(img2, sbox, ebox, (255,255,255), 1)
cv2.imshow("video", img2)
# f = move left
elif key == 102:
if BBSel >= 0:
sbox = BBLists[frameCntr][BBSel][0]
ebox = BBLists[frameCntr][BBSel][1]
sbox = (sbox[0]+1, sbox[1])
BBLists[frameCntr][BBSel][0] = sbox
img = orig.copy()
for BB in BBLists[frameCntr]:
cv2.rectangle(img, BB[0], BB[1], colors[BB[2]], 1)
img2 = img.copy()
cv2.rectangle(img2, sbox, ebox, (255,255,255), 1)
cv2.imshow("video", img2)
# h = move right
elif key == 104:
if BBSel >= 0:
sbox = BBLists[frameCntr][BBSel][0]
ebox = BBLists[frameCntr][BBSel][1]
sbox = (sbox[0]-1, sbox[1])
BBLists[frameCntr][BBSel][0] = sbox
img = orig.copy()
for BB in BBLists[frameCntr]:
cv2.rectangle(img, BB[0], BB[1], colors[BB[2]], 1)
img2 = img.copy()
cv2.rectangle(img2, sbox, ebox, (255,255,255), 1)
cv2.imshow("video", img2)
# r = remove BB
elif key == 114:
if BBSel >= 0:
BBLists[frameCntr].pop(BBSel)
BBSel = -1
img = orig.copy()
for BB in BBLists[frameCntr]:
cv2.rectangle(img, BB[0], BB[1], colors[BB[2]], 1)
cv2.imshow("video", img)
BBNum = len(BBLists[frameCntr])
file = open(path + name.split(".")[0] + ".txt","w")
for BB in BBLists[frameCntr]:
center = ((BB[0][0] + BB[1][0])/(2*img.shape[1]), (BB[0][1] + BB[1][1])/(2*img.shape[0]))
size = (abs(BB[1][0] - BB[0][0]) / img.shape[1],abs(BB[1][1] - BB[0][1]) / img.shape[0])
label = BB[2]
file.write(str(label))
file.write(" ")
file.write(str(center[0]))
file.write(" ")
file.write(str(center[1]))
file.write(" ")
file.write(str(size[0]))
file.write(" ")
file.write(str(size[1]))
file.write("\n")
file.close()