kapil commited on
Commit
40b46e0
·
1 Parent(s): 8b20043

Migrate binaries to LFS properly

Browse files
Files changed (4) hide show
  1. Cargo.lock +0 -0
  2. dataset/annotate.py +0 -410
  3. dataset/labels.json +0 -0
  4. dataset/labels.pkl +0 -3
Cargo.lock DELETED
The diff for this file is too large to render. See raw diff
 
dataset/annotate.py DELETED
@@ -1,410 +0,0 @@
1
- import os
2
- import os.path as osp
3
- import cv2
4
- import pandas as pd
5
- import numpy as np
6
- from yacs.config import CfgNode as CN
7
- import argparse
8
-
9
- # used to convert dart angle to board number
10
- BOARD_DICT = {
11
- 0: '13', 1: '4', 2: '18', 3: '1', 4: '20', 5: '5', 6: '12', 7: '9', 8: '14', 9: '11',
12
- 10: '8', 11: '16', 12: '7', 13: '19', 14: '3', 15: '17', 16: '2', 17: '15', 18: '10', 19: '6'
13
- }
14
-
15
-
16
- def crop_board(img_path, bbox=None, crop_info=(0, 0, 0), crop_pad=1.1):
17
- img = cv2.imread(img_path)
18
- if bbox is None:
19
- x, y, r = crop_info
20
- r = int(r * crop_pad)
21
- bbox = [y-r, y+r, x-r, x+r]
22
- crop = img[bbox[0]:bbox[1], bbox[2]:bbox[3]]
23
- return crop, bbox
24
-
25
-
26
- def on_click(event, x, y, flags, param):
27
- global xy, img_copy
28
- h, w = img_copy.shape[:2]
29
- if event == cv2.EVENT_LBUTTONDOWN:
30
- if len(xy) < 7:
31
- xy.append([x/w, y/h])
32
- print_xy()
33
- else:
34
- print('Already annotated 7 points.')
35
-
36
-
37
- def print_xy():
38
- global xy
39
- names = {
40
- 0: 'cal_1', 1: 'cal_2', 2: 'cal_3', 3: 'cal_4',
41
- 4: 'dart_1', 5: 'dart_2', 6: 'dart_3'}
42
- print('{}: {}'.format(names[len(xy)-1], xy[-1]))
43
-
44
-
45
- def get_ellipses(xy, r_double=0.17, r_treble=0.1074):
46
- c = np.mean(xy[:4], axis=0)
47
- a1_double = ((xy[2][0] - xy[3][0]) ** 2 + (xy[2][1] - xy[3][1]) ** 2) ** 0.5 / 2
48
- a2_double = ((xy[0][0] - xy[1][0]) ** 2 + (xy[0][1] - xy[1][1]) ** 2) ** 0.5 / 2
49
- a1_treble = a1_double * (r_treble / r_double)
50
- a2_treble = a2_double * (r_treble / r_double)
51
- angle = np.arctan((xy[3, 1] - c[1]) / (xy[3, 0] - c[0])) / np.pi * 180
52
- return c, [a1_double, a2_double], [a1_treble, a2_treble], angle
53
-
54
-
55
- def draw_ellipses(img, xy, num_pts=7):
56
- # img must be uint8
57
- xy = np.array(xy)
58
- if xy.shape[0] > num_pts:
59
- xy = xy.reshape((-1, 2))
60
- if np.mean(xy) < 1:
61
- h, w = img.shape[:2]
62
- xy[:, 0] *= w
63
- xy[:, 1] *= h
64
- c, a_double, a_treble, angle = get_ellipses(xy)
65
- angle = np.arctan((xy[3,1]-c[1])/(xy[3,0]-c[0]))/np.pi*180
66
- cv2.ellipse(img, (int(round(c[0])), int(round(c[1]))),
67
- (int(round(a_double[0])), int(round(a_double[1]))),
68
- int(round(angle)), 0, 360, (255, 255, 255))
69
- cv2.ellipse(img, (int(round(c[0])), int(round(c[1]))),
70
- (int(round(a_treble[0])), int(round(a_treble[1]))),
71
- int(round(angle)), 0, 360, (255, 255, 255))
72
- return img
73
-
74
-
75
- def get_circle(xy):
76
- c = np.mean(xy[:4], axis=0)
77
- r = np.mean(np.linalg.norm(xy[:4] - c, axis=-1))
78
- return c, r
79
-
80
-
81
- def board_radii(r_d, cfg):
82
- r_t = r_d * (cfg.board.r_treble / cfg.board.r_double) # treble radius, in px
83
- r_ib = r_d * (cfg.board.r_inner_bull / cfg.board.r_double) # inner bull radius, in px
84
- r_ob = r_d * (cfg.board.r_outer_bull / cfg.board.r_double) # outer bull radius, in px
85
- w_dt = cfg.board.w_double_treble * (r_d / cfg.board.r_double) # width of double and treble
86
- return r_t, r_ob, r_ib, w_dt
87
-
88
-
89
- def draw_circles(img, xy, cfg, color=(255, 255, 255)):
90
- c, r_d = get_circle(xy) # double radius
91
- r_t, r_ob, r_ib, w_dt = board_radii(r_d, cfg)
92
- for r in [r_d, r_d - w_dt, r_t, r_t - w_dt, r_ib, r_ob]:
93
- cv2.circle(img, (round(c[0]), round(c[1])), round(r), color)
94
- return img
95
-
96
-
97
- def transform(xy, img=None, angle=9, M=None):
98
-
99
- if xy.shape[-1] == 3:
100
- has_vis = True
101
- vis = xy[:, 2:]
102
- xy = xy[:, :2]
103
- else:
104
- has_vis = False
105
-
106
- if img is not None and np.mean(xy[:4]) < 1:
107
- h, w = img.shape[:2]
108
- xy *= [[w, h]]
109
-
110
- if M is None:
111
- c, r = get_circle(xy) # not necessarily a circle
112
- # c is center of 4 calibration points, r is mean distance from center to calibration points
113
-
114
- src_pts = xy[:4].astype(np.float32)
115
- dst_pts = np.array([
116
- [c[0] - r * np.sin(np.deg2rad(angle)), c[1] - r * np.cos(np.deg2rad(angle))],
117
- [c[0] + r * np.sin(np.deg2rad(angle)), c[1] + r * np.cos(np.deg2rad(angle))],
118
- [c[0] - r * np.cos(np.deg2rad(angle)), c[1] + r * np.sin(np.deg2rad(angle))],
119
- [c[0] + r * np.cos(np.deg2rad(angle)), c[1] - r * np.sin(np.deg2rad(angle))]
120
- ]).astype(np.float32)
121
- M = cv2.getPerspectiveTransform(src_pts, dst_pts)
122
-
123
- xyz = np.concatenate((xy, np.ones((xy.shape[0], 1))), axis=-1).astype(np.float32)
124
- xyz_dst = np.matmul(M, xyz.T).T
125
- xy_dst = xyz_dst[:, :2] / xyz_dst[:, 2:]
126
-
127
- if img is not None:
128
- img = cv2.warpPerspective(img.copy(), M, (img.shape[1], img.shape[0]))
129
- xy_dst /= [[w, h]]
130
-
131
- if has_vis:
132
- xy_dst = np.concatenate([xy_dst, vis], axis=-1)
133
-
134
- return xy_dst, img, M
135
-
136
-
137
- def get_dart_scores(xy, cfg, numeric=False):
138
- valid_cal_pts = xy[:4][(xy[:4, 0] > 0) & (xy[:4, 1] > 0)]
139
- if xy.shape[0] <= 4 or valid_cal_pts.shape[0] < 4: # missing calibration point
140
- return []
141
- xy, _, _ = transform(xy.copy(), angle=0)
142
- c, r_d = get_circle(xy)
143
- r_t, r_ob, r_ib, w_dt = board_radii(r_d, cfg)
144
- xy -= c
145
- angles = np.arctan2(-xy[4:, 1], xy[4:, 0]) / np.pi * 180
146
- angles = [a + 360 if a < 0 else a for a in angles] # map to 0-360
147
- distances = np.linalg.norm(xy[4:], axis=-1)
148
- scores = []
149
- for angle, dist in zip(angles, distances):
150
- if dist > r_d:
151
- scores.append('0')
152
- elif dist <= r_ib:
153
- scores.append('DB')
154
- elif dist <= r_ob:
155
- scores.append('B')
156
- else:
157
- number = BOARD_DICT[int(angle / 18)]
158
- if dist <= r_d and dist > r_d - w_dt:
159
- scores.append('D' + number)
160
- elif dist <= r_t and dist > r_t - w_dt:
161
- scores.append('T' + number)
162
- else:
163
- scores.append(number)
164
- if numeric:
165
- for i, s in enumerate(scores):
166
- if 'B' in s:
167
- if 'D' in s:
168
- scores[i] = 50
169
- else:
170
- scores[i] = 25
171
- else:
172
- if 'D' in s or 'T' in s:
173
- scores[i] = int(s[1:])
174
- scores[i] = scores[i] * 2 if 'D' in s else scores[i] * 3
175
- else:
176
- scores[i] = int(s)
177
- return scores
178
-
179
-
180
- def draw(img, xy, cfg, circles, score, color=(255, 255, 0)):
181
- xy = np.array(xy)
182
- if xy.shape[0] > 7:
183
- xy = xy.reshape((-1, 2))
184
- if np.mean(xy) < 1:
185
- h, w = img.shape[:2]
186
- xy[:, 0] *= w
187
- xy[:, 1] *= h
188
- if xy.shape[0] >= 4 and circles:
189
- img = draw_circles(img, xy, cfg)
190
- if xy.shape[0] > 4 and score:
191
- scores = get_dart_scores(xy, cfg)
192
- font = cv2.FONT_HERSHEY_SIMPLEX
193
- font_scale = 0.5
194
- line_type = 1
195
- for i, [x, y] in enumerate(xy):
196
- if i < 4:
197
- c = (0, 255, 0) # green
198
- else:
199
- c = color # cyan
200
- x = int(round(x))
201
- y = int(round(y))
202
- if i >= 4:
203
- cv2.circle(img, (x, y), 1, c, 1)
204
- if score:
205
- txt = str(scores[i - 4])
206
- else:
207
- txt = str(i + 1)
208
- cv2.putText(img, txt, (x + 8, y), font,
209
- font_scale, c, line_type)
210
- else:
211
- cv2.circle(img, (x, y), 1, c, 1)
212
- cv2.putText(img, str(i + 1), (x + 8, y), font,
213
- font_scale, c, line_type)
214
- return img
215
-
216
-
217
- def adjust_xy(idx):
218
- global xy, img_copy
219
- key = cv2.waitKey(0) & 0xFF
220
- xy = np.array(xy)
221
- h, w = img_copy.shape[:2]
222
- xy[:, 0] *= w; xy[:, 1] *= h
223
- if key == 52: # one pixel left
224
- if idx == -1:
225
- xy[:, 0] -= 1
226
- else:
227
- xy[idx, 0] -= 1
228
- if key == 56: # one pixel up
229
- if idx == -1:
230
- xy[:, 1] -= 1
231
- else:
232
- xy[idx, 1] -= 1
233
- if key == 54: # one pixel right
234
- if idx == -1:
235
- xy[:, 0] += 1
236
- else:
237
- xy[idx, 0] += 1
238
- if key == 50: # one pixel down
239
- if idx == -1:
240
- xy[:, 1] += 1
241
- else:
242
- xy[idx, 1] += 1
243
- xy[:, 0] /= w; xy[:, 1] /= h
244
- xy = xy.tolist()
245
-
246
-
247
- def add_last_dart(annot, data_path, folder):
248
- csv_path = osp.join(data_path, 'annotations', folder + '.csv')
249
- if osp.isfile(csv_path):
250
- dart_labels = []
251
- csv = pd.read_csv(csv_path)
252
- for idx in csv.index.values:
253
- for c in csv.columns:
254
- dart_labels.append(str(csv.loc[idx, c]))
255
- annot['last_dart'] = dart_labels
256
- return annot
257
-
258
-
259
- def get_bounding_box(img_path, scale=0.2):
260
- img = cv2.imread(img_path)
261
- img_resized = cv2.resize(img, None, fx=scale, fy=scale)
262
- h, w = img_resized.shape[:2]
263
- xy_bbox = []
264
-
265
- def on_click_bbox(event, x, y, flags, param):
266
- if event == cv2.EVENT_LBUTTONDOWN:
267
- if len(xy_bbox) < 2:
268
- xy_bbox.append([
269
- round((x / w) * img.shape[1]),
270
- round((y / h) * img.shape[0])])
271
-
272
- window = 'get bbox'
273
- cv2.namedWindow(window)
274
- cv2.setMouseCallback(window, on_click_bbox)
275
- while len(xy_bbox) < 2:
276
- # print(xy_bbox)
277
- cv2.imshow(window, img_resized)
278
- key = cv2.waitKey(100)
279
- if key == ord('q'): # quit
280
- cv2.destroyAllWindows()
281
- break
282
- cv2.destroyAllWindows()
283
- assert len(xy_bbox) == 2, 'click 2 points to get bounding box'
284
- xy_bbox = np.array(xy_bbox)
285
- # bbox = [y1 y2 x1 x2]
286
- bbox = [min(xy_bbox[:, 1]), max(xy_bbox[:, 1]), min(xy_bbox[:, 0]), max(xy_bbox[:, 0])]
287
- return bbox
288
-
289
-
290
- def main(cfg, folder, scale, draw_circles, dart_score=True):
291
- global xy, img_copy
292
- img_dir = osp.join(cfg.data.path, 'images', folder)
293
- imgs = sorted(os.listdir(img_dir))
294
- annot_path = osp.join(cfg.data.path, 'annotations', folder + '.pkl')
295
- if osp.isfile(annot_path):
296
- annot = pd.read_pickle(annot_path)
297
- else:
298
- annot = pd.DataFrame(columns=['img_name', 'bbox', 'xy'])
299
- annot['img_name'] = imgs
300
- annot['bbox'] = None
301
- annot['xy'] = None
302
- annot = add_last_dart(annot, cfg.data.path, folder)
303
-
304
- i = 0
305
- for j in range(len(annot)):
306
- a = annot.iloc[j,:]
307
- if a['bbox'] is not None:
308
- i = j
309
-
310
- while i < len(imgs):
311
- xy = []
312
- a = annot.iloc[i,:]
313
- print('Annotating {}'.format(a['img_name']))
314
- if a['bbox'] is None:
315
- if i == 0:
316
- bbox = get_bounding_box(osp.join(img_dir, a['img_name']))
317
- if i > 0:
318
- last_a = annot.iloc[i-1,:]
319
- if last_a['xy'] is not None:
320
- xy = last_a['xy'].copy()
321
- else:
322
- xy = []
323
- else:
324
- bbox, xy = a['bbox'], a['xy']
325
-
326
- crop, _ = crop_board(osp.join(img_dir, a['img_name']), bbox=bbox)
327
- crop = cv2.resize(crop, (int(crop.shape[1] * scale), int(crop.shape[0] * scale)))
328
- cv2.putText(crop, '{}/{} {}'.format(i+1, len(annot), a['img_name']), (0, 12), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
329
- img_copy = crop.copy()
330
-
331
- cv2.namedWindow(folder)
332
- cv2.setMouseCallback(folder, on_click)
333
- while True:
334
- img_copy = draw(img_copy, xy, cfg, draw_circles, dart_score)
335
- cv2.imshow(folder, img_copy)
336
- key = cv2.waitKey(100) & 0xFF # update every 100 ms
337
-
338
- if key == ord('q'): # quit
339
- cv2.destroyAllWindows()
340
- i = len(imgs)
341
- break
342
-
343
- if key == ord('b'): # draw new bounding box
344
- idx = annot[(annot['img_name'] == a['img_name'])].index.values[0]
345
- annot.at[idx, 'bbox'] = get_bounding_box(osp.join(img_dir, a['img_name']), scale)
346
- break
347
-
348
- if key == ord('.'):
349
- i += 1
350
- img_copy = crop.copy()
351
- break
352
-
353
- if key == ord(','):
354
- if i > 0:
355
- i += -1
356
- img_copy = crop.copy()
357
- break
358
-
359
- if key == ord('z'): # undo keypoint
360
- xy = xy[:-1]
361
- img_copy = crop.copy()
362
-
363
- if key == ord('x'): # reset annotation
364
- idx = annot[(annot['img_name'] == a['img_name'])].index.values[0]
365
- annot.at[idx, 'xy'] = None,
366
- annot.at[idx, 'bbox'] = None
367
- annot.to_pickle(annot_path)
368
- break
369
-
370
- if key == ord('d'): # delete img
371
- print('Are you sure you want to delete this image? (y/n)')
372
- key = cv2.waitKey(0) & 0xFF
373
- if key == ord('y'):
374
- idx = annot[(annot['img_name'] == a['img_name'])].index.values[0]
375
- annot = annot.drop([idx])
376
- annot.to_pickle(annot_path)
377
- os.remove(osp.join(img_dir, a['img_name']))
378
- print('Deleted image {}'.format(a['img_name']))
379
- break
380
- else:
381
- print('Image not deleted.')
382
- continue
383
-
384
- if key == ord('a'): # accept keypoints
385
- idx = annot[(annot['img_name'] == a['img_name'])].index.values[0]
386
- annot.at[idx, 'xy'] = xy
387
- annot.at[idx, 'bbox'] = bbox
388
- annot.to_pickle(annot_path)
389
- i += 1
390
- break
391
-
392
- if key in [ord('1'), ord('2'), ord('3'), ord('4'), ord('5'), ord('6'), ord('7'), ord('0')]:
393
- adjust_xy(idx=key - 49) # ord('1') = 49
394
- img_copy = crop.copy()
395
- continue
396
-
397
-
398
- if __name__ == '__main__':
399
- import sys
400
- sys.path.append('../../')
401
- parser = argparse.ArgumentParser()
402
- parser.add_argument('-f', '--img-folder', default='d2_04_05_2020')
403
- parser.add_argument('-s', '--scale', type=float, default=0.5)
404
- parser.add_argument('-d', '--draw-circles', action='store_true')
405
- args = parser.parse_args()
406
-
407
- cfg = CN(new_allowed=True)
408
- cfg.merge_from_file('../configs/tiny480_20e.yaml')
409
-
410
- main(cfg, args.img_folder, args.scale, args.draw_circles)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dataset/labels.json DELETED
The diff for this file is too large to render. See raw diff
 
dataset/labels.pkl DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:1bbde9f5cbfa1d623884c86210154867f99d3589309cc062476884952ac4c935
3
- size 2791670