Add FLUX 3 Klein layout outputs to clothing planner

This commit is contained in:
2026-11-04 10:36:37 +00:00
parent 91c6659306
commit f677b7e567
3 changed files with 147 additions and 4 deletions
+16
View File
@@ -140,6 +140,22 @@ Outputs:
- `layout_json`: reusable normalized coordinates and warnings - `layout_json`: reusable normalized coordinates and warnings
- `layout_preview`: a diagnostic IMAGE showing the regions and optional labels - `layout_preview`: a diagnostic IMAGE showing the regions and optional labels
Optional FLUX.3 outputs:
- `flux3_prompt`: a complete FLUX.3 scene caption followed by the native
bounding-box JSON array. Connect this to the FLUX.3 Klein prompt input; keep
the workflow's official prompt upsampler in the normal position.
- `flux3_boxes_json`: the same bounding-box array separately for inspection or
downstream processing.
The `output_format` setting selects `legacy`, `flux3`, or `both` (default).
FLUX.3 boxes use `[top, left, bottom, right]` integer coordinates on a 0–1000
top-left-origin grid. Every generated ID is also emitted as `<id>` in the
caption. `flux3_reference_mode=editing` is intentionally rejected until source
coordinates and reference-image identifiers are available; the current VLM
inventory schema supports generation-style layouts only. Bounding boxes guide
placement and scale but are not hard pixel masks.
The planner uses normalized top-left-origin coordinates, configurable margins The planner uses normalized top-left-origin coordinates, configurable margins
and spacing, and rejects malformed or invalid inventory fields with actionable and spacing, and rejects malformed or invalid inventory fields with actionable
errors. The preview is diagnostic only; feeding it to an image model does not errors. The preview is diagnostic only; feeding it to an image model does not
+93 -4
View File
@@ -352,6 +352,81 @@ def build_layout_prompt(plan: dict[str, Any]) -> str:
return "\n".join(lines) 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]: def _background(value: str) -> tuple[int, int, int]:
text = str(value or DEFAULT_BACKGROUND).strip().lstrip("#") text = str(value or DEFAULT_BACKGROUND).strip().lstrip("#")
if len(text) == 3: if len(text) == 3:
@@ -394,8 +469,8 @@ def render_preview(plan: dict[str, Any], background_color: str, show_labels: boo
class DumasClothingLayoutPlanner: class DumasClothingLayoutPlanner:
CATEGORY = "Dumas/Image/Layout" CATEGORY = "Dumas/Image/Layout"
DESCRIPTION = "Create a deterministic object layout from VLM inventory JSON." DESCRIPTION = "Create a deterministic object layout from VLM inventory JSON."
RETURN_TYPES = ("STRING", "STRING", "IMAGE") RETURN_TYPES = ("STRING", "STRING", "IMAGE", "STRING", "STRING")
RETURN_NAMES = ("layout_prompt", "layout_json", "layout_preview") RETURN_NAMES = ("layout_prompt", "layout_json", "layout_preview", "flux3_prompt", "flux3_boxes_json")
FUNCTION = "plan" FUNCTION = "plan"
@classmethod @classmethod
@@ -409,10 +484,19 @@ class DumasClothingLayoutPlanner:
"spacing_percent": ("FLOAT", {"default": 3.0, "min": 0.0, "max": 20.0, "step": 0.5}), "spacing_percent": ("FLOAT", {"default": 3.0, "min": 0.0, "max": 20.0, "step": 0.5}),
"background_color": ("STRING", {"default": DEFAULT_BACKGROUND}), "background_color": ("STRING", {"default": DEFAULT_BACKGROUND}),
"show_labels": ("BOOLEAN", {"default": True}), "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) 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) preview = render_preview(plan, background_color, show_labels)
try: try:
import numpy as np import numpy as np
@@ -420,7 +504,12 @@ class DumasClothingLayoutPlanner:
tensor = torch.from_numpy(np.asarray(preview).astype("float32") / 255.0)[None, ...] tensor = torch.from_numpy(np.asarray(preview).astype("float32") / 255.0)[None, ...]
except ImportError as exc: except ImportError as exc:
raise RuntimeError("DUMAS Clothing Layout Planner requires ComfyUI's torch and numpy runtime") from 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} NODE_CLASS_MAPPINGS = {"DumasClothingLayoutPlanner": DumasClothingLayoutPlanner}
@@ -111,6 +111,44 @@ class ClothingLayoutPlannerTests(unittest.TestCase):
self.assertIn("cotton shirt", prompt) self.assertIn("cotton shirt", prompt)
self.assertIn("region", prompt) self.assertIn("region", prompt)
def test_flux3_boxes_use_official_order_and_1000_grid(self):
plan = PLANNER.plan_layout(inventory(
{"id": "item_001", "name": "blue shirt", "description": "blue cotton"},
{"id": "item_002", "name": "red bag", "description": "red leather"},
), 1200, 800, "grid")
rows = PLANNER.build_flux3_boxes(plan)
self.assertEqual(len(rows), 2)
for row in rows:
top, left, bottom, right = row["bbox"]
self.assertTrue(0 <= top < bottom <= 1000)
self.assertTrue(0 <= left < right <= 1000)
self.assertEqual(rows[0]["desc"], "blue cotton")
def test_flux3_prompt_has_matching_tags_and_trailing_json(self):
plan = PLANNER.plan_layout(inventory({
"id": "item_001", "name": "blue shirt", "description": "blue cotton",
}))
prompt, boxes_json = PLANNER.build_flux3_prompt(plan)
self.assertIn("<item_001>", prompt)
self.assertTrue(prompt.endswith(boxes_json))
self.assertEqual(json.loads(boxes_json)[0]["id"], "item_001")
def test_flux3_padding_shrinks_without_changing_legacy_plan(self):
plan = PLANNER.plan_layout(inventory({"id": "item_001", "name": "shirt"}))
original = dict(plan["regions"][0])
padded = PLANNER.build_flux3_boxes(plan, 10)[0]["bbox"]
self.assertEqual(plan["regions"][0], original)
self.assertGreater(padded[2] - padded[0], 0)
self.assertGreater(padded[3] - padded[1], 0)
def test_flux3_editing_mode_requires_source_coordinates(self):
with self.assertRaisesRegex(ValueError, "editing.*source bounding boxes"):
PLANNER.DumasClothingLayoutPlanner().plan(
inventory({"id": "item_001", "name": "shirt"}),
640, 480, "balanced", 5, 3, "#EEE8DC", True,
"flux3", 0, True, "editing",
)
def test_invalid_fields_have_actionable_errors(self): def test_invalid_fields_have_actionable_errors(self):
with self.assertRaisesRegex(ValueError, "invalid unit"): with self.assertRaisesRegex(ValueError, "invalid unit"):
PLANNER.normalize_inventory(inventory({"id": "x", "name": "X", "unit": "trio"})) PLANNER.normalize_inventory(inventory({"id": "x", "name": "X", "unit": "trio"}))