Giter Site home page Giter Site logo

chrise96 / image-to-coco-json-converter Goto Github PK

View Code? Open in Web Editor NEW
192.0 192.0 57.0 899 KB

Convert segmentation RGB mask images to COCO JSON format

Jupyter Notebook 58.11% Python 41.89%
coco-dataset converter instance-segmentation ms-coco python voc

image-to-coco-json-converter's People

Contributors

chrise96 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

image-to-coco-json-converter's Issues

Keyerror '(1, 1, 1)'

Hi,
I have a mask with values "(2, 2, 2)": 0 which I added in the category_colors. Why is it giving me a key error of "(1, 1, 1)" ? The pixel values in my mask are either (0, 0, 0) for background or (2, 2, 2) for mask. Thanks

get alot of polygon points

Hello @chrise96,

thank you for providing this amazing repo. I have just successfully ran and visualized annotations as well. But when I load this json file into annotation tool then it has so many multiple points because of many polygon points. is it possible to discard unnecessary points and stores only required points if shape changes??

Your help will be appreciated. thank you inadvance.

'MultiPolygon' object has no attribute 'exterior'

Hi
Thank you for putting the effort on creating this great repo.

I am trying to convert binary images to coco annotations, but i get this error. can you please explain why is this happening and how can i solve it. I also googled it, but have not been lucky yet.
"""

AttributeError Traceback (most recent call last)
Input In [5], in
84 coco_format["categories"] = create_category_annotation(category_ids)
86 # Create images and annotations sections
---> 87 coco_format["images"], coco_format["annotations"], annotation_cnt = images_annotations_info(mask_path)
89 with open("output/{}.json".format(keyword),"w") as outfile:
90 json.dump(coco_format, outfile)

Input In [5], in images_annotations_info(maskpath)
50 category_id = category_colors[color]
52 # "annotations" info
---> 53 polygons, segmentations = create_sub_mask_annotation(sub_mask)
55 # Check if we have classes that are a multipolygon
56 if category_id in multipolygon_ids:
57 # Combine the polygons to calculate the bounding box and area

File ~/Desktop/Py_js/Data/python_with_vlad/pycharm/polymore/Binary-Image_to_coco/image-to-coco-json-converter-master/src/create_annotations.py:56, in create_sub_mask_annotation(sub_mask)
52 continue
54 polygons.append(poly)
---> 56 segmentation = np.array(poly.exterior.coords).ravel().tolist()
57 segmentations.append(segmentation)
59 return polygons, segmentations

AttributeError: 'MultiPolygon' object has no attribute 'exterior'

"""

Thanks in advance.

I don't know this error

KeyError Traceback (most recent call last)
in ()
84
85 # Create images and annotations sections
---> 86 coco_format["images"], coco_format["annotations"], annotation_cnt = images_annotations_info(mask_path)
87
88 with open("output/{}.json".format(keyword),"w") as outfile:

in images_annotations_info(maskpath)
47 sub_masks = create_sub_masks(mask_image_open, w, h)
48 for color, sub_mask in sub_masks.items():
---> 49 category_id = category_colors[color]
50
51 # "annotations" info

KeyError: '(130, 130, 130)'

Interior contours

I have been using your code for a while, and I noticed that object that have a inside ring, this inside ring gets ignored and only keeps the exterior contours.
Example
here is the submask generated for a category by your code
image

after all the code is executed, I tried to reconstruct the mask from the segmentation generated using the following code

from pycocotools import mask as maskUtils
def annToRLE( ann, height, width):
        """
        Convert annotation which can be polygons, uncompressed RLE to RLE.
        :return: binary mask (numpy 2D array)
        """
        segm = ann['segmentation']
        if isinstance(segm, list):
            # polygon -- a single object might consist of multiple parts
            # we merge all parts into one mask rle code
            rles = maskUtils.frPyObjects(segm, height, width)
            rle = maskUtils.merge(rles)
        elif isinstance(segm['counts'], list):
            # uncompressed RLE
            rle = maskUtils.frPyObjects(segm, height, width)
        else:
            # rle
            rle = ann['segmentation']
        return rle

def annToMask( ann, height, width):
    """
    Convert annotation which can be polygons, uncompressed RLE, or RLE to binary mask.
    :return: binary mask (numpy 2D array)
    """
    rle = annToRLE(ann, height, width)
    m = maskUtils.decode(rle)
    return m

and this is the reconstructed mask
image

as you can see the object now is only one piece without the inside contours

is there a way to keep the inside ring also ?
Do you think the problem is in the functions that reconstruct the mask or your code?

double annotations

I was trying your code, and it seems i am getting 13 segmentations, including one for the entire image.
my mask has only 6 annotations, all same category id.
I have commented the multipolygon, since I do not need it and changed some codes since I have only 1 catgeory.

Any ideas?

Error When running script AttributeError: 'Image' object has no attribute 'shape'

Hello,
I have clones the repo and done absolutly 0 modification.
When running your notebook i get AttributeError: 'Image' object has no attribute 'shape'.
More details :

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-2-77e827a19691> in <module>
     67     for keyword in ['train', 'val']:
     68         mask_path = 'dataset/{}_mask'.format(keyword)
---> 69         coco_format['images'], coco_format['annotations'] = images_annotations_info(mask_path)
     70         #print(json.dumps(coco_format))
     71         with open('output/{}.json'.format(keyword),'w') as outfile:

<ipython-input-2-77e827a19691> in images_annotations_info(maskpath)
     41 
     42             # 'annotations' info
---> 43             polygons, segmentations = create_sub_mask_annotation(sub_mask)
     44 
     45             # Three labels are multipolygons in our case: wall, roof and sky

D:\PERSO\PERSONAL WORK\15 - AI MachineLearning\image-to-coco-json-converter\create_annotations.py in create_sub_mask_annotation(sub_mask)
     35     # Note: there could be multiple contours if the object
     36     # is partially occluded. (E.g. an elephant behind a tree)
---> 37     contours = measure.find_contours(sub_mask, 0.5, positive_orientation='low')
     38 
     39     polygons = []

c:\users\richa\appdata\local\programs\python\python37\lib\site-packages\skimage\measure\_find_contours.py in find_contours(array, level, fully_connected, positive_orientation, mask)
    121         raise ValueError('Parameters "positive_orientation" must be either '
    122                          '"high" or "low".')
--> 123     if array.shape[0] < 2 or array.shape[1] < 2:
    124         raise ValueError("Input array must be at least 2x2.")
    125     if array.ndim != 2:

AttributeError: 'Image' object has no attribute 'shape'

Pip versions :
Pillow 8.0.1
numpy 1.18.5
scikit-image 0.17.2
Shapely 1.7.1

Mabey this is linked to a different version of a library ?

Thanks in advance

Grayscale image

I want to use your code but I have grayscale masks, where each category is coded with a value, could you guide me to what should be changed in order to achieve this.
Thanks

Background annotations

Hello,

I have a question about background annotations:

I have a Black and White mask: Black == Background ; White == The object I segmented

Unfortunately now, when I only add the white part to the category the script complains, that it has a key error at (0,0,0). But if I add the (0,0,0) I have the issue, that the background also is an annotation.

Do you have any idea how to resolve this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.