Spaces:
Paused
Paused
Commit ·
0f1978d
1
Parent(s): e1b8d5e
fix: prevent extra legs from garment reference bleeding into lower bodyTwo fixes for the 'three legs' artifact:1. Prompt: add explicit 'exactly two legs' constraint to upper-body prompt2. Crop: upper-body garments are cropped to top 75% before being used as the reference latent — hanging ties, drawstrings and tails in the bottom quarter were being read as limbs by the model
Browse files
app.py
CHANGED
|
@@ -249,10 +249,10 @@ _PROMPTS = {
|
|
| 249 |
"upper": (
|
| 250 |
"start with Picture 1 as the base image, keeping its lighting, environment, and background. "
|
| 251 |
"Do NOT change the pants, trousers, shorts, skirt, shoes or accessories — keep the entire lower body from Picture 1 pixel-perfect. "
|
| 252 |
-
"
|
| 253 |
-
"
|
| 254 |
-
"
|
| 255 |
-
"
|
| 256 |
"Do not add or alter any lower body clothing. "
|
| 257 |
"Match the pose from Picture 1, high quality, sharp details, 4k. "
|
| 258 |
"Preserve the face, hair and expression from Picture 1 exactly."
|
|
@@ -260,19 +260,17 @@ _PROMPTS = {
|
|
| 260 |
"lower": (
|
| 261 |
"start with Picture 1 as the base image, keeping its lighting, environment, and background. "
|
| 262 |
"Keep the top/shirt/jacket and the shoes/feet/accessories from Picture 1 exactly as they are. "
|
| 263 |
-
"Remove only the pants/skirt/shorts from Picture 1 and replace them with the exact garment from Picture 2
|
| 264 |
-
"
|
| 265 |
-
"
|
| 266 |
-
"Do not simplify or genericise any feature — reproduce it faithfully. "
|
| 267 |
"Match the pose from Picture 1, high quality, sharp details, 4k. "
|
| 268 |
"Preserve the face and expression from Picture 1 exactly."
|
| 269 |
),
|
| 270 |
"full": (
|
| 271 |
"start with Picture 1 as the base image, keeping its lighting, environment, and background. "
|
| 272 |
-
"Remove the entire outfit from Picture 1 and replace it completely with the exact garment from Picture 2
|
| 273 |
-
"
|
| 274 |
-
"
|
| 275 |
-
"Do not simplify or genericise any feature — reproduce it faithfully. "
|
| 276 |
"Match the pose from Picture 1, high quality, sharp details, 4k. "
|
| 277 |
"Preserve the face and expression from Picture 1 exactly."
|
| 278 |
),
|
|
@@ -338,23 +336,14 @@ _PATTERN_CANDIDATES = [
|
|
| 338 |
"camouflage", "color block", "embroidered", "lace",
|
| 339 |
]
|
| 340 |
_SLEEVE_CANDIDATES = [
|
| 341 |
-
"sleeveless", "short sleeve", "long sleeve",
|
| 342 |
-
"balloon or puff sleeve", "cropped sleeve",
|
| 343 |
-
]
|
| 344 |
-
_NECKLINE_CANDIDATES = [
|
| 345 |
-
"v-neck", "round crew neck", "collared button-up shirt",
|
| 346 |
-
"turtleneck", "scoop neck", "off-shoulder neckline",
|
| 347 |
-
]
|
| 348 |
-
_FEATURE_CANDIDATES = [
|
| 349 |
-
"no special features", "tie or bow at waist or neck",
|
| 350 |
-
"buttons down the front", "ruffles or frills", "lace trim",
|
| 351 |
-
"drawstring or elastic waist", "belt", "pockets",
|
| 352 |
]
|
| 353 |
|
| 354 |
def describe_garment(img_pil: Image.Image) -> str:
|
| 355 |
-
"""Use CLIP to detect color, pattern,
|
| 356 |
-
|
| 357 |
-
|
|
|
|
| 358 |
model, processor = _load_clip()
|
| 359 |
|
| 360 |
def _best(candidates, prompt_template):
|
|
@@ -366,16 +355,12 @@ def describe_garment(img_pil: Image.Image) -> str:
|
|
| 366 |
logits = model(**inputs).logits_per_image[0]
|
| 367 |
return candidates[logits.argmax().item()]
|
| 368 |
|
| 369 |
-
color
|
| 370 |
-
pattern
|
| 371 |
-
sleeve
|
| 372 |
-
neckline = _best(_NECKLINE_CANDIDATES, "a {} piece of clothing")
|
| 373 |
-
feature = _best(_FEATURE_CANDIDATES, "a piece of clothing with {}")
|
| 374 |
|
| 375 |
-
# Build compact but information-dense description
|
| 376 |
pattern_str = "" if ("solid" in pattern or "plain" in pattern) else f" {pattern}"
|
| 377 |
-
|
| 378 |
-
desc = f"{color}{pattern_str} {sleeve} {neckline}{feature_str}"
|
| 379 |
print(f"Garment description: {desc}")
|
| 380 |
return desc
|
| 381 |
|
|
@@ -557,6 +542,14 @@ def _run_generate(target_img, clothing_img, garment_type_override, progress):
|
|
| 557 |
clothing_name = f"clothing_{uid}.png"
|
| 558 |
clothing_pil = _boost_contrast(_prep(clothing_img))
|
| 559 |
_prep(target_img).save(f"{COMFYUI_INPUT}/{target_name}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 560 |
clothing_pil.save(f"{COMFYUI_INPUT}/{clothing_name}")
|
| 561 |
|
| 562 |
# Garment type — (1) user dropdown override, (2) CLIP auto-detect inside GPU worker
|
|
@@ -608,16 +601,29 @@ def _run_generate(target_img, clothing_img, garment_type_override, progress):
|
|
| 608 |
)
|
| 609 |
workflow["107"]["inputs"]["text"] = enriched_prompt
|
| 610 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 611 |
# If garment is already on a plain background, bypass SAM3 + PersonMaskUltra.
|
| 612 |
# First strip the background with rembg so the garment is on clean white —
|
| 613 |
# this prevents beige/cream backgrounds from confusing the model.
|
| 614 |
if product_photo:
|
| 615 |
print("Product photo — removing background with rembg, bypassing SAM3")
|
| 616 |
-
clothing_clean = remove_garment_bg(
|
| 617 |
clean_name = f"clothing_clean_{uid}.png"
|
| 618 |
clothing_clean.save(f"{COMFYUI_INPUT}/{clean_name}")
|
| 619 |
workflow["81"]["inputs"]["image"] = clean_name
|
| 620 |
workflow["110"]["inputs"]["image"] = ["81", 0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 621 |
|
| 622 |
# Submit to ComfyUI
|
| 623 |
progress(0.05, desc="Submitting to ComfyUI...")
|
|
|
|
| 249 |
"upper": (
|
| 250 |
"start with Picture 1 as the base image, keeping its lighting, environment, and background. "
|
| 251 |
"Do NOT change the pants, trousers, shorts, skirt, shoes or accessories — keep the entire lower body from Picture 1 pixel-perfect. "
|
| 252 |
+
"The person has exactly two legs — preserve both legs exactly as they appear in Picture 1, do not add, remove, or alter any limbs. "
|
| 253 |
+
"Remove only the top/shirt/jacket from Picture 1 and replace it with the exact garment shown in Picture 2, "
|
| 254 |
+
"strictly preserving the color, fabric texture, pattern, sleeve style, silhouette, and overall design of the garment in Picture 2. "
|
| 255 |
+
"Only add features that are clearly visible in Picture 2 — do not invent or add details not present in Picture 2. "
|
| 256 |
"Do not add or alter any lower body clothing. "
|
| 257 |
"Match the pose from Picture 1, high quality, sharp details, 4k. "
|
| 258 |
"Preserve the face, hair and expression from Picture 1 exactly."
|
|
|
|
| 260 |
"lower": (
|
| 261 |
"start with Picture 1 as the base image, keeping its lighting, environment, and background. "
|
| 262 |
"Keep the top/shirt/jacket and the shoes/feet/accessories from Picture 1 exactly as they are. "
|
| 263 |
+
"Remove only the pants/skirt/shorts from Picture 1 and replace them with the exact garment from Picture 2, "
|
| 264 |
+
"strictly preserving its color, fabric, pattern, length, silhouette, and overall design. "
|
| 265 |
+
"Only add features that are clearly visible in Picture 2 — do not invent or add details not present in Picture 2. "
|
|
|
|
| 266 |
"Match the pose from Picture 1, high quality, sharp details, 4k. "
|
| 267 |
"Preserve the face and expression from Picture 1 exactly."
|
| 268 |
),
|
| 269 |
"full": (
|
| 270 |
"start with Picture 1 as the base image, keeping its lighting, environment, and background. "
|
| 271 |
+
"Remove the entire outfit from Picture 1 and replace it completely with the exact garment from Picture 2, "
|
| 272 |
+
"strictly preserving its color, fabric, pattern, silhouette, sleeve style, neckline, and overall design. "
|
| 273 |
+
"Only add features that are clearly visible in Picture 2 — do not invent or add details not present in Picture 2. "
|
|
|
|
| 274 |
"Match the pose from Picture 1, high quality, sharp details, 4k. "
|
| 275 |
"Preserve the face and expression from Picture 1 exactly."
|
| 276 |
),
|
|
|
|
| 336 |
"camouflage", "color block", "embroidered", "lace",
|
| 337 |
]
|
| 338 |
_SLEEVE_CANDIDATES = [
|
| 339 |
+
"sleeveless", "short sleeve", "long sleeve",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 340 |
]
|
| 341 |
|
| 342 |
def describe_garment(img_pil: Image.Image) -> str:
|
| 343 |
+
"""Use CLIP to detect color, pattern, and sleeve length.
|
| 344 |
+
Kept intentionally narrow — CLIP zero-shot is reliable for these broad categories
|
| 345 |
+
but unreliable for fine-grained attributes (necklines, ruffles, ties), which cause
|
| 346 |
+
the model to hallucinate features that aren't there if CLIP guesses wrong."""
|
| 347 |
model, processor = _load_clip()
|
| 348 |
|
| 349 |
def _best(candidates, prompt_template):
|
|
|
|
| 355 |
logits = model(**inputs).logits_per_image[0]
|
| 356 |
return candidates[logits.argmax().item()]
|
| 357 |
|
| 358 |
+
color = _best(_COLOR_CANDIDATES, "a {} colored piece of clothing")
|
| 359 |
+
pattern = _best(_PATTERN_CANDIDATES, "a {} fabric or textile")
|
| 360 |
+
sleeve = _best(_SLEEVE_CANDIDATES, "a piece of clothing with {} sleeves")
|
|
|
|
|
|
|
| 361 |
|
|
|
|
| 362 |
pattern_str = "" if ("solid" in pattern or "plain" in pattern) else f" {pattern}"
|
| 363 |
+
desc = f"{color}{pattern_str} {sleeve}"
|
|
|
|
| 364 |
print(f"Garment description: {desc}")
|
| 365 |
return desc
|
| 366 |
|
|
|
|
| 542 |
clothing_name = f"clothing_{uid}.png"
|
| 543 |
clothing_pil = _boost_contrast(_prep(clothing_img))
|
| 544 |
_prep(target_img).save(f"{COMFYUI_INPUT}/{target_name}")
|
| 545 |
+
|
| 546 |
+
def _crop_upper_garment(img_pil):
|
| 547 |
+
"""Crop an upper-body garment image to its top 75%.
|
| 548 |
+
Hanging elements (ties, tails) in the bottom quarter can be misread
|
| 549 |
+
as legs by the model when they appear in the reference latent."""
|
| 550 |
+
w, h = img_pil.size
|
| 551 |
+
return img_pil.crop((0, 0, w, int(h * 0.75)))
|
| 552 |
+
|
| 553 |
clothing_pil.save(f"{COMFYUI_INPUT}/{clothing_name}")
|
| 554 |
|
| 555 |
# Garment type — (1) user dropdown override, (2) CLIP auto-detect inside GPU worker
|
|
|
|
| 601 |
)
|
| 602 |
workflow["107"]["inputs"]["text"] = enriched_prompt
|
| 603 |
|
| 604 |
+
# For upper-body garments, crop to top 75% so that hanging elements
|
| 605 |
+
# (ties, belts, tails) don't bleed into the lower-body reference latent
|
| 606 |
+
# and get misread as limbs by the model.
|
| 607 |
+
clothing_ref = clothing_pil
|
| 608 |
+
if garment_type == "upper":
|
| 609 |
+
clothing_ref = _crop_upper_garment(clothing_pil)
|
| 610 |
+
print(f"Cropped upper garment to 75% height: {clothing_ref.size}")
|
| 611 |
+
|
| 612 |
# If garment is already on a plain background, bypass SAM3 + PersonMaskUltra.
|
| 613 |
# First strip the background with rembg so the garment is on clean white —
|
| 614 |
# this prevents beige/cream backgrounds from confusing the model.
|
| 615 |
if product_photo:
|
| 616 |
print("Product photo — removing background with rembg, bypassing SAM3")
|
| 617 |
+
clothing_clean = remove_garment_bg(clothing_ref)
|
| 618 |
clean_name = f"clothing_clean_{uid}.png"
|
| 619 |
clothing_clean.save(f"{COMFYUI_INPUT}/{clean_name}")
|
| 620 |
workflow["81"]["inputs"]["image"] = clean_name
|
| 621 |
workflow["110"]["inputs"]["image"] = ["81", 0]
|
| 622 |
+
elif garment_type == "upper":
|
| 623 |
+
# Save the cropped version as the reference even when not a product photo
|
| 624 |
+
cropped_name = f"clothing_crop_{uid}.png"
|
| 625 |
+
clothing_ref.save(f"{COMFYUI_INPUT}/{cropped_name}")
|
| 626 |
+
workflow["81"]["inputs"]["image"] = cropped_name
|
| 627 |
|
| 628 |
# Submit to ComfyUI
|
| 629 |
progress(0.05, desc="Submitting to ComfyUI...")
|