ryouchinsa / Rectlabel-support

RectLabel is an offline image annotation tool for object detection and segmentation.
https://rectlabel.com
504 stars 73 forks source link

Getting no label file with if ann['iscrowd']: continue statement. #241

Closed mimansa1907 closed 8 months ago

mimansa1907 commented 9 months ago

I am trying to convert the COCO1.0 annotation files generated in CVAT to Yolo format. The COCO json file created consists of segmentation masks in RLE format therefore 'iscrowd' variable is True across all annotations. Based on the code given in the function convert_coco_json, the statement if ann['iscrowd']: continue will skip the rest of the code of for loop statement in for ann in anns:. I commented the if condition and after that I am able to create text files, so what is exactly the purpose of this if statement in the function.

Code Snippet :

for ann in anns:
                if ann['iscrowd']:  **## Significance of this statement?**
                   continue
                # The COCO box format is [top left x, top left y, width, height]
                box = np.array(ann['bbox'], dtype=np.float64)
                box[:2] += box[2:] / 2  # xy top-left corner to center
                box[[0, 2]] /= w  # normalize x
                box[[1, 3]] /= h  # normalize y
                if box[2] <= 0 or box[3] <= 0:  # if w <= 0 and h <= 0
                    continue

                cls = coco80[ann['category_id'] - 1] if cls91to80 else ann['category_id'] - 1  # class
                box = [cls] + box.tolist()
                if box not in bboxes:
                    bboxes.append(box)
                if use_segments:
                    if len(ann['segmentation']) == 0:
                        segments.append([])
                        continue
                    if isinstance(ann['segmentation'], dict):
                        ann['segmentation'] = rle2polygon(ann['segmentation'])
                    if len(ann['segmentation']) > 1:
                        s = merge_multi_segment(ann['segmentation'])
                        s = (np.concatenate(s, axis=0) / np.array([w, h])).reshape(-1).tolist()
                    else:
                        s = [j for i in ann['segmentation'] for j in i]  # all segments concatenated
                        s = (np.array(s).reshape(-1, 2) / np.array([w, h])).reshape(-1).tolist()
                    s = [cls] + s
                    if s not in segments:
                        segments.append(s)
                if use_keypoints:
                    k = (np.array(ann['keypoints']).reshape(-1, 3) / np.array([w, h, 1])).reshape(-1).tolist()
                    k = box + k
                    keypoints.append(k)
ryouchinsa commented 9 months ago

Thanks for writing the issue. As you said, if ann['iscrowd']: continue should be commented out.

We updated our general_json2yolo.py script and we confirmed that the RLE mask with "iscrowd": 1is converted to YOLO segmentation format correctly.

スクリーンショット 2023-11-22 1 54 51

In this update, the RLE mask with holes can be converted to the YOLO segmentation format correctly. We hope this script would help your business.

The RLE mask.

スクリーンショット 2023-11-22 1 57 52

The converted YOLO segmentation format.

スクリーンショット 2023-11-22 2 11 14

On RectLabel, you have 2 options to label the polygon with holes.

Label the polygon with holes using Segment Anything models.

hole_sam

Label the polygon and erase inside the polygon using the Erase polygon tool.

hole_erase

ryouchinsa commented 8 months ago

Currently If you need our support to fix this problem, please let us know.

ryouchinsa commented 4 months ago

A user told us that converting RLE masks with "iscrowd": 1 to YOLO format might decrease the segmentation accuracy, https://github.com/ultralytics/JSON2YOLO/issues/38#issuecomment-2013148445

We added skip_iscrowd_1 flag to the convert_coco_json() function in the general_json2yolo.py script. Please give us your feedback.

Set skip_iscrowd_1=True.

スクリーンショット 2024-03-30 1 06 06

Set skip_iscrowd_1=False.

スクリーンショット 2024-03-30 1 06 44