This is an example of COCO RLE mask - https://pastebin.com/ZhE2en4C
This is the output of the YOLOv8 validation run, taken from the generated Predictions.json file.
I'm trying to decode the string in JavaScript and render it on the 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)
I can see the decoded mask.
Is there a library that can be used 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.
You can draw the mask on the canvas and then export the image if needed.
For actual drawing, you can use two methods:
Here are examples of both: