I want to create a Coco Style json file in Python.

Asked 2 years ago, Updated 2 years ago, 70 views

I would like to create a Coco Style json file in Python.

I'm a beginner in programming.
This is my first time asking a question.

http://www.immersivelimit.com/tutorials/create-coco-annotations-from-scratch/ #coco-dataset-format

I am trying to create an annotation file by referring to the above site.
The following is just an image of the code on the above site

 from PIL import Image# (pipe install Pillow)
import numpy as np
from image import measure
from shapely.geometry import Polygon, MultiPolygon

def create_sub_masks(mask_image):
    width, height=mask_image.size

    # Initialize dictionary of sub-masks indexed by RGB colors
    sub_masks={}
    for x in range (width):
        For y in range (height):
            # Get the RGB values of the pixel
            pixel=mask_image.getpixel((x,y))[:3]

            ifpixel!=(0,0,0):
                pixel_str=str(pixel)
                sub_mask=sub_masks.get(pixel_str)
                if sub_mask is None:
                    sub_masks [pixel_str] = Image.new('1', (width+2, height+2))
                sub_masks [pixel_str].putpixel((x+1,y+1),1)

    return sub_masks

create_sub_mask_announcement(sub_mask, image_id, category_id, announcement_id, is_crowd):
contents=measure.find_contours(sub_mask, 0.5, positive_orientation='low')

    segments = [ ]
    polygons = [ ]
    for tour in contents:
        for i in range (len(contour)) :
            row,col=contour[i]
        tour[i] = (col-1, row-1)

        poly=Polygon(contour)
        poly=poly.simplify (1.0, preserve_topology=False)
        polygons.append(poly)
        segmentation=np.array(poly.exterior.coords).ravel().tolist()
        segments.append(segmentation)

    multi_poly=MultiPolygon(polygons)
    x,y,max_x,max_y = multi_poly.bounds
    width = max_x -x
    height = max_y -y
    bbox=(x,y,width,height)
    area=multi_poly.area

    annotation = {
        'segmentation':segmentations,
        'iscrowd': is_crowd,
        'image_id': image_id,
        'category_id':category_id,
        'id' —Annotation_id,
        'bbox': bbox,
        'area': area
    }

    return announcement

class1_image=PIL.Image.open('./mask/class1/mask_10.jpg')
class2_image=PIL.Image.open('./mask/class2/mask_398.jpg')
class3_image=PIL.Image.open('./mask/class3/mask_0.jpg')


mask_images=[class1_image, class2_image, class3_image]

category_ids = {1:{'(255,0,0)':class1_id,},
                2:{'(0,255,0)':class2_id,},
                3: {'(0,0,255)':class3_id,}}

is_crowd = 0

announcement_id = 1
image_id = 1

announcements = [ ]
for mask_image in mask_images:
    sub_masks=create_sub_masks(mask_image)
    # print(sub_masks.keys())
    for color, sub_mask in sub_masks.items():
        # print(image_id, color)
        category_id = category_ids [ image_id ] [ color ]
        announcement=create_sub_mask_announcement(sub_mask, image_id, category_id, announcement_id, is_crowd)
        announcements.append(announcement)
        announcement_id+=1
    image_id+=1

Now that I've done this,

KeyError Traceback (most recent call last)
in
28 for color, sub_mask in sub_masks.items():
29# print(image_id,color)
--- >30 category_id = category_ids [image_id] [color]
31 notation=create_sub_mask_notation(sub_mask, image_id, category_id, announcement_id, is_crowd)
32 notations.append (announcement)

KeyError: '(0,1,5)'

I received an error like this.
The image is as follows.

mask_10.jpg
Enter a description of the image here
mask_398.jpg
Enter a description of the image here
mask_0.jpg
Enter a description of the image here

I'm not sure how create_sub_masks works, especially I don't understand the meaning of this place.

 if sub_mask is None:
    sub_masks [pixel_str] = Image.new('1', (width+2, height+2))
    sub_masks [pixel_str].putpixel((x+1,y+1),1)

I don't have enough study, so I'd like to ask for some wisdom.

python json annotations

2022-09-30 20:24

1 Answers

Self-resolved.

I chose png instead of jpg, and it worked.


2022-09-30 20:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.