Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
-
from diffusers import
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 7 |
import spaces
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
-
# Load
|
| 12 |
-
pipe =
|
| 13 |
-
"
|
| 14 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 15 |
-
|
| 16 |
).to(device)
|
| 17 |
|
| 18 |
-
# Load DETR
|
| 19 |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 20 |
detector = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50").to(device)
|
| 21 |
|
| 22 |
@spaces.GPU
|
| 23 |
-
def
|
| 24 |
if input_image is None or prompt == "":
|
| 25 |
return None
|
| 26 |
|
|
@@ -35,56 +35,51 @@ def detect_and_replace(input_image, prompt, negative_prompt=""):
|
|
| 35 |
mask = Image.new("L", input_image.size, 0)
|
| 36 |
draw = ImageDraw.Draw(mask)
|
| 37 |
|
| 38 |
-
|
| 39 |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 40 |
if detector.config.id2label[label.item()] == "person":
|
| 41 |
box = [int(i) for i in box.tolist()]
|
| 42 |
-
boxes.append(box)
|
| 43 |
draw.rectangle(box, fill=255)
|
|
|
|
| 44 |
|
| 45 |
-
if not
|
| 46 |
return "No human detected."
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
# Paste the resized image at the correct location
|
| 72 |
-
output_image.paste(resized_generated, (x1, y1), mask=None) # You can add mask for soft edges later
|
| 73 |
-
|
| 74 |
-
return output_image
|
| 75 |
|
| 76 |
# Gradio UI
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
-
gr.Markdown("## Replace Bride and Groom
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
input_image = gr.Image(type="pil", label="Input Image")
|
| 82 |
output_image = gr.Image(type="pil", label="Output Image")
|
| 83 |
|
| 84 |
-
prompt_text = gr.Textbox(label="Prompt", placeholder="
|
| 85 |
-
negative_prompt_text = gr.Textbox(label="Negative Prompt", placeholder="Optional negative prompt")
|
| 86 |
submit = gr.Button("Submit")
|
| 87 |
|
| 88 |
-
submit.click(
|
| 89 |
|
| 90 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import numpy as np
|
| 4 |
+
from diffusers import StableDiffusionXLInpaintPipeline
|
| 5 |
from PIL import Image, ImageDraw
|
| 6 |
from transformers import DetrImageProcessor, DetrForObjectDetection
|
| 7 |
import spaces
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
+
# Load the Stable Diffusion XL Inpainting model
|
| 12 |
+
pipe = StableDiffusionXLInpaintPipeline.from_pretrained(
|
| 13 |
+
"stabilityai/stable-diffusion-xl-base-1.0",
|
| 14 |
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
| 15 |
+
variant="fp16",
|
| 16 |
).to(device)
|
| 17 |
|
| 18 |
+
# Load the DETR object detection model
|
| 19 |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
| 20 |
detector = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50").to(device)
|
| 21 |
|
| 22 |
@spaces.GPU
|
| 23 |
+
def detect_and_replace_humans(input_image, prompt):
|
| 24 |
if input_image is None or prompt == "":
|
| 25 |
return None
|
| 26 |
|
|
|
|
| 35 |
mask = Image.new("L", input_image.size, 0)
|
| 36 |
draw = ImageDraw.Draw(mask)
|
| 37 |
|
| 38 |
+
found = False
|
| 39 |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
| 40 |
if detector.config.id2label[label.item()] == "person":
|
| 41 |
box = [int(i) for i in box.tolist()]
|
|
|
|
| 42 |
draw.rectangle(box, fill=255)
|
| 43 |
+
found = True
|
| 44 |
|
| 45 |
+
if not found:
|
| 46 |
return "No human detected."
|
| 47 |
|
| 48 |
+
# Pre-defined positive and negative prompts
|
| 49 |
+
positive_prompt = (
|
| 50 |
+
"Replace the masked humans with imaginary Indian bride and groom wearing traditional Indian wedding attire, "
|
| 51 |
+
"with detailed embroidery, colorful saree and sherwani, realistic faces, natural skin texture, matching pose, "
|
| 52 |
+
"perfect lighting, and the same camera perspective. Keep the background unchanged."
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
negative_prompt = (
|
| 56 |
+
"blurry, distorted, deformed, double face, extra limbs, low quality, bad proportions, low resolution, "
|
| 57 |
+
"changed background, multiple faces, duplicate body parts, cartoon, watermark, text"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Inpainting process
|
| 61 |
+
output = pipe(
|
| 62 |
+
prompt=positive_prompt,
|
| 63 |
+
negative_prompt=negative_prompt,
|
| 64 |
+
image=input_image,
|
| 65 |
+
mask_image=mask,
|
| 66 |
+
num_inference_steps=40,
|
| 67 |
+
guidance_scale=8.5
|
| 68 |
+
).images[0]
|
| 69 |
+
|
| 70 |
+
return output
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Gradio UI
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
+
gr.Markdown("## Replace Humans with Imaginary Indian Bride and Groom (Background Preserved)")
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
input_image = gr.Image(type="pil", label="Input Image")
|
| 78 |
output_image = gr.Image(type="pil", label="Output Image")
|
| 79 |
|
| 80 |
+
prompt_text = gr.Textbox(label="Prompt (Optional, Predefined Prompt Used)", placeholder="You can leave this blank")
|
|
|
|
| 81 |
submit = gr.Button("Submit")
|
| 82 |
|
| 83 |
+
submit.click(detect_and_replace_humans, inputs=[input_image, prompt_text], outputs=output_image)
|
| 84 |
|
| 85 |
demo.launch()
|