Views
No views yet

1from diffusers import DiffusionPipeline
2model_id = "yahoo-inc/photo-background-generation"
3pipeline = DiffusionPipeline.from_pretrained(model_id, custom_pipeline=model_id)
4pipeline = pipeline.to('cuda')1from PIL import Image, ImageOps
2import requests
3from io import BytesIO
4from transparent_background import Remover
5
6def resize_with_padding(img, expected_size):
7 img.thumbnail((expected_size[0], expected_size[1]))
8 # print(img.size)
9 delta_width = expected_size[0] - img.size[0]
10 delta_height = expected_size[1] - img.size[1]
11 pad_width = delta_width // 2
12 pad_height = delta_height // 2
13 padding = (pad_width, pad_height, delta_width - pad_width, delta_height - pad_height)
14 return ImageOps.expand(img, padding)
15
16seed = 0
17image_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/16/Granja_comary_Cisne_-_Escalavrado_e_Dedo_De_Deus_ao_fundo_-Teres%C3%B3polis.jpg/2560px-Granja_comary_Cisne_-_Escalavrado_e_Dedo_De_Deus_ao_fundo_-Teres%C3%B3polis.jpg'
18response = requests.get(image_url)
19img = Image.open(BytesIO(response.content))
20img = resize_with_padding(img, (512, 512))
21
22# Load background detection model
23remover = Remover() # default setting
24remover = Remover(mode='base') # nightly release checkpoint
25
26# Get foreground mask
27fg_mask = remover.process(img, type='map') # default setting - transparent background1seed = 13
2mask = ImageOps.invert(fg_mask)
3img = resize_with_padding(img, (512, 512))
4generator = torch.Generator(device='cuda').manual_seed(seed)
5prompt = 'A dark swan in a bedroom'
6cond_scale = 1.0
7with torch.autocast("cuda"):
8 controlnet_image = pipeline(
9 prompt=prompt, image=img, mask_image=mask, control_image=mask, num_images_per_prompt=1, generator=generator, num_inference_steps=20, guess_mode=False, controlnet_conditioning_scale=cond_scale
10 ).images[0]
11controlnet_image1@misc{eshratifar2024salient,
2 title={Salient Object-Aware Background Generation using Text-Guided Diffusion Models},
3 author={Amir Erfan Eshratifar and Joao V. B. Soares and Kapil Thadani and Shaunak Mishra and Mikhail Kuznetsov and Yueh-Ning Ku and Paloma de Juan},
4 year={2024},
5 eprint={2404.10157},
6 archivePrefix={arXiv},
7 primaryClass={cs.CV}
8}