-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnodes.py
95 lines (72 loc) · 2.43 KB
/
nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import base64
from PIL import Image
import torch
import numpy as np
import io
from .attention_couple import (
AttentionCouple,
AttentionCoupleRegion,
AttentionCoupleRegions,
)
class Base64ImageInput:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"base64_image": ("STRING", {"multiline": False, "default": ""}),
},
}
RETURN_TYPES = ("IMAGE",)
FUNCTION = "process_input"
CATEGORY = "A8R8"
def process_input(self, base64_image):
if base64_image:
image_bytes = base64.b64decode(base64_image)
# Open the image from bytes
image = Image.open(io.BytesIO(image_bytes))
image = image.convert("RGB")
image = np.array(image).astype(np.float32) / 255.0
image = torch.from_numpy(image)[None,]
return (image,)
class Base64ImageOutput:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"images": ("IMAGE",),
},
}
RETURN_TYPES = ()
FUNCTION = "process_output"
OUTPUT_NODE = True
CATEGORY = "A8R8"
def process_image(self, image):
i = 255.0 * image.cpu().numpy()
img = Image.fromarray(np.clip(i, 0, 255).astype(np.uint8))
buffered = io.BytesIO()
img.save(buffered, optimize=False, format="png", compress_level=4)
base64_image = base64.b64encode(buffered.getvalue()).decode()
return base64_image
def process_output(self, images: list[torch.Tensor]):
return {"ui": {"images": [self.process_image(image) for image in images]}}
# A dictionary that contains all nodes you want to export with their names
# NOTE: names should be globally unique
NODE_CLASS_MAPPINGS = {
"Base64ImageInput": Base64ImageInput,
"Base64ImageOutput": Base64ImageOutput,
"AttentionCouple": AttentionCouple,
"AttentionCoupleRegion": AttentionCoupleRegion,
"AttentionCoupleRegions": AttentionCoupleRegions,
}
# A dictionary that contains the friendly/humanly readable titles for the nodes
NODE_DISPLAY_NAME_MAPPINGS = {
"Base64ImageInput": "Base64Image Input Node",
"Base64ImageOutput": "Base64Image Output Node",
"AttentionCouple": "Attention Couple",
"AttentionCoupleRegion": "Attention Couple Region",
"AttentionCoupleRegions": "Attention Couple Regions",
}