In the example below, I get the generated data. I want to pass this masks to the web client to draw the mask image in the canvas, but I don't know which data inside the masks can be used to draw the image.
Can you give me an example?
from segment_anything import SamAutomaticMaskGenerator, sam_model_registry
import numpy as np
from PIL import Image
sam = sam_model_registry["vit_b"] (checkpoint="/Users/jiangxingshang/Downloads/sam_vit_b_01ec64.pth")
sam.to(device='cuda')
mask_generator = SamAutomaticMaskGenerator(sam)
img = Image.open("/Users/jiangxingshang/Pictures/8435e5dde71190ef699eced5cc1b9d16fdfa6056.jpg")
imgnp = np.array(img)
masks = mask_generator.generate(imgnp)
You can follow this example.
Briefly,
masksis a list, in which each mask is a dictionary. For example,will assign the first mask to
first_maskvariable, assuming the listmasksis not empty.The dictionary keys are the following: