-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKnodes.py
129 lines (98 loc) · 3.71 KB
/
Knodes.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from server import PromptServer
from io import BytesIO
from PIL import Image
import numpy as np
import base64
import torch
import json
class ImageOutput:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"images": ("IMAGE", {"default": None, "forceInput": True}),
"Actions": ("STRING", {"default": None})}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("IMAGE",)
FUNCTION = "Proc"
OUTPUT_NODE = True
CATEGORY = "Knodes"
def Proc(self, images, Actions = ""):
outs = []
for single_image in images:
img = np.asarray(single_image * 255., dtype=np.uint8)
img = Image.fromarray(img)
buffered = BytesIO()
img.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode()
outs.append(img_str)
PromptServer.instance.send_sync("knodes", {"images": outs, "Actions": Actions})
return (images,)
class LoadImageBase64:
@classmethod
def INPUT_TYPES(s):
return {"required": {"image": ("STRING", {"multiline": False})}}
RETURN_TYPES = ("IMAGE", "MASK")
CATEGORY = "Knodes"
FUNCTION = "Proc"
def Proc(self, image):
imgdata = base64.b64decode(image)
img = Image.open(BytesIO(imgdata))
if "A" in img.getbands():
mask = np.array(img.getchannel("A")).astype(np.float32) / 255.0
mask = 1.0 - torch.from_numpy(mask)
else:
mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu")
img = img.convert("RGB")
img = np.array(img).astype(np.float32) / 255.0
img = torch.from_numpy(img)[None,]
return (img, mask)
class LoadImagesBase64:
@classmethod
def INPUT_TYPES(s):
return {"required": {"strings": ("STRING", {"multiline": False})}}
RETURN_TYPES = ("IMAGE","MASK")
CATEGORY = "Knodes"
FUNCTION = "Proc"
def Proc(self, strings):
number_of_images = int(strings[:4].lstrip('0'), 16)
print("Number Of Images: " + str(number_of_images))
strings = strings[4:]
images = list()
for i in range(number_of_images):
length = int(strings[:8].lstrip('0'), 16)
print("Image #" + str(i) + " Length: " + str(length))
strings = strings[8:]
single_image = strings[:length]
strings = strings[length:]
images.append(single_image)
tensors = list()
masks = list()
for single_image in images:
imgdata = base64.b64decode(single_image)
img = Image.open(BytesIO(imgdata))
if "A" in img.getbands():
mask = np.array(img.getchannel("A")).astype(np.float32) / 255.0
mask = 1.0 - torch.from_numpy(mask)
masks.append(mask)
else:
mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu")
masks.append(mask)
img = img.convert("RGB")
img = np.array(img).astype(np.float32) / 255.0
img = torch.from_numpy(img)[None,]
tensors.append(img)
return (torch.cat(tensors), torch.cat(masks))
NODE_CLASS_MAPPINGS = {
"Image(s) To Websocket (Base64)": ImageOutput,
"Load Image (Base64)": LoadImageBase64,
"Load Images (Base64)": LoadImagesBase64
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ImageOutput": "Image(s) To Websocket (Base64)",
"LoadImageBase64": "Load Image (Base64)",
"LoadImageBase64": "Load Images (Base64)"
}