Munaf1987 commited on
Commit
ba3051b
·
verified ·
1 Parent(s): 7bab521

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -41
app.py CHANGED
@@ -1,26 +1,26 @@
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 realistic human generation model
12
- pipe = DiffusionPipeline.from_pretrained(
13
- "SG161222/RealVisXL_V4.0",
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,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
- 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 idx, box in enumerate(boxes):
51
- x1, y1, x2, y2 = box
52
- width, height = x2 - x1, y2 - y1
53
-
54
- # Generate one realistic human image per person
55
- generated_image = pipe(
56
- prompt=f"{prompt}, full body, plain background, isolated subject",
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
- # Crop the subject to avoid white borders
66
- cropped_generated = generated_image.crop(generated_image.getbbox())
67
-
68
- # Resize generated image to fit detected box
69
- resized_generated = cropped_generated.resize((width, height))
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 with Imaginary Realistic Characters")
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="Describe the imaginary bride/groom")
85
- negative_prompt_text = gr.Textbox(label="Negative Prompt", placeholder="Optional negative prompt")
86
  submit = gr.Button("Submit")
87
 
88
- submit.click(detect_and_replace, inputs=[input_image, prompt_text, negative_prompt_text], outputs=output_image)
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()