coco_assistant/utils/det2seg.py
def det2seg(cann, output_dir):
    """
    Function for converting segmentation polygons in MS-COCO
    object detection dataset to segmentation masks. The seg-
    mentation masks are stored with a colour palette that's
    randomly assigned based on class. Change the seed if you
    want to change colours.

    :param cann: COCO annotation object
    :param output_dir: Directory to store segmentation masks.
    """

    if os.path.isdir(output_dir) is False:
        os.makedirs(output_dir, exist_ok=True)

    imids = cann.getImgIds()
    cats = cann.loadCats(cann.getCatIds())

    cat_colours = {0: (0, 0, 0)}

    # Set seed for palette colour
    np.random.seed(121)

    # Create category colourmap
    for c in cats:
        cat_colours[c['id']] = (np.random.randint(0,256), np.random.randint(0,256), np.random.randint(0,256))

    colour_map = np.array(list(cat_colours.values()))
    if colour_map.shape != (len(cats) + 1, 3):
        raise AssertionError("Incorrect shape of color map array")

    for imid in tqdm(imids):
        img = cann.loadImgs(imid)
        if len(img) > 1:
            raise AssertionError("Multiple images with same id")
        h, w = img[0]['height'], img[0]['width']
        name = img[0]['file_name']
        if name[-4:] != ".png":
            name = name[:-4] + ".png"
        im = np.zeros((h, w), dtype=np.uint8)
        annids = cann.getAnnIds(imgIds=[imid])
        if not annids:
            # No annotations
            res = Image.fromarray(im)
            res.save(os.path.join(output_dir, '{}'.format(name)))
        else:
            anns = cann.loadAnns(annids)
            for ann in anns:
                poly = ann['segmentation'][0]
                cat = ann['category_id']
                img = Image.new('L', (w, h))
                if len(poly) >= 6:
                    ImageDraw.Draw(img).polygon(poly, fill=cat)
                else:
                    continue
                mask = np.array(img)
                im = np.maximum(im, mask)
            res = Image.fromarray(im)
            res.putpalette(colour_map.astype(np.uint8))
            res.save(os.path.join(output_dir, '{}'.format(name)))

    Function for converting segmentation polygons in MS-COCO
    object detection dataset to segmentation masks. The seg-
    mentation masks are stored with a colour palette that's
    randomly assigned based on class. Change the seed if you
    want to change colours.

    :param cann: COCO annotation object
    :param output_dir: Directory to store segmentation masks.
    
Function for converting segmentation polygons in MS-COCO
object detection dataset to segmentation masks. The seg
mentation masks are stored with a colour palette that's
randomly assigned based on class. Change the seed if you
want to change colours.
Function for converting segmentation polygons in MS - COCO object detection dataset to segmentation masks . The seg mentation masks are stored with a colour palette that ' s randomly assigned based on class . Change the seed if you want to change colours .
def det2seg(cann, output_dir):
    if os.path.isdir(output_dir) is False:
        os.makedirs(output_dir, exist_ok=True)
    imids = cann.getImgIds()
    cats = cann.loadCats(cann.getCatIds())
    cat_colours = {0: (0, 0, 0)}
    np.random.seed(121)
    for c in cats:
        cat_colours[c['id']] = (np.random.randint(0,256), np.random.randint(0,256), np.random.randint(0,256))
    colour_map = np.array(list(cat_colours.values()))
    if colour_map.shape != (len(cats) + 1, 3):
        raise AssertionError("Incorrect shape of color map array")
    for imid in tqdm(imids):
        img = cann.loadImgs(imid)
        if len(img) > 1:
            raise AssertionError("Multiple images with same id")
        h, w = img[0]['height'], img[0]['width']
        name = img[0]['file_name']
        if name[-4:] != ".png":
            name = name[:-4] + ".png"
        im = np.zeros((h, w), dtype=np.uint8)
        annids = cann.getAnnIds(imgIds=[imid])
        if not annids:
            res = Image.fromarray(im)
            res.save(os.path.join(output_dir, '{}'.format(name)))
        else:
            anns = cann.loadAnns(annids)
            for ann in anns:
                poly = ann['segmentation'][0]
                cat = ann['category_id']
                img = Image.new('L', (w, h))
                if len(poly) >= 6:
                    ImageDraw.Draw(img).polygon(poly, fill=cat)
                else:
                    continue
                mask = np.array(img)
                im = np.maximum(im, mask)
            res = Image.fromarray(im)
            res.putpalette(colour_map.astype(np.uint8))
            res.save(os.path.join(output_dir, '{}'.format(name)))
def det2seg ( cann , output_dir ) : if os . path . isdir ( output_dir ) is False : os . makedirs ( output_dir , exist_ok = True ) imids = cann . getImgIds ( ) cats = cann . loadCats ( cann . getCatIds ( ) ) cat_colours = { 0 : ( 0 , 0 , 0 ) } np . random . seed ( 121 ) for c in cats : cat_colours [ c [ 'id' ] ] = ( np . random . randint ( 0 , 256 ) , np . random . randint ( 0 , 256 ) , np . random . randint ( 0 , 256 ) ) colour_map = np . array ( list ( cat_colours . values ( ) ) ) if colour_map . shape != ( len ( cats ) + 1 , 3 ) : raise AssertionError ( "Incorrect shape of color map array" ) for imid in tqdm ( imids ) : img = cann . loadImgs ( imid ) if len ( img ) > 1 : raise AssertionError ( "Multiple images with same id" ) h , w = img [ 0 ] [ 'height' ] , img [ 0 ] [ 'width' ] name = img [ 0 ] [ 'file_name' ] if name [ - 4 : ] != ".png" : name = name [ : - 4 ] + ".png" im = np . zeros ( ( h , w ) , dtype = np . uint8 ) annids = cann . getAnnIds ( imgIds = [ imid ] ) if not annids : res = Image . fromarray ( im ) res . save ( os . path . join ( output_dir , '{}' . format ( name ) ) ) else : anns = cann . loadAnns ( annids ) for ann in anns : poly = ann [ 'segmentation' ] [ 0 ] cat = ann [ 'category_id' ] img = Image . new ( 'L' , ( w , h ) ) if len ( poly ) >= 6 : ImageDraw . Draw ( img ) . polygon ( poly , fill = cat ) else : continue mask = np . array ( img ) im = np . maximum ( im , mask ) res = Image . fromarray ( im ) res . putpalette ( colour_map . astype ( np . uint8 ) ) res . save ( os . path . join ( output_dir , '{}' . format ( name ) ) )
Function for converting segmentation polygons in MS-COCO
object detection dataset to segmentation masks.
Function for converting segmentation polygons in MS - COCO object detection dataset to segmentation masks .
"""
    Function for converting segmentation polygons in MS-COCO
    object detection dataset to segmentation masks. The seg-
    mentation masks are stored with a colour palette that's
    randomly assigned based on class. Change the seed if you
    want to change colours.

    :param cann: COCO annotation object
    :param output_dir: Directory to store segmentation masks.
    """ # Set seed for palette colour # Create category colourmap # No annotations
cann
COCO annotation object
COCO annotation object
output_dir
Directory to store segmentation masks.
Directory to store segmentation masks .