-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathimageInferenceInputs.py
More file actions
160 lines (136 loc) · 6.6 KB
/
Copy pathimageInferenceInputs.py
File metadata and controls
160 lines (136 loc) · 6.6 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from .utils import runwareUtils as rwUtils
class imageInferenceInputs:
"""Image Inference Inputs node for configuring image generation inputs"""
MAX_REFERENCE_IMAGES = 14
MAX_SUPER_RESOLUTION_REFERENCE_IMAGES = 5
@classmethod
def INPUT_TYPES(cls):
optionalInputs = {
"image": ("IMAGE", {
"tooltip": "Specifies an image input for the inference process."
}),
"mask": ("IMAGE", {
"tooltip": "Optional mask image for the inference process."
}),
"seedImage": ("IMAGE", {
"tooltip": "Optional seed image for the inference process"
}),
}
for i in range(1, cls.MAX_REFERENCE_IMAGES + 1):
ordinal = rwUtils.getOrdinal(i)
optionalInputs[f"Reference Image {i}"] = ("IMAGE", {
"tooltip": f"Specifies {ordinal.capitalize()} Reference Image for the inputs. These reference images help guide the image generation process."
})
optionalInputs[f"Reference Tag {i}"] = ("STRING", {
"tooltip": f"Optional tag describing {ordinal.capitalize()} Reference Image. Leave empty to omit.",
"default": "",
})
optionalInputs[f"Reference Role {i}"] = ("STRING", {
"tooltip": f"Optional role for {ordinal} reference image (e.g. person, garment). Leave empty to omit.",
"default": "",
})
optionalInputs[f"Reference Type {i}"] = ("STRING", {
"tooltip": f"Optional type for {ordinal} reference, e.g. 'sketch' for illustrative style models. Leave empty to omit.",
"default": "",
})
optionalInputs[f"Reference Strength {i}"] = ("FLOAT", {
"tooltip": f"Strength (0-1) for sketch reference. Only used when Reference Type is 'sketch'.",
"default": 0.0,
"min": 0.0,
"max": 1.0,
"step": 0.1,
})
for i in range(1, cls.MAX_SUPER_RESOLUTION_REFERENCE_IMAGES + 1):
ordinal = rwUtils.getOrdinal(i)
optionalInputs[f"Super Resolution Reference Image {i}"] = ("IMAGE", {
"tooltip": f"Specifies {ordinal.capitalize()} Super Resolution Reference Image for the inputs.",
})
optionalInputs["Fonts"] = ("RUNWAREIMAGEINFERENCEFONTS", {
"tooltip": "Connect Runware Image Inference Inputs Fonts for inputs.fonts (up to 2 font URL + text references).",
})
return {
"required": {},
"optional": optionalInputs
}
DESCRIPTION = (
"Configure custom inputs for Runware Image Inference, including image/mask/seedImage, reference images "
"(with optional tag, role, type e.g. 'sketch' for illustrative style models, and strength 0-1 for sketch), "
"super resolution references, and font references (inputs.fonts)."
)
FUNCTION = "createInputs"
RETURN_TYPES = ("RUNWAREIMAGEINFERENCEINPUTS",)
RETURN_NAMES = ("Inference Inputs",)
CATEGORY = "Runware"
def createInputs(self, **kwargs):
"""Create image inference inputs from provided parameters"""
image = kwargs.get("image", None)
mask = kwargs.get("mask", None)
seedImage = kwargs.get("seedImage", None)
inputs = {}
if image is not None:
inputs["image"] = rwUtils.convertTensor2IMG(image)
if mask is not None:
inputs["mask"] = rwUtils.convertTensor2IMG(mask)
if seedImage is not None:
inputs["seedImage"] = rwUtils.convertTensor2IMG(seedImage)
references = self._collectReferences(kwargs)
if len(references) > 0:
inputs["referenceImages"] = references
super_resolution_refs = self._collectSuperResolutionReferences(kwargs)
if len(super_resolution_refs) > 0:
inputs["superResolutionReferenceImages"] = super_resolution_refs
fonts = kwargs.get("Fonts", None)
if fonts is not None and isinstance(fonts, list) and len(fonts) > 0:
inputs["fonts"] = fonts
return (inputs,)
def _collectReferences(self, kwargs):
"""Collect and convert reference images to list. Each entry may include type (e.g. 'sketch') and strength (0-1, only for sketch)."""
reference_slots = []
for i in range(1, self.MAX_REFERENCE_IMAGES + 1):
image = kwargs.get(f"Reference Image {i}", None)
tag = kwargs.get(f"Reference Tag {i}", "")
role = kwargs.get(f"Reference Role {i}", "")
ref_type = kwargs.get(f"Reference Type {i}", "")
strength = kwargs.get(f"Reference Strength {i}", 0.0)
if image is not None:
reference_slots.append((image, tag, role, ref_type, strength))
if not reference_slots:
return []
has_tags = any(
isinstance(tag, str) and tag.strip() != ""
for _, tag, _, _, _ in reference_slots
)
has_roles = any(
isinstance(role, str) and role.strip() != ""
for _, _, role, _, _ in reference_slots
)
has_type = any(
isinstance(rt, str) and rt.strip() != ""
for _, _, _, rt, _ in reference_slots
)
if not has_tags and not has_roles and not has_type:
return [
rwUtils.convertTensor2IMG(image)
for image, _, _, _, _ in reference_slots
]
references = []
for image, tag, role, ref_type, strength in reference_slots:
entry = {"image": rwUtils.convertTensor2IMG(image)}
if isinstance(tag, str) and tag.strip() != "":
entry["tag"] = tag.strip()
if isinstance(role, str) and role.strip() != "":
entry["role"] = role.strip()
if isinstance(ref_type, str) and ref_type.strip() != "":
entry["type"] = ref_type.strip()
if ref_type.strip().lower() == "sketch":
entry["strength"] = max(0.0, min(1.0, float(strength)))
references.append(entry)
return references
def _collectSuperResolutionReferences(self, kwargs):
"""Collect and convert super resolution reference images to list (same pattern as referenceImages)"""
images = []
for i in range(1, self.MAX_SUPER_RESOLUTION_REFERENCE_IMAGES + 1):
image = kwargs.get(f"Super Resolution Reference Image {i}", None)
if image is not None:
images.append(rwUtils.convertTensor2IMG(image))
return images