HEL-892: add pinned controlnet aux reference widget conversion

Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
leefer
2026-10-29 23:48:20 +08:00
co-authored by multica-agent
parent d3c8ab1238
commit 28d05b4dc3
15 changed files with 3649 additions and 16 deletions
@@ -18,6 +18,7 @@ from compare_frontend_exports import differences
from calliope.comfyui.companions import load_companions
from calliope.comfyui.delivery_scope import exclusion_for
from calliope.comfyui.serialization_reference import (
AUX_PROFILE,
PROFILE,
resolve_reference_link,
serialize_reference_widgets,
@@ -25,7 +26,7 @@ from calliope.comfyui.serialization_reference import (
from calliope.comfyui.ui2api import convert, convert_reference
def audit(repo: Path) -> dict:
def audit(repo: Path, profile: str = PROFILE) -> dict:
rows = []
records = load_companions(repo / "docs/hel-892/supplied-api/manifest.json")
for path, record in records.items():
@@ -44,7 +45,7 @@ def audit(repo: Path) -> dict:
if node.get("type") in ("Reroute", "Note", "MarkdownNote"):
continue
try:
values = serialize_reference_widgets(node)
values = serialize_reference_widgets(node, profile)
except ValueError as exc:
unsupported.append({"node_id": nid, "type": node["type"], "reason": str(exc)})
else:
@@ -86,7 +87,7 @@ def audit(repo: Path) -> dict:
"differences": differences(expected, endpoint),
}
)
candidate = convert_reference(source)
candidate = convert_reference(source, profile)
rows.append(
{
"id": binding["id"],
@@ -109,9 +110,19 @@ def audit(repo: Path) -> dict:
}
)
return {
"profile": PROFILE,
"profile": profile,
"authority": "public_reference_only_not_object_info",
"comparison_scope": "supported root scalar widgets and legacy root link endpoints only",
"reference_conditions": {
"unmodified_frontend_widgets_assumed": True,
"ue_extension_serializer_match": "unknown",
"aux_release_artifact_commit_match": "unknown"
if profile == AUX_PROFILE
else "not_used",
"aio_combo_membership": "unknown_not_evaluated",
"model_and_media_files_available": "unknown",
"resolution_bounds": "schema metadata only; integer values serialized without clamping",
},
"target_version_match": "unknown",
"whole_source_equivalence_verified": False,
"execution_verified": False,
@@ -123,8 +134,9 @@ def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--repo", type=Path, required=True)
parser.add_argument("--output", type=Path, required=True)
parser.add_argument("--profile", choices=(PROFILE, AUX_PROFILE), default=PROFILE)
args = parser.parse_args()
report = audit(args.repo)
report = audit(args.repo, args.profile)
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n")
print(json.dumps({"reference_only": True, "active_cases": len(report["cases"])}))
@@ -1,8 +1,8 @@
"""Small, explicit public-reference contract, never a target object_info snapshot.
Sources and limits: docs/hel-892/serialization-evidence/sources.json.
Only unmodified scalar core widgets are covered. No plugin, dynamic prompt,
seed controller, subgraph, bypass or custom serializeValue is inferred.
Sources and limits: docs/hel-892/{serialization-evidence,controlnet-reference}/sources.json.
Only explicitly pinned scalar layouts are covered. No dynamic prompt, seed
controller, subgraph, bypass or custom serializeValue is inferred.
"""
from __future__ import annotations
@@ -10,6 +10,20 @@ from __future__ import annotations
from typing import Any
PROFILE = "comfy-frontend-1.37.11-core-reference"
AUX_PROFILE = "comfy-frontend-1.37.11-controlnet-aux-e8b689a-reference"
AUX_COMMIT = "e8b689a513c3e6b63edc44066560ca5919c0576e"
# pyproject declares 1.1.5 at AUX_COMMIT; registry artifact mapping and target
# installation remain unknown. This opt-in profile assumes unmodified widgets,
# including no UE serializer changes. It never verifies dynamic combo membership.
_AUX_LAYOUTS = {
"AIO_Preprocessor": (("preprocessor", str), ("resolution", int)),
"DepthAnythingPreprocessor": (("ckpt_name", str), ("resolution", int)),
}
_DEPTH_CHECKPOINTS = (
"depth_anything_vitl14.pth",
"depth_anything_vitb14.pth",
"depth_anything_vits14.pth",
)
# Widget construction order, including workflow-only upload button. A None type
# is a workflow-persisted widget with options.serialize=false in graphToPrompt.
@@ -26,8 +40,8 @@ _LAYOUTS = {
}
def reference_definitions() -> dict[str, Any]:
"""Only public core reference schemas; no model/file availability assertion."""
def reference_definitions(profile: str = PROFILE) -> dict[str, Any]:
"""Only opt-in public reference schemas; no model/file availability assertion."""
def spec(required, outputs, optional=None):
return {
@@ -38,7 +52,9 @@ def reference_definitions() -> dict[str, Any]:
"output": outputs,
}
return {
if profile not in (PROFILE, AUX_PROFILE):
raise ValueError("未知参考版本")
definitions = {
"Canny": spec(
{"image": "IMAGE", "low_threshold": "FLOAT", "high_threshold": "FLOAT"}, ["IMAGE"]
),
@@ -57,9 +73,22 @@ def reference_definitions() -> dict[str, Any]:
"PreviewImage": spec({"images": "IMAGE"}, []),
}
if profile == AUX_PROFILE:
for ct, layout in _AUX_LAYOUTS.items():
definitions[ct] = spec(
{"image": "IMAGE"},
["IMAGE"],
{name: "INT" if kind is int else "COMBO" for name, kind in layout},
)
return definitions
def serialize_reference_widgets(node: dict[str, Any]) -> dict[str, Any]:
def serialize_reference_widgets(node: dict[str, Any], profile: str = PROFILE) -> dict[str, Any]:
"""Apply only the pinned identity serializers; reject any extra/missing value."""
if profile not in (PROFILE, AUX_PROFILE):
raise ValueError("未知参考版本")
if profile == AUX_PROFILE and node.get("type") in _AUX_LAYOUTS:
return _serialize_aux(node)
if node.get("mode", 0) != 0:
raise ValueError("静音/旁路不在序列化参考契约内")
properties = node.get("properties") or {}
@@ -87,6 +116,48 @@ def serialize_reference_widgets(node: dict[str, Any]) -> dict[str, Any]:
return result
def _serialize_aux(node: dict[str, Any]) -> dict[str, Any]:
"""Pinned optional widget order; no defaults or guessed dynamic options.
AIO strings are serialized verbatim, with membership explicitly unverified.
DepthAnything's static enum proves a declared name, never file availability.
Resolution bounds are schema metadata, not a clamp/rounding serializer.
"""
properties = node.get("properties") or {}
if (
node.get("mode", 0) != 0
or properties.get("cnr_id") != "comfyui_controlnet_aux"
or properties.get("ver") != "1.1.5"
):
raise ValueError("节点提供方/声明版本/模式不符合固定 aux 参考")
layout = _AUX_LAYOUTS[node["type"]]
values = node.get("widgets_values")
if not isinstance(values, list) or len(values) != len(layout):
raise ValueError("aux 控件必须为固定的两个标量;不补默认值")
result = {}
for (name, kind), value in zip(layout, values):
if type(value) is not kind:
raise ValueError(f"{name} 的类型不符合 aux 标量契约")
result[name] = value
if (
node["type"] == "DepthAnythingPreprocessor"
and result["ckpt_name"] not in _DEPTH_CHECKPOINTS
):
raise ValueError("ckpt_name 不在固定源码静态枚举内")
socket_types = {"image": "IMAGE", layout[0][0]: "COMBO", "resolution": "INT"}
sockets = node.get("inputs", [])
names = [s.get("name") for s in sockets]
if len(names) != len(set(names)) or "image" not in names:
raise ValueError("aux 输入槽重复或缺少 image")
for socket in sockets:
if socket.get("type") != socket_types.get(socket.get("name")):
raise ValueError("aux 输入槽名称/类型不符合固定参考")
widget = socket.get("widget")
if widget is not None and widget != {"name": socket["name"]}:
raise ValueError("aux 输入控件绑定不符合固定参考")
return result
def validate_reroute(node: dict[str, Any]) -> None:
if (
node.get("mode", 0) != 0
@@ -13,6 +13,7 @@ from dataclasses import dataclass, field
from typing import Any
from .serialization_reference import (
AUX_PROFILE,
PROFILE,
reference_definitions,
resolve_reference_link,
@@ -38,21 +39,27 @@ def convert_reference(source: Any, profile: str = PROFILE) -> Conversion:
An unknown profile is rejected, not interpreted as a widget-order contract.
Public reference definitions cannot establish target execution eligibility.
"""
if profile != PROFILE or not isinstance(source, dict) or "nodes" not in source:
if (
profile not in (PROFILE, AUX_PROFILE)
or not isinstance(source, dict)
or "nodes" not in source
):
return Conversion(
None,
[{"code": "invalid_reference_contract", "message": "未知参考版本或不是 UI 图"}],
True,
)
try:
result = _convert(source, reference_definitions(), reference=True)
result = _convert(
source, reference_definitions(profile), reference=True, reference_profile=profile
)
except (ValueError, TypeError, KeyError, AttributeError, IndexError) as exc:
return Conversion(None, [{"code": "invalid_reference_contract", "message": str(exc)}], True)
result.reference_only = True
return result
def _convert(source, object_info=None, *, reference=False) -> Conversion:
def _convert(source, object_info=None, *, reference=False, reference_profile=PROFILE) -> Conversion:
issues: list[dict[str, Any]] = []
def fail(code: str, message: str, node_id: Any = "") -> None:
@@ -154,7 +161,7 @@ def _convert(source, object_info=None, *, reference=False) -> Conversion:
inputs = {}
if reference:
try:
inputs.update(serialize_reference_widgets(node))
inputs.update(serialize_reference_widgets(node, reference_profile))
if [s.get("type") for s in node.get("outputs", [])] != spec["output"]:
raise ValueError("源输出槽不符合固定参考契约")
names = [s.get("name") for s in node.get("inputs", [])]
@@ -0,0 +1,177 @@
"""Pinned-source contracts and real-source mutations, never target execution."""
import copy
import json
from pathlib import Path
import pytest
from calliope.comfyui.serialization_reference import (
AUX_PROFILE,
PROFILE,
reference_definitions,
resolve_reference_link,
serialize_reference_widgets,
)
from calliope.comfyui.ui2api import convert, convert_reference
REPO = Path(__file__).resolve().parents[2]
@pytest.fixture
def case01():
record = json.loads((REPO / "docs/hel-892/supplied-api/manifest.json").read_text())["cases"][0]
return json.loads((REPO / record["source_path"]).read_bytes())
def by_id(graph, nid):
return next(n for n in graph["nodes"] if n["id"] == nid)
def test_real_full_candidate_includes_all_fields_and_preserves_raw_meta_differences(case01):
api = json.loads((REPO / "docs/hel-892/supplied-api/01.api.json").read_bytes())
before = copy.deepcopy(case01)
result = convert_reference(case01, AUX_PROFILE)
assert result.reference_only and not result.issues
assert len(result.prompt) == len(api) == 22
for nid, node in result.prompt.items():
assert node["class_type"] == api[nid]["class_type"]
assert node["inputs"] == api[nid]["inputs"]
for name, value in node["inputs"].items():
assert type(value) is type(api[nid]["inputs"][name])
assert node["_meta"]["title"] == (
by_id(case01, int(nid)).get("title") or node["class_type"]
)
assert sum(result.prompt[n]["_meta"] != api[n]["_meta"] for n in api) == 13
assert case01 == before
assert convert_reference(case01, PROFILE).prompt is None
assert convert(case01).prompt is None
assert convert(case01, reference_definitions(AUX_PROFILE)).prompt is None
@pytest.mark.parametrize("nid", [1, 12, 18, 19, 20, 21, 33, 34, 4])
def test_each_real_aux_node_serializes_changed_values_not_api_constants(case01, nid):
original_image = convert_reference(case01, AUX_PROFILE).prompt[str(nid)]["inputs"]["image"]
node = by_id(case01, nid)
field = "ckpt_name" if nid == 4 else "preprocessor"
selected = "depth_anything_vits14.pth" if nid == 4 else "none"
node["widgets_values"] = [selected, 768]
candidate = convert_reference(case01, AUX_PROFILE)
assert not candidate.issues
assert candidate.prompt[str(nid)]["inputs"][field] == selected
assert candidate.prompt[str(nid)]["inputs"]["resolution"] == 768
assert candidate.prompt[str(nid)]["inputs"]["image"] == original_image
@pytest.mark.parametrize("nid", [1, 4])
@pytest.mark.parametrize(
"change",
[
"provider",
"version",
"missing_provider",
"missing_version",
"extra_widget",
"missing_widget",
"named_widgets",
"bool_resolution",
"float_resolution",
"combo_type",
"extra_socket",
"wrong_socket_type",
"wrong_binding",
"duplicate_socket",
"missing_image",
"wrong_output",
"bad_link",
"bypass",
],
)
def test_bad_aux_contract_fails_whole_graph(case01, nid, change):
node = by_id(case01, nid)
if change == "provider":
node["properties"]["cnr_id"] = "same_name_other_plugin"
elif change == "version":
node["properties"]["ver"] = "1.1.6"
elif change.startswith("missing_") and change in ("missing_provider", "missing_version"):
node["properties"].pop("cnr_id" if change == "missing_provider" else "ver")
elif change == "extra_widget":
node["widgets_values"].append("unknown")
elif change == "missing_widget":
node["widgets_values"].pop()
elif change == "named_widgets":
node["widgets_values"] = {"resolution": 512}
elif change == "bool_resolution":
node["widgets_values"][1] = True
elif change == "float_resolution":
node["widgets_values"][1] = 512.0
elif change == "combo_type":
node["widgets_values"][0] = ["dynamic", "combo"]
elif change == "extra_socket":
node["inputs"].append({"name": "unknown", "type": "STRING", "link": None})
elif change == "wrong_socket_type":
node["inputs"][0]["type"] = "MASK"
elif change == "wrong_binding":
node["inputs"][1]["widget"] = {"name": "resolution"}
elif change == "duplicate_socket":
node["inputs"].append(copy.deepcopy(node["inputs"][0]))
elif change == "missing_image":
node["inputs"].pop(0)
elif change == "wrong_output":
node["outputs"][0]["type"] = "MASK"
elif change == "bad_link":
node["inputs"][0]["link"] = 9999
elif change == "bypass":
node["mode"] = 4
result = convert_reference(case01, AUX_PROFILE)
assert result.reference_only and result.prompt is None and result.issues
def test_dynamic_combo_membership_is_unknown_and_static_enum_is_checked(case01):
# Synthetic selection is deliberately NOT asserted to exist in any environment.
by_id(case01, 1)["widgets_values"][0] = "UninstalledSyntheticPreprocessor"
result = convert_reference(case01, AUX_PROFILE)
assert result.prompt["1"]["inputs"]["preprocessor"] == "UninstalledSyntheticPreprocessor"
assert result.reference_only
assert convert(case01).prompt is None
by_id(case01, 4)["widgets_values"][0] = "unlisted.pth"
assert convert_reference(case01, AUX_PROFILE).prompt is None
def test_unconnected_optional_slots_may_be_absent_but_values_are_never_defaulted(case01):
by_id(case01, 4)["inputs"] = by_id(case01, 4)["inputs"][:1]
assert convert_reference(case01, AUX_PROFILE).prompt is not None
by_id(case01, 4)["widgets_values"] = []
assert convert_reference(case01, AUX_PROFILE).prompt is None
def test_aux_reroute_keeps_nonzero_source_slot_then_rejects_image_type(case01):
# Synthetic mutation of the real path: LoadImage MASK output through Reroute.
source = by_id(case01, 2)
reroute = by_id(case01, 32)
lid = reroute["inputs"][0]["link"]
link = next(link for link in case01["links"] if link[0] == lid)
source["outputs"][0]["links"].remove(lid)
source["outputs"][1]["links"] = [lid]
link[2] = 1
endpoint = resolve_reference_link(
{str(n["id"]): n for n in case01["nodes"]},
{str(link[0]): link for link in case01["links"]},
by_id(case01, 4)["inputs"][0]["link"],
)
assert endpoint == ["2", 1]
result = convert_reference(case01, AUX_PROFILE)
assert result.prompt is None
assert "link_type_unverified" in {i["code"] for i in result.issues}
def test_existing_reroute_guards_apply_to_extended_profile(case01):
by_id(case01, 32)["inputs"][0]["link"] = None
assert convert_reference(case01, AUX_PROFILE).prompt is None
def test_unknown_named_node_is_never_globally_registered(case01):
by_id(case01, 1)["type"] = "UnknownAux"
assert convert_reference(case01, AUX_PROFILE).prompt is None
with pytest.raises(ValueError):
serialize_reference_widgets(by_id(case01, 4), "invented-profile")
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
@@ -0,0 +1,224 @@
import sys, os
# Disable NPU device initialization and problematic MMCV ops to prevent RuntimeError
# Must be set BEFORE any MMCV imports happen anywhere in ComfyUI
os.environ['NPU_DEVICE_COUNT'] = '0'
os.environ['MMCV_WITH_OPS'] = '0'
from .utils import here, define_preprocessor_inputs, INPUT
from pathlib import Path
import traceback
import importlib
from .log import log, blue_text, cyan_text, get_summary, get_label
from .hint_image_enchance import NODE_CLASS_MAPPINGS as HIE_NODE_CLASS_MAPPINGS
from .hint_image_enchance import NODE_DISPLAY_NAME_MAPPINGS as HIE_NODE_DISPLAY_NAME_MAPPINGS
#Ref: https://github.com/comfyanonymous/ComfyUI/blob/76d53c4622fc06372975ed2a43ad345935b8a551/nodes.py#L17
sys.path.insert(0, str(Path(here, "src").resolve()))
for pkg_name in ["custom_controlnet_aux", "custom_mmpkg"]:
sys.path.append(str(Path(here, "src", pkg_name).resolve()))
#Enable CPU fallback for ops not being supported by MPS like upsample_bicubic2d.out
#https://github.com/pytorch/pytorch/issues/77764
#https://github.com/Fannovel16/comfyui_controlnet_aux/issues/2#issuecomment-1763579485
os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = os.getenv("PYTORCH_ENABLE_MPS_FALLBACK", '1')
def load_nodes():
shorted_errors = []
full_error_messages = []
node_class_mappings = {}
node_display_name_mappings = {}
for filename in (here / "node_wrappers").iterdir():
module_name = filename.stem
if module_name.startswith('.'): continue #Skip hidden files created by the OS (e.g. [.DS_Store](https://en.wikipedia.org/wiki/.DS_Store))
try:
module = importlib.import_module(
f".node_wrappers.{module_name}", package=__package__
)
node_class_mappings.update(getattr(module, "NODE_CLASS_MAPPINGS"))
if hasattr(module, "NODE_DISPLAY_NAME_MAPPINGS"):
node_display_name_mappings.update(getattr(module, "NODE_DISPLAY_NAME_MAPPINGS"))
log.debug(f"Imported {module_name} nodes")
except AttributeError:
pass # wip nodes
except Exception:
error_message = traceback.format_exc()
full_error_messages.append(error_message)
error_message = error_message.splitlines()[-1]
shorted_errors.append(
f"Failed to import module {module_name} because {error_message}"
)
if len(shorted_errors) > 0:
full_err_log = '\n\n'.join(full_error_messages)
print(f"\n\nFull error log from comfyui_controlnet_aux: \n{full_err_log}\n\n")
log.info(
f"Some nodes failed to load:\n\t"
+ "\n\t".join(shorted_errors)
+ "\n\n"
+ "Check that you properly installed the dependencies.\n"
+ "If you think this is a bug, please report it on the github page (https://github.com/Fannovel16/comfyui_controlnet_aux/issues)"
)
return node_class_mappings, node_display_name_mappings
AUX_NODE_MAPPINGS, AUX_DISPLAY_NAME_MAPPINGS = load_nodes()
#For nodes not mapping image to image or has special requirements
AIO_NOT_SUPPORTED = ["InpaintPreprocessor", "MeshGraphormer+ImpactDetector-DepthMapPreprocessor", "DiffusionEdge_Preprocessor"]
AIO_NOT_SUPPORTED += ["SavePoseKpsAsJsonFile", "FacialPartColoringFromPoseKps", "UpperBodyTrackingFromPoseKps", "RenderPeopleKps", "RenderAnimalKps"]
AIO_NOT_SUPPORTED += ["Unimatch_OptFlowPreprocessor", "MaskOptFlow"]
def preprocessor_options():
auxs = list(AUX_NODE_MAPPINGS.keys())
auxs.insert(0, "none")
for name in AIO_NOT_SUPPORTED:
if name in auxs:
auxs.remove(name)
return auxs
PREPROCESSOR_OPTIONS = preprocessor_options()
class AIO_Preprocessor:
@classmethod
def INPUT_TYPES(s):
return define_preprocessor_inputs(
preprocessor=INPUT.COMBO(PREPROCESSOR_OPTIONS, default="none"),
resolution=INPUT.RESOLUTION()
)
RETURN_TYPES = ("IMAGE",)
FUNCTION = "execute"
CATEGORY = "ControlNet Preprocessors"
def execute(self, preprocessor, image, resolution=512):
if preprocessor == "none":
return (image, )
else:
aux_class = AUX_NODE_MAPPINGS[preprocessor]
input_types = aux_class.INPUT_TYPES()
input_types = {
**input_types["required"],
**(input_types["optional"] if "optional" in input_types else {})
}
params = {}
for name, input_type in input_types.items():
if name == "image":
params[name] = image
continue
if name == "resolution":
params[name] = resolution
continue
if len(input_type) == 2 and ("default" in input_type[1]):
params[name] = input_type[1]["default"]
continue
default_values = { "INT": 0, "FLOAT": 0.0 }
if type(input_type[0]) is list:
for input_type_value in input_type[0]:
if input_type_value in default_values:
params[name] = default_values[input_type[0]]
else:
if input_type[0] in default_values:
params[name] = default_values[input_type[0]]
return getattr(aux_class(), aux_class.FUNCTION)(**params)
class ControlNetAuxSimpleAddText:
@classmethod
def INPUT_TYPES(s):
return dict(
required=dict(image=INPUT.IMAGE(), text=INPUT.STRING())
)
RETURN_TYPES = ("IMAGE",)
FUNCTION = "execute"
CATEGORY = "ControlNet Preprocessors"
def execute(self, image, text):
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import torch
font = ImageFont.truetype(str((here / "NotoSans-Regular.ttf").resolve()), 40)
img = Image.fromarray(image[0].cpu().numpy().__mul__(255.).astype(np.uint8))
ImageDraw.Draw(img).text((0,0), text, fill=(0,255,0), font=font)
return (torch.from_numpy(np.array(img)).unsqueeze(0) / 255.,)
class ExecuteAllControlNetPreprocessors:
@classmethod
def INPUT_TYPES(s):
return define_preprocessor_inputs(resolution=INPUT.RESOLUTION())
RETURN_TYPES = ("IMAGE",)
FUNCTION = "execute"
CATEGORY = "ControlNet Preprocessors"
def execute(self, image, resolution=512):
try:
from comfy_execution.graph_utils import GraphBuilder
except:
raise RuntimeError("ExecuteAllControlNetPreprocessor requries [Execution Model Inversion](https://github.com/comfyanonymous/ComfyUI/commit/5cfe38). Update ComfyUI/SwarmUI to get this feature")
graph = GraphBuilder()
curr_outputs = []
for preprocc in PREPROCESSOR_OPTIONS:
preprocc_node = graph.node("AIO_Preprocessor", preprocessor=preprocc, image=image, resolution=resolution)
hint_img = preprocc_node.out(0)
add_text_node = graph.node("ControlNetAuxSimpleAddText", image=hint_img, text=preprocc)
curr_outputs.append(add_text_node.out(0))
while len(curr_outputs) > 1:
_outputs = []
for i in range(0, len(curr_outputs), 2):
if i+1 < len(curr_outputs):
image_batch = graph.node("ImageBatch", image1=curr_outputs[i], image2=curr_outputs[i+1])
_outputs.append(image_batch.out(0))
else:
_outputs.append(curr_outputs[i])
curr_outputs = _outputs
return {
"result": (curr_outputs[0],),
"expand": graph.finalize(),
}
class ControlNetPreprocessorSelector:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"preprocessor": (PREPROCESSOR_OPTIONS,),
}
}
RETURN_TYPES = (PREPROCESSOR_OPTIONS,)
RETURN_NAMES = ("preprocessor",)
FUNCTION = "get_preprocessor"
CATEGORY = "ControlNet Preprocessors"
def get_preprocessor(self, preprocessor: str):
return (preprocessor,)
NODE_CLASS_MAPPINGS = {
**AUX_NODE_MAPPINGS,
"AIO_Preprocessor": AIO_Preprocessor,
"ControlNetPreprocessorSelector": ControlNetPreprocessorSelector,
**HIE_NODE_CLASS_MAPPINGS,
"ExecuteAllControlNetPreprocessors": ExecuteAllControlNetPreprocessors,
"ControlNetAuxSimpleAddText": ControlNetAuxSimpleAddText
}
NODE_DISPLAY_NAME_MAPPINGS = {
**AUX_DISPLAY_NAME_MAPPINGS,
"AIO_Preprocessor": "AIO Aux Preprocessor",
"ControlNetPreprocessorSelector": "Preprocessor Selector",
**HIE_NODE_DISPLAY_NAME_MAPPINGS,
"ExecuteAllControlNetPreprocessors": "Execute All ControlNet Preprocessors"
}
@@ -0,0 +1,26 @@
[
{
"local_file": "registry-1.1.5.json",
"url": "https://api.comfy.org/nodes/comfyui_controlnet_aux/versions/1.1.5",
"sha256": "694322dabd4cca87edfbf52369e3b9b8221ee6e26c0a5d2878dfd702ead373a7",
"retrieved_at": "2026-10-29T15:47:36.895566+00:00"
},
{
"local_file": "tags-response.json",
"url": "https://api.github.com/repos/Fannovel16/comfyui_controlnet_aux/tags?per_page=100",
"sha256": "2ba33ca0557f1bb5b7ba88d67f9d0093c7185a36ec51fe2b7bd9372d3e001d6d",
"retrieved_at": "2026-10-29T15:47:36.895566+00:00"
},
{
"local_file": "pyproject-history.json",
"url": "https://api.github.com/repos/Fannovel16/comfyui_controlnet_aux/commits?path=pyproject.toml&per_page=30",
"sha256": "e37aa0920b0b4ea1905867db5bb8505cc2210ee90ab6accbfe6552cf21edcf57",
"retrieved_at": "2026-10-29T15:47:36.895566+00:00"
},
{
"local_file": "version-commit.json",
"url": "https://api.github.com/repos/Fannovel16/comfyui_controlnet_aux/commits/e8b689a513c3e6b63edc44066560ca5919c0576e",
"sha256": "4e3c1379b671b6be3549deccfc4cbace9ac900e2c78bee025627a07e7c34fc43",
"retrieved_at": "2026-10-29T15:47:36.895566+00:00"
}
]
@@ -0,0 +1,55 @@
from ..utils import common_annotator_call, define_preprocessor_inputs, INPUT
import comfy.model_management as model_management
class Depth_Anything_Preprocessor:
@classmethod
def INPUT_TYPES(s):
return define_preprocessor_inputs(
ckpt_name=INPUT.COMBO(
["depth_anything_vitl14.pth", "depth_anything_vitb14.pth", "depth_anything_vits14.pth"]
),
resolution=INPUT.RESOLUTION()
)
RETURN_TYPES = ("IMAGE",)
FUNCTION = "execute"
CATEGORY = "ControlNet Preprocessors/Normal and Depth Estimators"
def execute(self, image, ckpt_name="depth_anything_vitl14.pth", resolution=512, **kwargs):
from custom_controlnet_aux.depth_anything import DepthAnythingDetector
model = DepthAnythingDetector.from_pretrained(filename=ckpt_name).to(model_management.get_torch_device())
out = common_annotator_call(model, image, resolution=resolution)
del model
return (out, )
class Zoe_Depth_Anything_Preprocessor:
@classmethod
def INPUT_TYPES(s):
return define_preprocessor_inputs(
environment=INPUT.COMBO(["indoor", "outdoor"]),
resolution=INPUT.RESOLUTION()
)
RETURN_TYPES = ("IMAGE",)
FUNCTION = "execute"
CATEGORY = "ControlNet Preprocessors/Normal and Depth Estimators"
def execute(self, image, environment="indoor", resolution=512, **kwargs):
from custom_controlnet_aux.zoe import ZoeDepthAnythingDetector
ckpt_name = "depth_anything_metric_depth_indoor.pt" if environment == "indoor" else "depth_anything_metric_depth_outdoor.pt"
model = ZoeDepthAnythingDetector.from_pretrained(filename=ckpt_name).to(model_management.get_torch_device())
out = common_annotator_call(model, image, resolution=resolution)
del model
return (out, )
NODE_CLASS_MAPPINGS = {
"DepthAnythingPreprocessor": Depth_Anything_Preprocessor,
"Zoe_DepthAnythingPreprocessor": Zoe_Depth_Anything_Preprocessor
}
NODE_DISPLAY_NAME_MAPPINGS = {
"DepthAnythingPreprocessor": "Depth Anything",
"Zoe_DepthAnythingPreprocessor": "Zoe Depth Anything"
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,14 @@
[project]
name = "comfyui_controlnet_aux"
description = "Plug-and-play ComfyUI node sets for making ControlNet hint images"
version = "1.1.5"
dependencies = ["torch", "importlib_metadata", "huggingface_hub", "scipy", "opencv-python>=4.7.0.72", "filelock", "numpy", "Pillow", "einops", "torchvision", "pyyaml", "scikit-image", "python-dateutil", "mediapipe", "fvcore", "yapf", "omegaconf", "ftfy", "addict", "yacs", "trimesh[easy]", "albumentations", "scikit-learn", "matplotlib"]
[project.urls]
Repository = "https://github.com/Fannovel16/comfyui_controlnet_aux"
[tool.comfy]
PublisherId = "fannovel16"
DisplayName = "comfyui_controlnet_aux"
Icon = ""
@@ -0,0 +1 @@
{"changelog":"","createdAt":"2026-04-13T10:46:14.267759Z","dependencies":["torch","importlib_metadata","huggingface_hub","scipy","opencv-python\u003e=4.7.0.72","filelock","numpy","Pillow","einops","torchvision","pyyaml","scikit-image","python-dateutil","mediapipe","fvcore","yapf","omegaconf","ftfy","addict","yacs","trimesh[easy]","albumentations","scikit-learn","matplotlib"],"deprecated":false,"downloadUrl":"https://cdn.comfy.org/fannovel16/comfyui_controlnet_aux/1.1.5/node.zip","id":"ddfa09d9-8aae-47c5-8426-9659a9f40703","node_id":"comfyui_controlnet_aux","status":"NodeVersionStatusActive","supported_accelerators":[],"supported_comfyui_frontend_version":"","supported_comfyui_version":"","supported_os":[],"tags":[],"version":"1.1.5"}
@@ -0,0 +1,119 @@
{
"authority": "public_reference_only_not_object_info",
"profile": "comfy-frontend-1.37.11-controlnet-aux-e8b689a-reference",
"declared_source_version": "1.1.5",
"reference_commit": "e8b689a513c3e6b63edc44066560ca5919c0576e",
"version_basis": "Upstream commit bumps pyproject version to 1.1.5. No git tags. Registry version API gives no commit; exact release artifact mapping remains unknown.",
"registry_artifact_commit_match": "unknown",
"target_version_match": "unknown",
"ue_serializer_match": "unknown",
"dynamic_combo_membership": "unknown_not_evaluated",
"sources": [
{
"id": "controlnet_aux:__init__.py",
"repository": "https://github.com/Fannovel16/comfyui_controlnet_aux",
"commit": "e8b689a513c3e6b63edc44066560ca5919c0576e",
"file": "__init__.py",
"lines": [
[
25,
65
],
[
67,
130
],
[
209,
224
]
],
"url": "https://github.com/Fannovel16/comfyui_controlnet_aux/blob/e8b689a513c3e6b63edc44066560ca5919c0576e/__init__.py",
"raw_url": "https://raw.githubusercontent.com/Fannovel16/comfyui_controlnet_aux/e8b689a513c3e6b63edc44066560ca5919c0576e/__init__.py",
"local_file": "__init__.py",
"sha256": "9a5786ef9569223acd235676c08297aff78b2de1e46ab69c96665222b8c2a4b4",
"retrieved_at": "2026-10-29T15:47:36.895566+00:00",
"target_version_match": "unknown"
},
{
"id": "controlnet_aux:utils.py",
"repository": "https://github.com/Fannovel16/comfyui_controlnet_aux",
"commit": "e8b689a513c3e6b63edc44066560ca5919c0576e",
"file": "utils.py",
"lines": [
[
60,
70
],
[
94,
120
]
],
"url": "https://github.com/Fannovel16/comfyui_controlnet_aux/blob/e8b689a513c3e6b63edc44066560ca5919c0576e/utils.py",
"raw_url": "https://raw.githubusercontent.com/Fannovel16/comfyui_controlnet_aux/e8b689a513c3e6b63edc44066560ca5919c0576e/utils.py",
"local_file": "utils.py",
"sha256": "7d217c7ba934556ee0ffa2ff8cd7a705873381b274db179623c11b829f71a375",
"retrieved_at": "2026-10-29T15:47:36.895566+00:00",
"target_version_match": "unknown"
},
{
"id": "controlnet_aux:node_wrappers/depth_anything.py",
"repository": "https://github.com/Fannovel16/comfyui_controlnet_aux",
"commit": "e8b689a513c3e6b63edc44066560ca5919c0576e",
"file": "node_wrappers/depth_anything.py",
"lines": [
[
4,
25
],
[
48,
55
]
],
"url": "https://github.com/Fannovel16/comfyui_controlnet_aux/blob/e8b689a513c3e6b63edc44066560ca5919c0576e/node_wrappers/depth_anything.py",
"raw_url": "https://raw.githubusercontent.com/Fannovel16/comfyui_controlnet_aux/e8b689a513c3e6b63edc44066560ca5919c0576e/node_wrappers/depth_anything.py",
"local_file": "node_wrappers__depth_anything.py",
"sha256": "f769ac091bd2725065f4d8cddc6fba9ae636d659d16c3d2a79b9c202b099ec17",
"retrieved_at": "2026-10-29T15:47:36.895566+00:00",
"target_version_match": "unknown"
},
{
"id": "controlnet_aux:pyproject.toml",
"repository": "https://github.com/Fannovel16/comfyui_controlnet_aux",
"commit": "e8b689a513c3e6b63edc44066560ca5919c0576e",
"file": "pyproject.toml",
"lines": [
[
1,
14
]
],
"url": "https://github.com/Fannovel16/comfyui_controlnet_aux/blob/e8b689a513c3e6b63edc44066560ca5919c0576e/pyproject.toml",
"raw_url": "https://raw.githubusercontent.com/Fannovel16/comfyui_controlnet_aux/e8b689a513c3e6b63edc44066560ca5919c0576e/pyproject.toml",
"local_file": "pyproject.toml",
"sha256": "e8ec5486ae4d2fd82192e56efbbcecaa80e27f217ba06c216b10fac6ef147894",
"retrieved_at": "2026-10-29T15:47:36.895566+00:00",
"target_version_match": "unknown"
},
{
"id": "controlnet_aux:LICENSE.txt",
"repository": "https://github.com/Fannovel16/comfyui_controlnet_aux",
"commit": "e8b689a513c3e6b63edc44066560ca5919c0576e",
"file": "LICENSE.txt",
"lines": [
[
1,
201
]
],
"url": "https://github.com/Fannovel16/comfyui_controlnet_aux/blob/e8b689a513c3e6b63edc44066560ca5919c0576e/LICENSE.txt",
"raw_url": "https://raw.githubusercontent.com/Fannovel16/comfyui_controlnet_aux/e8b689a513c3e6b63edc44066560ca5919c0576e/LICENSE.txt",
"local_file": "LICENSE.txt",
"sha256": "c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4",
"retrieved_at": "2026-10-29T15:47:36.895566+00:00",
"target_version_match": "unknown"
}
]
}
@@ -0,0 +1,3 @@
[
]
+250
View File
@@ -0,0 +1,250 @@
import torch
import numpy as np
import os
import cv2
import yaml
from pathlib import Path
from enum import Enum
from .log import log
import subprocess
import threading
import comfy
import tempfile
here = Path(__file__).parent.resolve()
config_path = Path(here, "config.yaml")
if os.path.exists(config_path):
config = yaml.load(open(config_path, "r"), Loader=yaml.FullLoader)
annotator_ckpts_path = str(Path(here, config["annotator_ckpts_path"]))
TEMP_DIR = config["custom_temp_path"]
USE_SYMLINKS = config["USE_SYMLINKS"]
ORT_PROVIDERS = config["EP_list"]
if USE_SYMLINKS is None or type(USE_SYMLINKS) != bool:
log.error("USE_SYMLINKS must be a boolean. Using False by default.")
USE_SYMLINKS = False
if TEMP_DIR is None:
TEMP_DIR = tempfile.gettempdir()
elif not os.path.isdir(TEMP_DIR):
try:
os.makedirs(TEMP_DIR)
except:
log.error("Failed to create custom temp directory. Using default.")
TEMP_DIR = tempfile.gettempdir()
if not os.path.isdir(annotator_ckpts_path):
try:
os.makedirs(annotator_ckpts_path)
except:
log.error("Failed to create config ckpts directory. Using default.")
annotator_ckpts_path = str(Path(here, "./ckpts"))
else:
annotator_ckpts_path = str(Path(here, "./ckpts"))
TEMP_DIR = tempfile.gettempdir()
USE_SYMLINKS = False
ORT_PROVIDERS = ["CUDAExecutionProvider", "DirectMLExecutionProvider", "OpenVINOExecutionProvider", "ROCMExecutionProvider", "CPUExecutionProvider", "CoreMLExecutionProvider"]
os.environ['AUX_ANNOTATOR_CKPTS_PATH'] = os.getenv('AUX_ANNOTATOR_CKPTS_PATH', annotator_ckpts_path)
os.environ['AUX_TEMP_DIR'] = os.getenv('AUX_TEMP_DIR', str(TEMP_DIR))
os.environ['AUX_USE_SYMLINKS'] = os.getenv('AUX_USE_SYMLINKS', str(USE_SYMLINKS))
os.environ['AUX_ORT_PROVIDERS'] = os.getenv('AUX_ORT_PROVIDERS', str(",".join(ORT_PROVIDERS)))
log.info(f"Using ckpts path: {annotator_ckpts_path}")
log.info(f"Using symlinks: {USE_SYMLINKS}")
log.info(f"Using ort providers: {ORT_PROVIDERS}")
# Sync with theoritical limit from Comfy base
# https://github.com/comfyanonymous/ComfyUI/blob/eecd69b53a896343775bcb02a4f8349e7442ffd1/nodes.py#L45
MAX_RESOLUTION=16384
def common_annotator_call(model, tensor_image, input_batch=False, show_pbar=True, **kwargs):
if "detect_resolution" in kwargs:
del kwargs["detect_resolution"] #Prevent weird case?
if "resolution" in kwargs:
detect_resolution = kwargs["resolution"] if type(kwargs["resolution"]) == int and kwargs["resolution"] >= 64 else 512
del kwargs["resolution"]
else:
detect_resolution = 512
if input_batch:
np_images = np.asarray(tensor_image * 255., dtype=np.uint8)
np_results = model(np_images, output_type="np", detect_resolution=detect_resolution, **kwargs)
return torch.from_numpy(np_results.astype(np.float32) / 255.0)
batch_size = tensor_image.shape[0]
if show_pbar:
pbar = comfy.utils.ProgressBar(batch_size)
out_tensor = None
for i, image in enumerate(tensor_image):
np_image = np.asarray(image.cpu() * 255., dtype=np.uint8)
np_result = model(np_image, output_type="np", detect_resolution=detect_resolution, **kwargs)
out = torch.from_numpy(np_result.astype(np.float32) / 255.0)
if out_tensor is None:
out_tensor = torch.zeros(batch_size, *out.shape, dtype=torch.float32)
out_tensor[i] = out
if show_pbar:
pbar.update(1)
return out_tensor
def define_preprocessor_inputs(**arguments):
return dict(
required=dict(image=INPUT.IMAGE()),
optional=arguments
)
class INPUT(Enum):
def IMAGE():
return ("IMAGE",)
def LATENT():
return ("LATENT",)
def MASK():
return ("MASK",)
def SEED(default=0):
return ("INT", dict(default=default, min=0, max=0xffffffffffffffff))
def RESOLUTION(default=512, min=64, max=MAX_RESOLUTION, step=64):
return ("INT", dict(default=default, min=min, max=max, step=step))
def INT(default=0, min=0, max=MAX_RESOLUTION, step=1):
return ("INT", dict(default=default, min=min, max=max, step=step))
def FLOAT(default=0, min=0, max=1, step=0.01):
return ("FLOAT", dict(default=default, min=min, max=max, step=step))
def STRING(default='', multiline=False):
return ("STRING", dict(default=default, multiline=multiline))
def COMBO(values, default=None):
return (values, dict(default=values[0] if default is None else default))
def BOOLEAN(default=True):
return ("BOOLEAN", dict(default=default))
class ResizeMode(Enum):
"""
Resize modes for ControlNet input images.
"""
RESIZE = "Just Resize"
INNER_FIT = "Crop and Resize"
OUTER_FIT = "Resize and Fill"
def int_value(self):
if self == ResizeMode.RESIZE:
return 0
elif self == ResizeMode.INNER_FIT:
return 1
elif self == ResizeMode.OUTER_FIT:
return 2
assert False, "NOTREACHED"
#https://github.com/Mikubill/sd-webui-controlnet/blob/e67e017731aad05796b9615dc6eadce911298ea1/internal_controlnet/external_code.py#L89
#Replaced logger with internal log
def pixel_perfect_resolution(
image: np.ndarray,
target_H: int,
target_W: int,
resize_mode: ResizeMode,
) -> int:
"""
Calculate the estimated resolution for resizing an image while preserving aspect ratio.
The function first calculates scaling factors for height and width of the image based on the target
height and width. Then, based on the chosen resize mode, it either takes the smaller or the larger
scaling factor to estimate the new resolution.
If the resize mode is OUTER_FIT, the function uses the smaller scaling factor, ensuring the whole image
fits within the target dimensions, potentially leaving some empty space.
If the resize mode is not OUTER_FIT, the function uses the larger scaling factor, ensuring the target
dimensions are fully filled, potentially cropping the image.
After calculating the estimated resolution, the function prints some debugging information.
Args:
image (np.ndarray): A 3D numpy array representing an image. The dimensions represent [height, width, channels].
target_H (int): The target height for the image.
target_W (int): The target width for the image.
resize_mode (ResizeMode): The mode for resizing.
Returns:
int: The estimated resolution after resizing.
"""
raw_H, raw_W, _ = image.shape
k0 = float(target_H) / float(raw_H)
k1 = float(target_W) / float(raw_W)
if resize_mode == ResizeMode.OUTER_FIT:
estimation = min(k0, k1) * float(min(raw_H, raw_W))
else:
estimation = max(k0, k1) * float(min(raw_H, raw_W))
log.debug(f"Pixel Perfect Computation:")
log.debug(f"resize_mode = {resize_mode}")
log.debug(f"raw_H = {raw_H}")
log.debug(f"raw_W = {raw_W}")
log.debug(f"target_H = {target_H}")
log.debug(f"target_W = {target_W}")
log.debug(f"estimation = {estimation}")
return int(np.round(estimation))
#https://github.com/Mikubill/sd-webui-controlnet/blob/e67e017731aad05796b9615dc6eadce911298ea1/scripts/controlnet.py#L404
def safe_numpy(x):
# A very safe method to make sure that Apple/Mac works
y = x
# below is very boring but do not change these. If you change these Apple or Mac may fail.
y = y.copy()
y = np.ascontiguousarray(y)
y = y.copy()
return y
#https://github.com/Mikubill/sd-webui-controlnet/blob/e67e017731aad05796b9615dc6eadce911298ea1/scripts/utils.py#L140
def get_unique_axis0(data):
arr = np.asanyarray(data)
idxs = np.lexsort(arr.T)
arr = arr[idxs]
unique_idxs = np.empty(len(arr), dtype=np.bool_)
unique_idxs[:1] = True
unique_idxs[1:] = np.any(arr[:-1, :] != arr[1:, :], axis=-1)
return arr[unique_idxs]
#Ref: https://github.com/ltdrdata/ComfyUI-Manager/blob/284e90dc8296a2e1e4f14b4b2d10fba2f52f0e53/__init__.py#L14
def handle_stream(stream, prefix):
for line in stream:
print(prefix, line, end="")
def run_script(cmd, cwd='.'):
process = subprocess.Popen(cmd, cwd=cwd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1)
stdout_thread = threading.Thread(target=handle_stream, args=(process.stdout, ""))
stderr_thread = threading.Thread(target=handle_stream, args=(process.stderr, "[!]"))
stdout_thread.start()
stderr_thread.start()
stdout_thread.join()
stderr_thread.join()
return process.wait()
def nms(x, t, s):
x = cv2.GaussianBlur(x.astype(np.float32), (0, 0), s)
f1 = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0]], dtype=np.uint8)
f2 = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]], dtype=np.uint8)
f3 = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.uint8)
f4 = np.array([[0, 0, 1], [0, 1, 0], [1, 0, 0]], dtype=np.uint8)
y = np.zeros_like(x)
for f in [f1, f2, f3, f4]:
np.putmask(y, cv2.dilate(x, kernel=f) == x, x)
z = np.zeros_like(y, dtype=np.uint8)
z[y > t] = 255
return z
File diff suppressed because one or more lines are too long