Repository: open-mmlab/mmpose

Issue:
False 'segmentation' labeled in 'macaque_train/test.json'
In general, the macaque dataset is missing parts of segmentation.

猴子的数据集：每当身体位置是分开的时候，‘'segmentation’ 的标注都有问题。我check了原始csv标注数据，是正确的；是你们制作的“macaque_train/test.json”出现了偏差。

![image](https://user-images.githubusercontent.com/17546446/135393754-c2539040-10d6-4e24-a3ca-b6bf721287ed.png)



Discussion:
Thanks for reporting this issue. We will check this later.

Resolving patch:
diff --git a/tools/dataset/parse_macaquepose_dataset.py b/tools/dataset/parse_macaquepose_dataset.py
--- a/tools/dataset/parse_macaquepose_dataset.py
+++ b/tools/dataset/parse_macaquepose_dataset.py
@@ -10,14 +10,22 @@
 np.random.seed(0)
 
 
-def PolyArea(x, y):
+def get_poly_area(x, y):
     """Calculate area of polygon given (x,y) coordinates (Shoelace formula)
 
     :param x: np.ndarray(N, )
     :param y: np.ndarray(N, )
     :return: area
     """
-    return 0.5 * np.abs(np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1)))
+    return float(0.5 *
+                 np.abs(np.dot(x, np.roll(y, 1)) - np.dot(y, np.roll(x, 1))))
+
+
+def get_seg_area(segmentations):
+    area = 0
+    for segmentation in segmentations:
+        area += get_poly_area(segmentation[:, 0], segmentation[:, 1])
+    return area
 
 
 def save_coco_anno(data_annotation,
@@ -63,9 +71,26 @@ def save_coco_anno(data_annotation,
                     keypoints[ind, 1] = p['position'][1]
                     keypoints[ind, 2] = 2
 
-            segmentation = np.array(seg[0]['segment'])
-            max_x, max_y = segmentation.max(0)
-            min_x, min_y = segmentation.min(0)
+            segmentations = []
+
+            max_x = -1
+            max_y = -1
+            min_x = 999999
+            min_y = 999999
+            for segm in seg:
+                if len(segm['segment']) == 0:
+                    continue
+
+                segmentation = np.array(segm['segment'])
+                segmentations.append(segmentation)
+
+                _max_x, _max_y = segmentation.max(0)
+                _min_x, _min_y = segmentation.min(0)
+
+                max_x = max(max_x, _max_x)
+                max_y = max(max_y, _max_y)
+                min_x = min(min_x, _min_x)
+                min_y = min(min_y, _min_y)
 
             anno = {}
             anno['keypoints'] = keypoints.reshape(-1).tolist()
@@ -79,10 +104,11 @@ def save_coco_anno(data_annotation,
                 float(max_y - min_y + 1)
             ]
             anno['iscrowd'] = 0
-            anno['area'] = float(
-                PolyArea(segmentation[:, 0], segmentation[:, 1]))
+            anno['area'] = get_seg_area(segmentations)
             anno['category_id'] = 1
-            anno['segmentation'] = segmentation.reshape([1, -1]).tolist()
+            anno['segmentation'] = [
+                seg.reshape(-1).tolist() for seg in segmentations
+            ]
 
             annotations.append(anno)
             ann_id += 1
@@ -133,7 +159,6 @@ def save_coco_anno(data_annotation,
 
 
 dataset_dir = '/data/macaque/'
-
 with open(os.path.join(dataset_dir, 'annotations.csv'), 'r') as fp:
     data_annotation_all = list(csv.reader(fp, delimiter=','))[1:]
 


Test patch: