ComfyUI/custom_nodes/clip_interrogator.py
inflamously 98155446bf feat: add image cropping
feat: remove custom js stuff since it just doesnt work well
feat: add cond debugging

feat: impls PoC refresh of custom_nodes + custom_node extensions

feat: ignore workflows folder
feat: add batch file to start application under windows
feat: integrate reload custom node into refresh
feat: update custom node ui
feat: impl node change event handling

!WIP!

feat: add CustomNodeData class for reuse
feat: remove all reloaded nodes for test purposes and save graph afterwards
!WIP!

feat: remove unused registeredNodes
feat: comment out graph removal

feat: comment on some functions for proper understanding and bookmarking (for now)

feat: comment node execution location
feat: add exception for IS_CHANGED issues
feat: extend example_node README

!WIP!

feat: custom test nodes for now

!WIP!

feat: avoid refresh spam

feat: add debug_cond custom_node with WIP ui

feat: add hint for validating output_ui data

feat: pass refresh button into combo function

feat: impl output ui error

feat: auto refresh nodes
fix: various minor issues

!WIP!

feat: barebone JS scripting in BE for ui templating

!WIP!

feat: impl interrogation with clip
feat: impl more debug samplers
feat: change requirements.txt for transformers

fix: __init__.py issues when importing custom_nodes

feat: temp ignore 3rdparty code

feat: add custom_nodes debug_latent and image_fx
2023-09-15 00:16:44 +02:00

58 lines
1.7 KiB
Python

import os
import random
import sys
import hashlib
import base64
from clip_interrogator import Interrogator, Config
from torch import Tensor
import torchvision.transforms as T
from PIL import Image
class ClipInterrogator:
MODEL_NAME = ["ViT-L-14/openai"]
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"clip": ("CLIP",),
"model_name": (ClipInterrogator.MODEL_NAME,),
}
}
RETURN_TYPES = ("CONDITIONING",)
FUNCTION = "clip_interrogate"
OUTPUT_NODE = True
CATEGORY = "inflamously"
VALUE = ""
@classmethod
def IS_CHANGED(s, image, clip, model_name):
# TODO: Why does this not cache immidiately
return hashlib.md5(str(bytearray(image.numpy())).encode("utf-8")).hexdigest()
def clip_interrogate(self, image, clip, model_name):
img_tensor = image[0]
# define a transform to convert a tensor to PIL image
transform = T.ToPILImage()
h, w, c = img_tensor.size()
# print(h,w,c)
# convert the tensor to PIL image using above transform
img = transform(image[0].reshape(c, h, w)) # Reshape since Tensor is using Height, Width, Color but Image needs C, H, W
config = Config(clip_model_name=model_name)
config.apply_low_vram_defaults()
ci = Interrogator(config)
ClipInterrogator.VALUE = ci.interrogate(img)
print("Image:", ClipInterrogator.VALUE)
tokens = clip.tokenize(ClipInterrogator.VALUE)
cond, pooled = clip.encode_from_tokens(tokens, return_pooled=True)
return ([[cond, {"pooled_output": pooled}]], )
NODE_CLASS_MAPPINGS = {
"ClipInterrogator": ClipInterrogator
}