CASIA-IVA-Lab / FastSAM

Fast Segment Anything
GNU Affero General Public License v3.0
7.29k stars 681 forks source link

Masks without the background image #114

Open DenisSebastian opened 1 year ago

DenisSebastian commented 1 year ago

How to obtain as a result the masks without the background image?

BWDforce commented 1 year ago

Some lines in prompt.py/plot_to_result should be modified

giswqs commented 1 year ago

I was also looking for a way to get the masks without the background image, similar to the format that the original SAM provides. The FastSAM code is not very clear about this.

yejing0 commented 9 months ago

I also have this problem+

YukunZEX commented 6 months ago

import sys import os from matplotlib import pyplot as plt import numpy as np import cv2 from PIL import Image

file_path = os.path.abspath(file) work_path = os.path.dirname(os.path.dirname(file_path)) sys.path.append(work_path) from fastsam import FastSAM, FastSAMPrompt

model = FastSAM(work_path+'/weights/FastSAM_X.pt') IMAGE_PATH = work_path+'/examples/home.jpg' DEVICE = 'cuda' #'cpu' everything_results = model(IMAGE_PATH, device=DEVICE, retina_masks=False, conf=0.4, iou=0.9,) conf=0.4, iou=0.9,imgsz=1024, prompt_process = FastSAMPrompt(IMAGE_PATH, everything_results, device=DEVICE)

ann = prompt_process.everything_prompt()

mask=everything_results[0].masks.data mask=(mask.cpu()*np.array(list(range(1,mask.size()[0]+1)))[:,np.newaxis,np.newaxis]).sum(axis=0)

image = Image.fromarray(mask.numpy().astype('uint8')) image.save(work_path+'/output/home.png')

plt.figure(figsize=(10, 10)) # 255 is the fill color in grayscale (white) plt.imshow(mask, cmap='gray') # Display the image in grayscale plt.axis('off') # Turn off axis numbers and ticks plt.show()

TuuSiwei commented 3 months ago

How to obtain as a result the masks without the background image?

do you have any way to relize it?