Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,9 @@ import spaces
|
|
| 8 |
|
| 9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
-
# Load
|
| 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,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
|
| 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 |
-
#
|
| 66 |
-
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
|