facebookresearch / sam2

The repository provides code for running inference with the Meta Segment Anything Model 2 (SAM 2), links for downloading the trained model checkpoints, and example notebooks that show how to use the model.
Apache License 2.0
11.76k stars 1.03k forks source link

How to get annotations into json file #250

Open sarkadava opened 1 month ago

sarkadava commented 1 month ago

Hey, sorry if I am missing something in the documentation, but I would like to know how can I convert the annotation results for images into json file with coordinates (something like coco file) so that I can work with the annotations further? Thanks a lot!

Dashenboy commented 1 month ago

So you can use these masks that it outputs (Numpy array) and turn them into an image using PIL. Here is a quick example i made def save_mask_with_original_name(maskimage, original_filename, save_directory):

Ensure the maskimage has the correct shape and type

if maskimage.dtype != np.uint8:
    maskimage = (maskimage * 255).astype(np.uint8)  # Convert to 8-bit grayscale if necessary
# Convert maskimage to RGB
masksss = Image.fromarray(maskimage).convert('RGB')
masksss = masksss.resize(mask_size, Image.Resampling.LANCZOS)

# Extract the base filename without extension
base_filename = os.path.splitext(original_filename)[0]

# Save the mask image with the same base filename
save_path = os.path.join(save_directory, f"{base_filename}_mask.jpg")
masksss.save(save_path, format='JPEG')
sarkadava commented 1 month ago

@Dashenboy thanks for your reply. Sorry if I was not clear, I don't need an image of the mask. I need the mask to save in COCO formant, ie, with coordinates such that I am able to open it with Image Annotator, or further use in training an object/subject recognition model. I see that the output of SAM has vectors of True,False; and am not sure how to get rather the coordinates