#!/usr/bin/env python3 """ Simple usage example for ISNet Background Remover Shows how to use the model with one-line loading """ from PIL import Image from skimage import io import torch import torch.nn.functional as F from transformers import AutoModelForImageSegmentation from torchvision.transforms.functional import normalize import numpy as np def preprocess_image(im: np.ndarray, model_input_size: list) -> torch.Tensor: """Preprocess image for model input""" if len(im.shape) < 3: im = im[:, :, np.newaxis] im_tensor = torch.tensor(im, dtype=torch.float32).permute(2,0,1) im_tensor = F.interpolate(torch.unsqueeze(im_tensor,0), size=model_input_size, mode='bilinear') image = torch.divide(im_tensor,255.0) image = normalize(image,[0.5,0.5,0.5],[1.0,1.0,1.0]) return image def postprocess_image(result: torch.Tensor, im_size: list)-> np.ndarray: """Postprocess model output to get mask""" result = torch.squeeze(F.interpolate(result, size=im_size, mode='bilinear') ,0) ma = torch.max(result) mi = torch.min(result) result = (result-mi)/(ma-mi) im_array = (result*255).permute(1,2,0).cpu().data.numpy().astype(np.uint8) im_array = np.squeeze(im_array) return im_array def main(): # One-line model loading with trust_remote_code=True model = AutoModelForImageSegmentation.from_pretrained("mateenahmed/isnet-background-remover", trust_remote_code=True) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") model.to(device) # Example image URL image_path = "https://farm5.staticflickr.com/4007/4322154488_997e69e4cf_z.jpg" orig_im = io.imread(image_path) orig_im_size = orig_im.shape[0:2] model_input_size = [1024, 1024] # Preprocess image image = preprocess_image(orig_im, model_input_size).to(device) # Inference result = model(image) # Post process result_image = postprocess_image(result, orig_im_size) # Save result pil_mask_im = Image.fromarray(result_image) orig_image = Image.open(image_path) no_bg_image = orig_image.copy() no_bg_image.putalpha(pil_mask_im) no_bg_image.save("output_no_bg.png") print("✅ Background removed! Check output_no_bg.png") if __name__ == "__main__": main()