This is an example of a COCO RLE mask - https://pastebin.com/ZhE2en4C
It's an output from a YOLOv8 validation run, taken from the generated predictions.json file.
I'm trying to decode this string in JavaScript and render it on a canvas. The encoded string is valid, because in python I can do this:
from pycocotools import mask as coco_mask
from PIL import Image
example_prediction = {
"image_id": "102_jpg",
"category_id": 0,
"bbox": [153.106, 281.433, 302.518, 130.737],
"score": 0.8483,
"segmentation": {
"size": [640, 640],
"counts": "<RLE string here>"
}
}
def rle_to_bitmap(rle):
bitmap = coco_mask.decode(rle)
return bitmap
def show_bitmap(bitmap):
img = Image.fromarray(bitmap.astype(np.uint8) * 255, mode='L')
img.show()
input("Press Enter to continue...")
img.close()
mask_bitmap = rle_to_bitmap(example_prediction["segmentation"])
show_bitmap(mask_bitmap)
And I can see the decoded mask.
Is there a library I can use to decode the same string in JavaScript and convert it to an Image? I tried digging into the source code of pycocotools, but I couldn't do it.
You can draw the mask on a canvas and then export the image if you need.
For the actual drawing you can use two approaches:
Here's the example of both: