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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -8,9 +8,9 @@ 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)
@@ -47,13 +47,13 @@ def detect_and_replace(input_image, prompt, negative_prompt=""):
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,
@@ -62,11 +62,14 @@ def detect_and_replace(input_image, prompt, negative_prompt=""):
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
 
 
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)
 
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,
 
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