Skip to content

Commit ed43099

Browse files
Make sure that the bbox element is filled and categories are properly loaded.
1 parent 6e88f45 commit ed43099

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

backend/webserver/api/annotations.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
create_annotation.add_argument('metadata', type=dict, location='json')
1919
create_annotation.add_argument('segmentation', type=list, location='json')
2020
create_annotation.add_argument('keypoints', type=list, location='json')
21+
create_annotation.add_argument('bbox', type=list, location='json')
2122
create_annotation.add_argument('color', location='json')
2223

2324
update_annotation = reqparse.RequestParser()
@@ -42,11 +43,12 @@ def post(self):
4243
metadata = args.get('metadata', {})
4344
segmentation = args.get('segmentation', [])
4445
keypoints = args.get('keypoints', [])
46+
bbox = args.get('bbox', [])
4547

4648
image = current_user.images.filter(id=image_id, deleted=False).first()
4749
if image is None:
4850
return {"message": "Invalid image id"}, 400
49-
51+
5052
logger.info(
5153
f'{current_user.username} has created an annotation for image {image_id} with {isbbox}')
5254
logger.info(
@@ -59,6 +61,7 @@ def post(self):
5961
metadata=metadata,
6062
segmentation=segmentation,
6163
keypoints=keypoints,
64+
bbox=bbox,
6265
isbbox=isbbox
6366
)
6467
annotation.save()

backend/webserver/api/annotator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def post(self):
3030
image = data.get('image')
3131
dataset = data.get('dataset')
3232
image_id = image.get('id')
33-
33+
3434
image_model = ImageModel.objects(id=image_id).first()
3535

3636
if image_model is None:
@@ -40,9 +40,9 @@ def post(self):
4040
db_dataset = current_user.datasets.filter(id=image_model.dataset_id).first()
4141
if dataset is None:
4242
return {'success': False, 'message': 'Could not find associated dataset'}
43-
43+
4444
db_dataset.update(annotate_url=dataset.get('annotate_url', ''))
45-
45+
4646
categories = CategoryModel.objects.all()
4747
annotations = AnnotationModel.objects(image_id=image_id)
4848

@@ -64,7 +64,7 @@ def post(self):
6464
category_update['keypoint_edges'] = category.get('keypoint_edges', [])
6565
category_update['keypoint_labels'] = category.get('keypoint_labels', [])
6666
category_update['keypoint_colors'] = category.get('keypoint_colors', [])
67-
67+
6868
db_category.update(**category_update)
6969

7070
# Iterate every annotation from the data annotations
@@ -95,6 +95,7 @@ def post(self):
9595
sessions.append(model)
9696

9797
keypoints = annotation.get('keypoints', [])
98+
bbox = annotation.get('bbox', [])
9899
if keypoints:
99100
counted = True
100101

@@ -103,6 +104,7 @@ def post(self):
103104
inc__milliseconds=total_time,
104105
set__isbbox=annotation.get('isbbox', False),
105106
set__keypoints=keypoints,
107+
set__bbox=bbox,
106108
set__metadata=annotation.get('metadata'),
107109
set__color=annotation.get('color')
108110
)
@@ -203,5 +205,3 @@ def get(self, image_id):
203205
data.get('categories').append(category)
204206

205207
return data
206-
207-

backend/webserver/util/coco_util.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def paperjs_to_coco(image_width, image_height, paperjs):
3434
compound_path = {"children": [paperjs]}
3535
else:
3636
compound_path = paperjs[1]
37-
37+
3838
children = compound_path.get('children', [])
3939

4040
for child in children:
@@ -43,11 +43,11 @@ def paperjs_to_coco(image_width, image_height, paperjs):
4343
segments_to_add = []
4444

4545
for point in child_segments:
46-
46+
4747
# Cruve
4848
if len(point) == 4:
4949
point = point[0]
50-
50+
5151
# Point
5252
if len(point) == 2:
5353
x = round(center[0] + point[0], 2)
@@ -102,7 +102,7 @@ def paperjs_to_coco_cliptobounds(image_width, image_height, paperjs): # todo: th
102102
compound_path = {"children": [paperjs]}
103103
else:
104104
compound_path = paperjs[1]
105-
105+
106106
children = compound_path.get('children', [])
107107

108108
for child in children:
@@ -124,7 +124,7 @@ def paperjs_to_coco_cliptobounds(image_width, image_height, paperjs): # todo: th
124124
inside = True
125125
break
126126
i_start += 1
127-
127+
128128
if inside: # if point is inside the canvas. Otherwise ignore it
129129
edges = {
130130
'w_0': np.array([[0,0],[image_width, 0]], np.float),
@@ -136,12 +136,12 @@ def paperjs_to_coco_cliptobounds(image_width, image_height, paperjs): # todo: th
136136
for i in range(i_start, i_start + len(child_segments)):
137137
p = i % len(child_segments)
138138
point = child_segments[p]
139-
139+
140140
# print('point:', point, flush=True)
141141
# Cruve
142142
if len(point) == 4:
143143
point = point[0]
144-
144+
145145
# Point
146146
if len(point) == 2:
147147
x = round(center[0] + point[0], 2)
@@ -221,7 +221,7 @@ def get_image_coco(image_id):
221221
"""
222222
image = ImageModel.objects(id=image_id)\
223223
.only(*ImageModel.COCO_PROPERTIES)
224-
224+
225225
image = fix_ids(image)[0]
226226
dataset = DatasetModel.objects(id=image.get('dataset_id')).first()
227227

@@ -239,10 +239,10 @@ def get_image_coco(image_id):
239239
category_annotations = db_annotations\
240240
.filter(category_id=category.get('id'))\
241241
.only(*AnnotationModel.COCO_PROPERTIES)
242-
242+
243243
if category_annotations.count() == 0:
244244
continue
245-
245+
246246
category_annotations = fix_ids(category_annotations)
247247
for annotation in category_annotations:
248248

@@ -256,7 +256,7 @@ def get_image_coco(image_id):
256256
arr = np.array(annotation.get('keypoints', []))
257257
arr = arr[2::3]
258258
annotation['num_keypoints'] = len(arr[arr > 0])
259-
259+
260260
annotations.append(annotation)
261261

262262
if len(category.get('keypoint_labels')) > 0:
@@ -265,7 +265,7 @@ def get_image_coco(image_id):
265265
else:
266266
del category['keypoint_edges']
267267
del category['keypoint_labels']
268-
268+
269269
categories.append(category)
270270

271271
coco = {
@@ -323,8 +323,9 @@ def get_dataset_coco(dataset):
323323

324324
has_keypoints = len(annotation.get('keypoints', [])) > 0
325325
has_segmentation = len(annotation.get('segmentation', [])) > 0
326+
has_bbox = len(annotation.get('bbox', [])) > 0
326327

327-
if has_keypoints or has_segmentation:
328+
if has_keypoints or has_segmentation or has_bbox:
328329
del annotation['deleted']
329330

330331
if not has_keypoints:
@@ -333,7 +334,7 @@ def get_dataset_coco(dataset):
333334
arr = np.array(annotation.get('keypoints', []))
334335
arr = arr[2::3]
335336
annotation['num_keypoints'] = len(arr[arr > 0])
336-
337+
337338
coco.get('annotations').append(annotation)
338339

339340
image = fix_ids(image)

backend/workers/tasks/data.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ def export_annotations(task_id, dataset_id, categories):
9090

9191
has_keypoints = len(annotation.get('keypoints', [])) > 0
9292
has_segmentation = len(annotation.get('segmentation', [])) > 0
93+
has_bbox = len(annotation.get('bbox', [])) > 0
9394

94-
if has_keypoints or has_segmentation:
95+
if has_keypoints or has_segmentation or has_bbox:
9596

9697
if not has_keypoints:
9798
if 'keypoints' in annotation:
@@ -231,18 +232,17 @@ def import_annotations(task_id, dataset_id, coco_json):
231232
category_id = annotation.get('category_id')
232233
segmentation = annotation.get('segmentation', [])
233234
keypoints = annotation.get('keypoints', [])
234-
bboxes = annotation.get('bbox', [])
235235
# is_crowd = annotation.get('iscrowed', False)
236236
area = annotation.get('area', 0)
237-
bbox = annotation.get('bbox', [0, 0, 0, 0])
238-
isbbox = annotation.get('isbbox', False) if len(bboxes) == 0 else len(bboxes) > 0
237+
bbox = annotation.get('bbox', [])
238+
isbbox = annotation.get('isbbox', False) if len(bbox) == 0 else True
239239

240240
progress += 1
241241
task.set_progress((progress / total_items) * 100, socket=socket)
242242

243243
has_segmentation = len(segmentation) > 0
244244
has_keypoints = len(keypoints) > 0
245-
has_bbox = len(bboxes) > 0
245+
has_bbox = len(bbox) > 0
246246
if not has_segmentation and not has_keypoints and not has_bbox:
247247
task.warning(
248248
f"Annotation {annotation.get('id')} has none of segmentation, keypoints and bounding boxes")
@@ -261,7 +261,8 @@ def import_annotations(task_id, dataset_id, coco_json):
261261
image_id=image_model.id,
262262
category_id=category_model_id,
263263
segmentation=segmentation,
264-
keypoints=keypoints
264+
keypoints=keypoints,
265+
bbox=bbox
265266
).first()
266267

267268
if annotation_model is None:
@@ -273,7 +274,7 @@ def import_annotations(task_id, dataset_id, coco_json):
273274
annotation_model.color = annotation.get('color')
274275
annotation_model.metadata = annotation.get('metadata', {})
275276

276-
if has_segmentation:
277+
if has_segmentation or has_bbox:
277278
annotation_model.segmentation = segmentation
278279
annotation_model.area = area
279280
annotation_model.bbox = bbox

0 commit comments

Comments
 (0)