소스 검색

add missing 'bbox' field in COCO annotation

"bbox": [x, y, width, height]
bbox coordinates are measured from the top left image corner and 0-indexed
Lingjie Zhu 6 년 전
부모
커밋
aef5216557
1개의 변경된 파일10개의 추가작업 그리고 2개의 파일을 삭제
  1. 10 2
      examples/instance_segmentation/labelme2coco.py

+ 10 - 2
examples/instance_segmentation/labelme2coco.py

@@ -136,13 +136,21 @@ def main():
             mask = pycocotools.mask.encode(mask)
             area = float(pycocotools.mask.area(mask))
 
+            bbox = []
+            for points in segmentations[label]:
+                bbox.extend(points)
+            bbox = np.asarray(bbox).reshape((-1, 2))
+            xmin, ymin = np.amin(bbox, axis=0).tolist();
+            xmax, ymax = np.amax(bbox, axis=0).tolist();
+
             data['annotations'].append(dict(
                 id=len(data['annotations']),
+                image_id=image_id,
+                category_id=cls_id,
                 segmentation=segmentations[label],
                 area=area,
+                bbox=[xmin, ymin, xmax - xmin, ymax - ymin],
                 iscrowd=None,
-                image_id=image_id,
-                category_id=cls_id,
             ))
 
     with open(out_ann_file, 'w') as f: