Skip to content

Commit

Permalink
Fix #430, #431 and add new logo
Browse files Browse the repository at this point in the history
  • Loading branch information
Fannovel16 committed Aug 16, 2024
1 parent ebdee60 commit 835e85e
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 12 deletions.
Binary file added NotoSans-Regular.ttf
Binary file not shown.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
# ComfyUI's ControlNet Auxiliary Preprocessors
![](./examples/example_mesh_graphormer.png)
Plug-and-play [ComfyUI](https://github.com/comfyanonymous/ComfyUI) node sets for making [ControlNet](https://github.com/lllyasviel/ControlNet/) hint images

"anime style, a protest in the street, cyberpunk city, a woman with pink hair and golden eyes (looking at the viewer) is holding a sign with the text "ComfyUI ControlNet Aux" in bold, neon pink" on Flux.1 Dev
[Original image with workflow](./examples/ExecuteAll.png)
![](./examples/ExecuteAll1.jpg)
![](./examples/ExecuteAll2.jpg)


The code is copy-pasted from the respective folders in https://github.com/lllyasviel/ControlNet/tree/main/annotator and connected to [the 🤗 Hub](https://huggingface.co/lllyasviel/Annotators).

All credit & copyright goes to https://github.com/lllyasviel.
Expand Down
68 changes: 64 additions & 4 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import sys, os
from .utils import here, define_preprocessor_inputs, INPUT
from pathlib import Path
import threading
import traceback
import warnings
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
Expand Down Expand Up @@ -62,8 +60,10 @@ def load_nodes():

AUX_NODE_MAPPINGS, AUX_DISPLAY_NAME_MAPPINGS = load_nodes()

AIO_NOT_SUPPORTED = ["InpaintPreprocessor"]
#For nodes not mapping image to image
#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())
Expand Down Expand Up @@ -119,6 +119,63 @@ def execute(self, preprocessor, image, resolution=512):

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
Expand All @@ -144,11 +201,14 @@ def get_preprocessor(self, preprocessor: str):
"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"
}
Binary file added examples/ExecuteAll.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/ExecuteAll1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/ExecuteAll2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions node_wrappers/depth_anything.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class Depth_Anything_Preprocessor:
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()
)
["depth_anything_vitl14.pth", "depth_anything_vitb14.pth", "depth_anything_vits14.pth"]
),
resolution=INPUT.RESOLUTION()
)

RETURN_TYPES = ("IMAGE",)
Expand Down
2 changes: 1 addition & 1 deletion node_wrappers/recolor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ImageIntensityDetector:
@classmethod
def INPUT_TYPES(s):
#https://github.com/Mikubill/sd-webui-controlnet/blob/416c345072c9c2066101e225964e3986abe6945e/scripts/processor.py#L1229
define_preprocessor_inputs(
return define_preprocessor_inputs(
gamma_correction=INPUT.FLOAT(default=1.0, min=0.1, max=2.0),
resolution=INPUT.RESOLUTION()
)
Expand Down
2 changes: 1 addition & 1 deletion node_wrappers/unimatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def INPUT_TYPES(s):
CATEGORY = "ControlNet Preprocessors/Optical Flow"

def estimate(self, image, ckpt_name, backward_flow=False, bidirectional_flow=False):
assert len(image) > 1, "[Unimatch] Requiring as least two frames as a optical flow estimator. Only use this node on video input."
assert len(image) > 1, "[Unimatch] Requiring as least two frames as an optical flow estimator. Only use this node on video input."
from controlnet_aux.unimatch import UnimatchDetector
tensor_images = image
model = UnimatchDetector.from_pretrained(filename=ckpt_name).to(model_management.get_torch_device())
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "comfyui_controlnet_aux"
description = "Plug-and-play ComfyUI node sets for making ControlNet hint images"
version = "1.0.4-alpha.1"
version = "1.0.4-alpha.2"
license = "LICENSE"
dependencies = ["torch", "importlib_metadata", "huggingface_hub", "scipy", "opencv-python>=4.7.0.72", "filelock", "numpy", "Pillow", "einops", "torchvision", "pyyaml", "scikit-image", "python-dateutil", "mediapipe", "svglib", "fvcore", "yapf", "omegaconf", "ftfy", "addict", "yacs", "trimesh[easy]", "albumentations", "scikit-learn"]

Expand Down
2 changes: 1 addition & 1 deletion src/controlnet_aux/anime_face_segment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def from_pretrained(cls, pretrained_model_or_path=BDS_MODEL_NAME, filename="UNet
seg_model_path = custom_hf_download("skytnt/anime-seg", seg_filename)

model = UNet()
ckpt = torch.load(model_path)
ckpt = torch.load(model_path, map_location="cpu")
model.load_state_dict(ckpt)
model.eval()

Expand Down

0 comments on commit 835e85e

Please sign in to comment.