Update app.py
Browse files
app.py
CHANGED
@@ -1,25 +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 |
-
"SG161222/RealVisXL_V4.0", # ✅
|
14 |
-
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
|
|
15 |
).to(device)
|
16 |
|
17 |
-
# Load
|
18 |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
|
19 |
detector = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50").to(device)
|
20 |
|
21 |
@spaces.GPU
|
22 |
-
def detect_and_replace(input_image, prompt):
|
23 |
if input_image is None or prompt == "":
|
24 |
return None
|
25 |
|
@@ -34,36 +35,53 @@ def detect_and_replace(input_image, prompt):
|
|
34 |
mask = Image.new("L", input_image.size, 0)
|
35 |
draw = ImageDraw.Draw(mask)
|
36 |
|
|
|
37 |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
|
38 |
if detector.config.id2label[label.item()] == "person":
|
39 |
box = [int(i) for i in box.tolist()]
|
|
|
40 |
draw.rectangle(box, fill=255)
|
41 |
|
42 |
-
if
|
43 |
return "No human detected."
|
44 |
|
45 |
-
|
46 |
-
prompt=prompt,
|
47 |
-
negative_prompt="low quality, blurry, extra limbs, bad anatomy, ugly, deformed, poorly drawn",
|
48 |
-
image=input_image,
|
49 |
-
mask_image=mask,
|
50 |
-
guidance_scale=7.5,
|
51 |
-
num_inference_steps=30
|
52 |
-
).images[0]
|
53 |
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
# Gradio UI
|
57 |
with gr.Blocks() as demo:
|
58 |
-
gr.Markdown("##
|
59 |
|
60 |
with gr.Row():
|
61 |
input_image = gr.Image(type="pil", label="Input Image")
|
62 |
output_image = gr.Image(type="pil", label="Output Image")
|
63 |
|
64 |
-
prompt_text = gr.Textbox(label="Prompt", placeholder="
|
|
|
65 |
submit = gr.Button("Submit")
|
66 |
|
67 |
-
submit.click(detect_and_replace, inputs=[input_image, prompt_text], outputs=output_image)
|
68 |
|
69 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import numpy as np
|
4 |
+
from diffusers import DiffusionPipeline
|
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 Inpainting Pipeline
|
12 |
+
pipe = DiffusionPipeline.from_pretrained(
|
13 |
+
"SG161222/RealVisXL_V4.0", # ✅ Realistic human generation model
|
14 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
|
15 |
+
use_safetensors=True
|
16 |
).to(device)
|
17 |
|
18 |
+
# Load DETR for human detection
|
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(input_image, prompt, negative_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 |
+
boxes = []
|
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 boxes:
|
46 |
return "No human detected."
|
47 |
|
48 |
+
output_image = input_image.copy()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
for box in boxes:
|
51 |
+
x1, y1, x2, y2 = box
|
52 |
+
width, height = x2 - x1, y2 - y1
|
53 |
+
|
54 |
+
# Generate imaginary person image
|
55 |
+
generated_image = pipe(
|
56 |
+
prompt=prompt,
|
57 |
+
negative_prompt=negative_prompt,
|
58 |
+
width=512,
|
59 |
+
height=768,
|
60 |
+
guidance_scale=7.5,
|
61 |
+
num_inference_steps=30,
|
62 |
+
output_type="pil"
|
63 |
+
).images[0]
|
64 |
+
|
65 |
+
# Resize generated image to fit the detected box
|
66 |
+
resized_generated = generated_image.resize((width, height))
|
67 |
+
|
68 |
+
# Paste the generated image on the original image at the detected location
|
69 |
+
output_image.paste(resized_generated, (x1, y1))
|
70 |
+
|
71 |
+
return output_image
|
72 |
|
73 |
# Gradio UI
|
74 |
with gr.Blocks() as demo:
|
75 |
+
gr.Markdown("## Replace Bride and Groom with Imaginary Realistic Characters")
|
76 |
|
77 |
with gr.Row():
|
78 |
input_image = gr.Image(type="pil", label="Input Image")
|
79 |
output_image = gr.Image(type="pil", label="Output Image")
|
80 |
|
81 |
+
prompt_text = gr.Textbox(label="Prompt", placeholder="Describe the imaginary bride/groom")
|
82 |
+
negative_prompt_text = gr.Textbox(label="Negative Prompt", placeholder="Optional negative prompt")
|
83 |
submit = gr.Button("Submit")
|
84 |
|
85 |
+
submit.click(detect_and_replace, inputs=[input_image, prompt_text, negative_prompt_text], outputs=output_image)
|
86 |
|
87 |
demo.launch()
|