File size: 2,043 Bytes
0fb3e0c
f8b086e
 
 
389e07e
 
d3b192f
 
 
d29e8d5
 
 
 
 
 
 
 
 
0fb3e0c
8971aa3
80a8459
 
d547635
 
ae57e16
389e07e
d547635
c24af23
d547635
 
 
 
 
c24af23
 
 
 
d3b192f
f8b086e
 
0fb3e0c
ae57e16
0fb3e0c
e956b51
f8b086e
b1ee382
f8b086e
 
fc3ec83
ae57e16
fc3ec83
0fb3e0c
 
d547635
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import torch
from diffusers import StableDiffusionInpaintPipeline
import gradio as gr
from PIL import Image
import numpy as np

# Ensure we use GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"

def load_model():
    """Load Stable Diffusion 2 Inpainting model."""
    pipe = StableDiffusionInpaintPipeline.from_pretrained(
        "stabilityai/stable-diffusion-2-inpainting",
        torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
    ).to(device)
    return pipe

pipe = load_model()

def inpaint(image_editor_output, prompt):
    """Perform inpainting using the uploaded image and mask."""
    print(f"image_editor_output:{image_editor_output}")
    
    if not isinstance(image_editor_output, dict) or "background" not in image_editor_output or "layers" not in image_editor_output:
        raise ValueError("Invalid input. Please upload an image and paint a mask.")
    
    # Extract the base image (background or composite)
    image = Image.fromarray(image_editor_output["composite"]).convert("RGB")  # Ensure image is in RGB format
    
    # Extract the first layer (mask) and convert to grayscale
    mask_array = np.array(image_editor_output["layers"][0], dtype=np.uint8)
    mask = Image.fromarray(mask_array[:, :, 3]).convert("L")  # Use the alpha channel
    
    # Resize mask to match image size if needed
    if mask.size != image.size:
        mask = mask.resize(image.size, Image.LANCZOS)
    
    # Perform inpainting
    result = pipe(prompt=prompt, image=image, mask_image=mask).images[0]
    return result

# Gradio UI with ImageEditor for inpainting
demo = gr.Interface(
    fn=inpaint,
    inputs=[
        gr.ImageEditor(label="Upload & Paint Image (White areas will be inpainted)"),
        gr.Textbox(label="Prompt for Inpainting"),
    ],
    outputs=gr.Image(type="pil", label="Generated Image"),
    title="Stable Diffusion 2 Inpainting",
    description="Upload an image, paint directly on it to define mask areas, and provide a text prompt.",
)

demo.launch()