-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnodes.py
734 lines (573 loc) · 22.9 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
import os
import json
tag_category1 = []
tag_category2 = []
def get_tag_category(version=1):
global tag_category1, tag_category2
if version == 1:
if not tag_category1:
tag_category1 = json.load(open(os.path.join(os.path.dirname(os.path.realpath(__file__)),"tag_category.json")))
return tag_category1
else:
if not tag_category2:
tag_category2 = json.load(open(os.path.join(os.path.dirname(os.path.realpath(__file__)),"tag_category_v2.json")))
return tag_category2
class TagData:
def __init__(self, tag, weight):
self.tag = tag
self.weight = weight
self.format = tag.lower().strip().replace(' ', '_')
def __str__(self):
return self.format
def __repr__(self):
return self.format
def __eq__(self, other):
if isinstance(other, TagData):
return self.format == other.format
return False
def __hash__(self):
return hash(self.format)
def text(self, format=False, underscore=False):
tag_text = self.tag
if format:
tag_text = self.format
if underscore:
tag_text = tag_text.replace(' ', '_')
if self.weight != 1.0:
return f"({tag_text}:{self.weight})"
return tag_text
def parse_tags(tag_string) -> list:
def clean_tag(tag):
# Remove any leading/trailing whitespace and parentheses
tag = tag.strip()
while tag.startswith(('(', ')')) or tag.endswith(('(', ')')):
tag = tag.strip('()')
return tag.strip()
def get_weight_and_tags(group):
group = clean_tag(group)
if ':' in group:
tags_part, weight_part = group.split(':', 1)
try:
weight = float(weight_part)
except ValueError:
weight = 1.0
tags = [t.strip() for t in tags_part.split(',')]
else:
tags = [t.strip() for t in group.split(',')]
weight = 1.0
return tags, weight
result = []
paren_count = 0
# First pass: split into proper groups
groups = []
current = ''
for char in tag_string:
if char == '(':
paren_count += 1
elif char == ')':
paren_count -= 1
current += char
if paren_count == 0 and char == ',':
if current.strip(',').strip():
groups.append(current.strip(',').strip())
current = ''
if current.strip(',').strip():
groups.append(current.strip(',').strip())
# Second pass: process each group
for group in groups:
group = group.strip()
if not group:
continue
# Count the number of opening parentheses at the start
opening_count = 0
for char in group:
if char == '(':
opening_count += 1
else:
break
# Count the number of closing parentheses at the end
closing_count = 0
for char in reversed(group):
if char == ')':
closing_count += 1
else:
break
# Get the actual parentheses pairs count (use the minimum to ensure matching pairs)
paren_pairs = min(opening_count, closing_count)
tags, custom_weight = get_weight_and_tags(group)
# Set weight based on parentheses pairs
if custom_weight != 1.0:
weight = custom_weight
else:
if paren_pairs == 0:
weight = 1.0
else:
weight = 1.0 + (paren_pairs * 0.1)
# Add each tag with its weight
for tag in tags:
tag = clean_tag(tag)
if tag:
result.append(TagData(tag, weight))
return result
def remove_duplicates(lst):
return list(dict.fromkeys(lst))
def tagdata_to_string(tags:list[TagData], underscore=False) -> str:
return ", ".join([tag.text(underscore=underscore) for tag in tags])
class TagIf:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"tags": ("STRING", ),
"find": ("STRING", {"default": ""}),
"output1": ("STRING", {"default": ""}),
},
"optional": {
"output2": ("STRING", {"default": ""}),
"output3": ("STRING", {"default": ""}),
}
}
RETURN_TYPES = ("STRING","STRING","STRING",)
RETURN_NAMES = ("output1","output2","output3",)
FUNCTION = "tag"
CATEGORY = "string"
OUTPUT_NODE = True
def tag(self, tags:str, find:str, output1:str, output2:str=None, output3:str=None):
tags = parse_tags(tags)
find = parse_tags(find)
if all(tag in tags for tag in find):
return (output1, output2, output3)
else:
return ("", "", "")
class TagSwitcher:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"input_tags": ("STRING",),
"default_image": ("IMAGE",),
"tags1": ("STRING", {"default": ""}),
"image1": ("IMAGE", {"default": ""}),
"any1": ("BOOLEAN", {"default": True}),
},
"optional": {
"tags2": ("STRING", {"default": ""}),
"image2": ("IMAGE", {"default": ""}),
"any2": ("BOOLEAN", {"default": True}),
"tags3": ("STRING", {"default": ""}),
"image3": ("IMAGE", {"default": ""}),
"any3": ("BOOLEAN", {"default": True}),
"tags4": ("STRING", {"default": ""}),
"image4": ("IMAGE", {"default": ""}),
"any4": ("BOOLEAN", {"default": True}),
}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "tag"
CATEGORY = "image"
OUTPUT_NODE = True
def tag(self, input_tags="", default_image=None, tags1="", image1=None, any1=True, tags2="", image2=None, any2=True, tags3="", image3=None, any3=True, tags4="", image4=None, any4=True):
input_tags = parse_tags(input_tags)
target_tags = []
tags1 = set(parse_tags(tags1))
target_tags.append((tags1, image1, any1))
tags2 = set(parse_tags(tags2))
target_tags.append((tags2, image2, any2))
tags3 = set(parse_tags(tags3))
target_tags.append((tags3, image3, any3))
tags4 = set(parse_tags(tags4))
target_tags.append((tags4, image4, any4))
for tags, image, any_flag in target_tags:
if any_flag:
if any(tag in tags for tag in input_tags):
return (image,)
else:
if all(tag in input_tags for tag in tags):
return (image,)
return (default_image,)
class TagMerger:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"tags1": ("STRING",),
"tags2": ("STRING",),
"under_score": ("BOOLEAN", {"default": True}),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("result",)
FUNCTION = "tag"
CATEGORY = "text"
OUTPUT_NODE = True
def tag(self, tags1:str, tags2:str, under_score=True):
taglist1 = parse_tags(tags1)
taglist2 = parse_tags(tags2)
tags = remove_duplicates(taglist1 + taglist2)
return (tagdata_to_string(tags, underscore=under_score),)
class TagRemover:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"tags": ("STRING",),
"exclude_tags": ("STRING", {"default": ""}),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("result",)
FUNCTION = "tag"
CATEGORY = "text"
OUTPUT_NODE = True
def tag(self, tags:str, exclude_tags:str=""):
tag_data = parse_tags(tags)
exclude_tag_data = parse_tags(exclude_tags)
uniq_tags = [tag for tag in tag_data if tag not in exclude_tag_data]
return (tagdata_to_string(uniq_tags) ,)
class TagReplace:
# TODO: use TagData class
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"tags": ("STRING", ),
"replace_tags": ("STRING", {"default": ""}),
"match": ("FLOAT", {"default": 0.3}),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("result",)
FUNCTION = "tag"
CATEGORY = "text"
OUTPUT_NODE = True
def _get_categories(self, tag: str) -> set:
"""タグのカテゴリーを取得する"""
tag_category = get_tag_category(2)
return set(tag_category.get(tag, []))
def _category_match_percentage(self, categories1: set, categories2: set) -> float:
"""2つのカテゴリセット間の一致度(%)を計算する"""
if not categories1 or not categories2:
return 0
intersection = categories1.intersection(categories2)
union = categories1.union(categories2)
return len(intersection) / len(union)
def tag(self, tags:str, replace_tags:str="", match:float=0.3):
tags = [tag.strip() for tag in tags.replace("\n",",").split(",")]
tags_normalized = [tag.replace(" ", "_").lower().strip() for tag in tags]
replace_tags = [tag.strip() for tag in replace_tags.replace("\n",",").split(",")]
replace_tags_normalized = [tag.replace(" ", "_").lower().strip() for tag in replace_tags]
replace_tags_used = {tag:False for tag in replace_tags_normalized}
result = []
for i, tag in enumerate(tags_normalized):
tag_categories = self._get_categories(tag)
best_match_tag = None
best_match_tag_id = None
best_match_percentage = 0
for k, replace_tag in enumerate(replace_tags_normalized):
replace_categories = self._get_categories(replace_tag)
match_percentage = self._category_match_percentage(tag_categories, replace_categories)
if match_percentage and match_percentage > best_match_percentage:
best_match_percentage = match_percentage
best_match_tag = replace_tag
replace_tags_used[replace_tag] = True
best_match_tag_id = k
if best_match_tag and best_match_percentage >= match:
result.append(replace_tags[best_match_tag_id])
else:
result.append(tags[i])
# replace_tags の中から、tags に存在しないタグを追加
extra_tags = [replace_tag for replace_tag, used in replace_tags_used.items() if not used]
result.extend(extra_tags)
return (", ".join(result),)
class TagSelector:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"tags": ("STRING", ),
"categorys": ("STRING", {"default": "*"}),
"exclude" : ("BOOLEAN", {"default": False}),
"whitelist_only": ("BOOLEAN", {"default": False}),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("result",)
FUNCTION = "tag"
CATEGORY = "text"
OUTPUT_NODE = True
def tag(self, tags:str, categorys:str, exclude:bool=False, whitelist_only:bool=False):
tag_list = parse_tags(tags)
tag_category = get_tag_category(2)
target_category = [category.strip() for category in categorys.replace("\n",",").replace(".",",").split(",")]
result = []
for i, tag in enumerate(tag_list):
tag_text = tag.format
if tag_text in tag_category:
if '*' == categorys:
result.append(tag)
continue
category_list = tag_category.get(tag_text, [])
for category in category_list:
if exclude:
if category not in target_category:
result.append(tag)
break
else:
if category in target_category:
result.append(tag)
break
else:
if whitelist_only:
continue
result.append(tag)
return (tagdata_to_string(result),)
class TagComparator:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"tags1": ("STRING", ),
"tags2": ("STRING", ),
},
}
RETURN_TYPES = ("STRING", "STRING", "STRING",)
RETURN_NAMES = ("tags1_unique", "tags2_unique", "common_tags",)
FUNCTION = "tag"
CATEGORY = "text"
OUTPUT_NODE = True
def tag(self, tags1:str, tags2:str):
tag_list1 = parse_tags(tags1)
tag_list2 = parse_tags(tags2)
tags1_unique = [tag for tag in tag_list1 if tag not in tag_list2]
tags2_unique = [tag for tag in tag_list2 if tag not in tag_list1]
common_tags = [tag for tag in tag_list1 if tag in tag_list2]
return (tagdata_to_string(tags1_unique), tagdata_to_string(tags2_unique), tagdata_to_string(common_tags))
class TagFilter:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"tags": ("STRING", ),
"pose": ("BOOLEAN", {"default": True}),
"gesture": ("BOOLEAN", {"default": True}),
"action": ("BOOLEAN", {"default": True}),
"emotion": ("BOOLEAN", {"default": True}),
"expression": ("BOOLEAN", {"default": True}),
"camera": ("BOOLEAN", {"default": True}),
"angle": ("BOOLEAN", {"default": True}),
"sensitive": ("BOOLEAN", {"default": True}),
"liquid": ("BOOLEAN", {"default": True}),
"include_categories": ("STRING", {"default": ""}),
"exclude_categories": ("STRING", {"default": ""}),
},
}
RETURN_TYPES = ("STRING",)
RETURN_NAMES = ("result",)
FUNCTION = "tag"
CATEGORY = "text"
OUTPUT_NODE = True
def tag(self, tags, pose=True, gesture=True, action=True, emotion=True, expression=True, camera=True, angle=True, sensitive=True, liquid=True, include_categories="", exclude_categories=""):
targets = []
exclude_targets = []
if pose:
targets.append("pose")
if gesture:
targets.append("gesture")
if action:
targets.append("action")
if emotion:
targets.append("emotion")
if expression:
targets.append("expression")
if camera:
targets.append("camera")
if angle:
targets.append("angle")
if sensitive:
targets.append("sensitive")
if liquid:
targets.append("liquid")
include_categories = include_categories.strip()
if include_categories:
targets += [category.strip() for category in include_categories.replace("\n",",").split(",")]
if exclude_categories:
exclude_targets = [category.strip() for category in exclude_categories.replace("\n",",").split(",")]
targets = [target for target in targets if target not in exclude_targets]
tag_list = parse_tags(tags)
result = []
tag_category = get_tag_category(2)
for i, tag in enumerate(tag_list):
tag_text = tag.format
if tag_text in tag_category:
category_list = tag_category.get(tag_text, [])
if not category_list:
continue
for category in category_list:
if category in exclude_targets:
# not include this tag
break
else:
if '*' == include_categories:
# include all tags
result.append(tag)
else:
for category in category_list:
if category in targets:
# include this tag
result.append(tag)
break
return (tagdata_to_string(result),)
NODE_CLASS_MAPPINGS = {
"TagSwitcher": TagSwitcher,
"TagMerger": TagMerger,
"TagFilter": TagFilter,
"TagReplace": TagReplace,
"TagRemover": TagRemover,
"TagIf": TagIf,
"TagSelector": TagSelector,
"TagComparator": TagComparator
}
NODE_DISPLAY_NAME_MAPPINGS = {
"TagSwitcher": "TagSwitcher",
"TagMerger": "TagMerger",
"TagFilter": "TagFilter",
"TagReplace": "TagReplace",
"TagRemover": "TagRemover",
"TagIf": "TagIf",
"TagSelector": "TagSelector",
"TagComparator": "TagComparator"
}
def simple_test():
sample_tags = "school_uniform, (long hair, v:1.2), (sitting:1.5), (standing:0.5), attack, ((1girl)), original_tag"
tf = TagFilter()
result = tf.tag(sample_tags, pose=False, gesture=False, action=False, emotion=False, expression=False, camera=False, angle=False, sensitive=False, liquid=False,
include_categories="*", exclude_categories="pose")
print("@@@ all in, pose out", result)
if 'school_uniform, (long hair:1.2), (1girl:1.2)' != result[0]:
raise Exception("Test failed")
result = tf.tag(sample_tags, pose=False, gesture=False, action=False, emotion=False, expression=False, camera=False, angle=False, sensitive=False, liquid=False,
include_categories="clothing")
print("@@@ clothing", result)
if 'school_uniform' != result[0]:
raise Exception("Test failed")
result = tf.tag(sample_tags, pose=False, gesture=False, action=True, emotion=False, expression=False, camera=False, angle=False, sensitive=False, liquid=False)
print("@@@ action", result)
if 'attack' != result[0]:
raise Exception("Test failed")
result = tf.tag(sample_tags, pose=True, gesture=True, action=True, emotion=True, expression=False, camera=True, angle=False, sensitive=False, liquid=False)
print("@@@ pose", result)
if '(v:1.2), (sitting:1.5), (standing:0.5), attack' != result[0]:
raise Exception("Test failed")
ti = TagIf()
result = ti.tag(tags=sample_tags,
find="sitting",
output1="sitting found")
print("@@@ tagif sitting", result)
if 'sitting found' != result[0]:
raise Exception("Test failed")
result = ti.tag(tags=sample_tags,
find="school_uniform1",
output1="found")
print("@@@ tagif uniform not found", result)
if '' != result[0]:
raise Exception("Test failed")
ts = TagSwitcher()
result = ts.tag(input_tags=sample_tags,
default_image="default",
tags1="1girl,2girls",
image1="image1",
any1=True)
print("@@@ tagswitcher found1", result)
if 'image1' != result[0]:
raise Exception("Test failed")
result = ts.tag(input_tags=sample_tags,
default_image="default",
tags1="1girl,2girls",
image1="image1",
any1=False)
print("@@@ tagswitcher found2", result)
if 'default' != result[0]:
raise Exception("Test failed")
result = ts.tag(input_tags=sample_tags,
default_image="default",
tags1="2girls",
image1="image1",
any1=True)
print("@@@ tagswitcher not found", result)
if 'default' != result[0]:
raise Exception("Test failed")
tm = TagMerger()
result = tm.tag(tags1=sample_tags,
tags2="(1boy:1.5), (1girl:1.5), ((sitting)), (lying:0.5), long hair",
under_score=True)
print("@@@ tagmerger1", result)
if 'school_uniform, (long_hair:1.2), (v:1.2), (sitting:1.5), (standing:0.5), attack, (1girl:1.2), original_tag, (1boy:1.5), (lying:0.5)' != result[0]:
raise Exception("Test failed")
result = tm.tag(tags1=sample_tags,
tags2="(1boy:1.5), (1girl:1.5), ((sitting)), (lying:0.5), long_hair",
under_score=False)
print("@@@ tagmerger2 underscore", result)
if 'school_uniform, (long hair:1.2), (v:1.2), (sitting:1.5), (standing:0.5), attack, (1girl:1.2), original_tag, (1boy:1.5), (lying:0.5)' != result[0]:
raise Exception("Test failed")
ts = TagSelector()
result = ts.tag(tags=sample_tags,
categorys="pose",
whitelist_only=True)
print("@@@ tagselector1 whitelist", result)
if '(v:1.2), (sitting:1.5), (standing:0.5), attack' != result[0]:
raise Exception("Test failed")
result = ts.tag(tags=sample_tags,
categorys="pose",
whitelist_only=False)
print("@@@ tagselector2 no whitelist", result)
if '(v:1.2), (sitting:1.5), (standing:0.5), attack, original_tag' != result[0]:
raise Exception("Test failed")
result = ts.tag(tags=sample_tags,
categorys="pose",
exclude=True,
whitelist_only=True)
print("@@@ tagselector3 whitelist exclude", result)
if 'school_uniform, (long hair:1.2), (v:1.2), (sitting:1.5), (standing:0.5), attack, (1girl:1.2)' != result[0]:
raise Exception("Test failed")
result = ts.tag(tags=sample_tags,
categorys="pose",
exclude=True,
whitelist_only=False)
print("@@@ tagselector3 no whitelist exclude", result)
if 'school_uniform, (long hair:1.2), (v:1.2), (sitting:1.5), (standing:0.5), attack, (1girl:1.2), original_tag' != result[0]:
raise Exception("Test failed")
tc = TagComparator()
result = tc.tag(tags1=sample_tags,
tags2="(1boy:1.5), (1girl:1.5), ((sitting)), (lying:0.5), long hair, twintails")
print("@@@ tagcomparator1", result)
if 'school_uniform, (v:1.2), (standing:0.5), attack, original_tag' != result[0]:
raise Exception("Test failed")
if '(1boy:1.5), (lying:0.5), twintails' != result[1]:
raise Exception("Test failed")
if '(long hair:1.2), (sitting:1.5), (1girl:1.2)' != result[2]:
raise Exception("Test failed")
tr = TagRemover()
result = tr.tag(tags=sample_tags,
exclude_tags="school_uniform, long hair, 1girl")
print("@@@ tagremover1", result)
if '(v:1.2), (sitting:1.5), (standing:0.5), attack, original_tag' != result[0]:
raise Exception("Test failed")
#if __name__ == "__main__":
# simple_test()