Skip to content

Commit 5986b49

Browse files
committed
Background generation updates
1 parent 350f7d9 commit 5986b49

4 files changed

Lines changed: 24 additions & 11 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ _This script is useful for **testing and fine-tuning** object placements before
104104

105105
All the images rendered are just 2 objects on a white surface with a light grey "sky". If you would like to add a custom (more realistic) background to each image in your image output folder, you can navigate to the `generate_backgrounds.sh` script.
106106

107-
- Here, you can customize the `PROMPT` argument to guide the background generation process.
107+
- Here, you can customize the `PROMPT` (what you want the scene to include) and `NEGATIVE_PROMPT` (what you want to avoid in the scene) arguments to guide the background generation process.
108+
109+
- Make sure to provide a valid Hugging Face token in the `HF_TOKEN` field to authenticate and access the model.
108110

109111
- The process utilizes the [**Stable Diffusion XL inpainting model**](https://huggingface.co/diffusers/stable-diffusion-xl-1.0-inpainting-0.1) to modify specific regions of images with custom backgrounds.
110112

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ Adding AI-generated Backgrounds (Experimental)
159159

160160
To enhance image realism, use ``generate_backgrounds.sh`` to add AI-generated backgrounds via the Stable Diffusion XL inpainting model:
161161

162-
Customize the background generation with the ``PROMPT`` variable.
162+
Customize the background generation with the ``PROMPT`` (what you want the scene to include) and ``NEGATIVE_PROMPT`` (what you want to avoid in the scene) variables.
163163

164164
.. tip::
165165

scripts/generate_backgrounds.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@ cd "../src"
55
# Path to Python executable
66
PYTHON_EXECUTABLE="python"
77

8-
# Prompt for the model (required)
8+
# Prompt for the generation
99
PROMPT=""
10+
NEGATIVE_PROMPT=""
11+
12+
# Hugging Face Token
13+
HF_TOKEN=""
1014

1115
# Install dependencies
12-
if ! python -c "import torch, torchvision, torchaudio" &> /dev/null; then
13-
pip install torch --index-url https://download.pytorch.org/whl/cu121/torch_stable.html
16+
if ! python -c "import torch" &> /dev/null; then
17+
pip install torch --index-url https://download.pytorch.org/whl/cu121/
1418
fi
1519

1620
if ! pip freeze | grep -q -f requirements.txt; then
1721
pip install -r requirements.txt
1822
fi
1923

20-
$PYTHON_EXECUTABLE generate_background.py "$PROMPT"
24+
# Hugging Face authentication
25+
huggingface-cli login --token "$HF_TOKEN"
26+
27+
$PYTHON_EXECUTABLE generate_background.py "$PROMPT" "$NEGATIVE_PROMPT"

src/generate_background.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Generate AI-enhanced images for each image in the output directory."""
22
import torch
33
from diffusers import AutoPipelineForInpainting
4-
from PIL import Image
4+
from PIL import Image, ImageFilter
55
import os
66
import json
77
import sys
@@ -30,6 +30,7 @@ def generate_background(image_file: str,
3030
mask_file: str,
3131
output_path: str,
3232
prompt: str,
33+
negative_prompt: str,
3334
device: str,
3435
pipe: AutoPipelineForInpainting) -> None:
3536
""" Generate and save an enhanced image using a diffusion model for a given image and mask.
@@ -45,12 +46,14 @@ def generate_background(image_file: str,
4546
# Open both image and mask
4647
image = Image.open(image_file).convert("RGB")
4748
mask = Image.open(mask_file).convert("L")
49+
mask = mask.filter(ImageFilter.GaussianBlur(radius=2))
4850
mask = mask.point(lambda x: 0 if x < 254 else 255)
4951
# Use a generator
5052
generator = torch.Generator(device=device).manual_seed(42)
5153
# Perform inpainting
5254
output_image = pipe(
5355
prompt=prompt,
56+
negative_prompt=negative_prompt,
5457
image=image,
5558
mask_image=mask,
5659
guidance_scale=5,
@@ -61,7 +64,7 @@ def generate_background(image_file: str,
6164
filename = os.path.basename(image_file)
6265
output_image.save(os.path.join(output_path, filename))
6366

64-
def main(prompt: str, device: str) -> None:
67+
def main(prompt: str, negative_prompt: str, device: str) -> None:
6568
"""Generate enhanced images for each image in the output directory.
6669
6770
Args:
@@ -86,7 +89,7 @@ def main(prompt: str, device: str) -> None:
8689
enhanced_path_dir = os.path.join(enhanced_image_dir, relative_path)
8790
if not os.path.exists(enhanced_path_dir):
8891
os.makedirs(enhanced_path_dir)
89-
generate_background(image_file, mask_file, enhanced_path_dir, prompt, device, pipe)
92+
generate_background(image_file, mask_file, enhanced_path_dir, prompt, negative_prompt, device, pipe)
9093

9194
if __name__ == '__main__':
9295
# Check if CUDA is available
@@ -96,6 +99,7 @@ def main(prompt: str, device: str) -> None:
9699
print("CUDA is not available. Using CPU.")
97100
device = "cpu"
98101
# Extract prompt
99-
prompt = sys.argv[-1]
100-
main(prompt, device)
102+
prompt = sys.argv[-2]
103+
negative_prrompt = sys.argv[-1]
104+
main(prompt, negative_prrompt, device)
101105

0 commit comments

Comments
 (0)