cocodataset / cocoapi

COCO API - Dataset @ http://cocodataset.org/
Other
6.05k stars 3.75k forks source link

Convert [x,y] coordinates of a polygon to a coco segmentation list. #319

Open nirandiw opened 5 years ago

nirandiw commented 5 years ago

Suppose I have an image which I have annotated using an external tool. I was able to download it as a xml file with the annotation information.

<annotation>
    <size>
        <width>720</width>
        <height>576</height>
        <depth>3</depth>
    </size>
    <object>
        <name>nanny</name>
        <bndbox>
            <xmin>7</xmin>
            <ymin>12</ymin
            ><xmax>711</xmax>
            <ymax>558</ymax>
        </bndbox>
        <segment_polygons>
            <polygon>
                <point>
                    <value>213</value>
                    <value>9</value>
                </point>
                <point>
                    <value>113</value>
                    <value>87</value>
                </point>
                <point>
                    <value>113</value>
                    <value>107</value>
                </point>
                <point>
                    <value>62</value>
                    <value>146</value>
                </point>
                <point>
                    <value>37</value>
                    <value>146</value>
                </point>
                <point>
                    <value>12</value>
                    <value>165</value>
                </point>
                <point>
                    <value>12</value>
                    <value>477</value>
                </point>
                <point>
                    <value>138</value>
                    <value>575</value>
                </point>
                <point>
                    <value>691</value>
                    <value>575</value>
                </point>
                <point>
                    <value>741</value>
                    <value>536</value>
                </point>
                <point>
                    <value>741</value>
                    <value>87</value>
                </point>
                <point>
                    <value>640</value>
                    <value>9</value>
                </point>
            </polygon>
        </segment_polygons>
    </object>
</annotation>

As can be seen, the polygon is represented with a list of [x_i, y_i] coordinates. I want to convert this annotation to the coco annotations. I understand the x,y coordinates representing the polygon (i.e., <point><value>741</value><value>87</value></point>) has to be mapped to a list to put under "segmentations". How can I generate that segmentation?

Is what's stored in "segmentations" the binary mask of the polygon? If so can I use the output of this Matlab function for the above purpose? https://de.mathworks.com/help/images/ref/poly2mask.html

If not what should I do to get the coco segmentation values from a polygon given as a set of x,y coordinates.

@dexter1608

cm107 commented 4 years ago

If you're looking to convert xml annotation files to a coco json, I think that my annotation_utils might be a good reference for you. In this repo, there are tools for converting labelImg xml annotations to labelme json annotations, and labelme json annotations to a coco json. It looks like the fields in your xml file are slightly different, so would have to modify field names slightly when parsing xml data. Refer to ElementTree for a guide on how to parse data from xml files.

amirali0112017 commented 1 year ago

I have txt files with polygon coordinates of objects in each frame, how can I convert them into the COCO format?