Add FLUX 3 Klein layout outputs to clothing planner
This commit is contained in:
@@ -352,6 +352,81 @@ def build_layout_prompt(plan: dict[str, Any]) -> str:
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _flux3_bbox(region: dict[str, Any], padding_percent: float = 0.0) -> list[int]:
|
||||
"""Convert a normalized region to FLUX.3's 0-1000 box format."""
|
||||
|
||||
padding = _bounded_number(padding_percent, "flux3_box_padding", 0, 40) / 100
|
||||
x, y = float(region["x"]), float(region["y"])
|
||||
width, height = float(region["width"]), float(region["height"])
|
||||
# Padding is internal: shrink the target box symmetrically without moving
|
||||
# it outside the layout or changing the original legacy plan.
|
||||
x += width * padding
|
||||
y += height * padding
|
||||
width *= max(0.0, 1 - 2 * padding)
|
||||
height *= max(0.0, 1 - 2 * padding)
|
||||
top = max(0, min(1000, round(y * 1000)))
|
||||
left = max(0, min(1000, round(x * 1000)))
|
||||
bottom = max(0, min(1000, round((y + height) * 1000)))
|
||||
right = max(0, min(1000, round((x + width) * 1000)))
|
||||
if top >= 1000:
|
||||
top = 999
|
||||
if left >= 1000:
|
||||
left = 999
|
||||
if bottom <= top:
|
||||
bottom = min(1000, top + 1)
|
||||
if right <= left:
|
||||
right = min(1000, left + 1)
|
||||
return [top, left, bottom, right]
|
||||
|
||||
|
||||
def build_flux3_boxes(
|
||||
plan: dict[str, Any],
|
||||
padding_percent: float = 0.0,
|
||||
include_descriptions: bool = True,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Build FLUX.3 generation-style layout rows from an existing plan."""
|
||||
|
||||
rows = []
|
||||
for region in plan["regions"]:
|
||||
description = region["description"] if include_descriptions else ""
|
||||
if not description:
|
||||
description = region["name"]
|
||||
rows.append({
|
||||
"id": region["id"],
|
||||
"bbox": _flux3_bbox(region, padding_percent),
|
||||
"desc": description,
|
||||
})
|
||||
return rows
|
||||
|
||||
|
||||
def build_flux3_prompt(
|
||||
plan: dict[str, Any],
|
||||
padding_percent: float = 0.0,
|
||||
include_descriptions: bool = True,
|
||||
) -> tuple[str, str]:
|
||||
"""Return a FLUX.3 caption and its trailing JSON box array."""
|
||||
|
||||
boxes = build_flux3_boxes(plan, padding_percent, include_descriptions)
|
||||
if not boxes:
|
||||
return "", "[]"
|
||||
references = []
|
||||
for region in plan["regions"]:
|
||||
references.append(f"{region['name']} <{region['id']}>")
|
||||
caption = (
|
||||
"A photorealistic 90-degree overhead flat-lay product photograph of "
|
||||
"the exact physical objects identified in the supplied reference image, "
|
||||
"arranged individually on a clean neutral surface with realistic materials, "
|
||||
"natural fabric drape, soft studio lighting, and subtle contact shadows. "
|
||||
f"The scene contains exactly these unique inventory entries: {', '.join(references)}. "
|
||||
"Reproduce each item's original colours, materials, construction, prints, "
|
||||
"graphics, and visible details faithfully. Display each object only once "
|
||||
"inside its allocated region. Do not include the original person, mannequin, "
|
||||
"duplicate garments, invented accessories, or unrelated props."
|
||||
)
|
||||
boxes_json = json.dumps(boxes, ensure_ascii=False, separators=(",", ":"))
|
||||
return f"{caption} {boxes_json}", boxes_json
|
||||
|
||||
|
||||
def _background(value: str) -> tuple[int, int, int]:
|
||||
text = str(value or DEFAULT_BACKGROUND).strip().lstrip("#")
|
||||
if len(text) == 3:
|
||||
@@ -394,8 +469,8 @@ def render_preview(plan: dict[str, Any], background_color: str, show_labels: boo
|
||||
class DumasClothingLayoutPlanner:
|
||||
CATEGORY = "Dumas/Image/Layout"
|
||||
DESCRIPTION = "Create a deterministic object layout from VLM inventory JSON."
|
||||
RETURN_TYPES = ("STRING", "STRING", "IMAGE")
|
||||
RETURN_NAMES = ("layout_prompt", "layout_json", "layout_preview")
|
||||
RETURN_TYPES = ("STRING", "STRING", "IMAGE", "STRING", "STRING")
|
||||
RETURN_NAMES = ("layout_prompt", "layout_json", "layout_preview", "flux3_prompt", "flux3_boxes_json")
|
||||
FUNCTION = "plan"
|
||||
|
||||
@classmethod
|
||||
@@ -409,10 +484,19 @@ class DumasClothingLayoutPlanner:
|
||||
"spacing_percent": ("FLOAT", {"default": 3.0, "min": 0.0, "max": 20.0, "step": 0.5}),
|
||||
"background_color": ("STRING", {"default": DEFAULT_BACKGROUND}),
|
||||
"show_labels": ("BOOLEAN", {"default": True}),
|
||||
"output_format": (["legacy", "flux3", "both"], {"default": "both"}),
|
||||
"flux3_box_padding": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 40.0, "step": 0.5}),
|
||||
"flux3_include_descriptions": ("BOOLEAN", {"default": True}),
|
||||
"flux3_reference_mode": (["generation", "editing"], {"default": "generation"}),
|
||||
}}
|
||||
|
||||
def plan(self, inventory_json, canvas_width, canvas_height, layout_mode, margin_percent, spacing_percent, background_color, show_labels):
|
||||
def plan(self, inventory_json, canvas_width, canvas_height, layout_mode, margin_percent, spacing_percent, background_color, show_labels, output_format, flux3_box_padding, flux3_include_descriptions, flux3_reference_mode):
|
||||
plan = plan_layout(inventory_json, canvas_width, canvas_height, layout_mode, margin_percent, spacing_percent)
|
||||
if output_format in {"flux3", "both"} and flux3_reference_mode == "editing":
|
||||
raise ValueError(
|
||||
"flux3_reference_mode='editing' requires source bounding boxes and reference-image identifiers; "
|
||||
"the current inventory schema only supports generation mode"
|
||||
)
|
||||
preview = render_preview(plan, background_color, show_labels)
|
||||
try:
|
||||
import numpy as np
|
||||
@@ -420,7 +504,12 @@ class DumasClothingLayoutPlanner:
|
||||
tensor = torch.from_numpy(np.asarray(preview).astype("float32") / 255.0)[None, ...]
|
||||
except ImportError as exc:
|
||||
raise RuntimeError("DUMAS Clothing Layout Planner requires ComfyUI's torch and numpy runtime") from exc
|
||||
return build_layout_prompt(plan), json.dumps(plan, ensure_ascii=False, indent=2), tensor
|
||||
flux3_prompt, flux3_boxes_json = "", "[]"
|
||||
if output_format in {"flux3", "both"}:
|
||||
flux3_prompt, flux3_boxes_json = build_flux3_prompt(
|
||||
plan, flux3_box_padding, flux3_include_descriptions
|
||||
)
|
||||
return build_layout_prompt(plan), json.dumps(plan, ensure_ascii=False, indent=2), tensor, flux3_prompt, flux3_boxes_json
|
||||
|
||||
|
||||
NODE_CLASS_MAPPINGS = {"DumasClothingLayoutPlanner": DumasClothingLayoutPlanner}
|
||||
|
||||
Reference in New Issue
Block a user