chrise96 / image-to-coco-json-converter Goto Github PK
View Code? Open in Web Editor NEWConvert segmentation RGB mask images to COCO JSON format
Convert segmentation RGB mask images to COCO JSON format
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
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.
Hi
Thank you for putting the effort on creating this great repo.
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.
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)'
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
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
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?
Get this error when trying to convert these types of masks to json . I assume its because of the weird polygons:
Similar issue reported here akTwelve/cocosynth#9 and maybe fixed here https://github.com/akTwelve/cocosynth/blob/3909837290ab3511ff03ffe57ae870c929bd40a0/python/coco_json_utils.py
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?
I try to run the script, if the annotations more than 300, it will error with 'AttributeError: 'MultiPolygon' object has no attribute 'exterior''. How I can solve this problem?
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
i have a dataset from a lot of complex shape.
but this model was not able to clip overlay area.
how to clip overlay area?
this is my dataset, thanks.
https://drive.google.com/drive/folders/1butmjjGTgMIEr6bq1nQ3ejjm0M7oN_YR?usp=share_link
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
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?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.