Gainward777 commited on
Commit
c2b172c
1 Parent(s): f71f039

Update utils/utils.py

Browse files
Files changed (1) hide show
  1. utils/utils.py +77 -77
utils/utils.py CHANGED
@@ -1,77 +1,77 @@
1
- from PIL import Image
2
- import numpy as np
3
- import cv2
4
-
5
-
6
- #first stage sketch preprocess
7
- def conventional_resize(img):
8
- original_width, original_height = img.size
9
- aspect_ratio = original_width / original_height
10
-
11
- conventional_sizes = {
12
- 1: (1024, 1024),
13
- 4/3: (1152, 896),
14
- 3/2: (1216, 832),
15
- 16/9: (1344, 768),
16
- 21/9: (1568, 672),
17
- 3/1: (1728, 576),
18
- 1/4: (512, 2048),
19
- 1/3: (576, 1728),
20
- 9/16: (768, 1344),
21
- 2/3: (832, 1216),
22
- 3/4: (896, 1152)
23
- }
24
-
25
- closest_aspect_ratio = min(conventional_sizes.keys(), key=lambda x: abs(x - aspect_ratio))
26
- new_width, new_height = conventional_sizes[closest_aspect_ratio]
27
-
28
- resized_img = img.resize((new_width, new_height), Image.LANCZOS)
29
-
30
- return resized_img
31
-
32
-
33
- def get_substrate(img, color=(255, 255, 255, 255)):
34
- size=img.size
35
- substrate = Image.new("RGBA", size, color)
36
- return substrate.convert("RGB")
37
-
38
-
39
- def sketch_process(img):
40
- substrate=conventional_resize(get_substrate(img))
41
- resized_img = conventional_resize(img)
42
- return substrate, resized_img
43
-
44
-
45
-
46
-
47
- #first stage prompt preprocess
48
- def remove_duplicates(base_prompt):
49
- prompt_list = base_prompt.split(", ")
50
- seen = set()
51
- unique_tags = []
52
- for tag in prompt_list :
53
- tag_clean = tag.lower().strip()
54
- if tag_clean not in seen and tag_clean != "":
55
- unique_tags.append(tag)
56
- seen.add(tag_clean)
57
- return ", ".join(unique_tags)
58
-
59
-
60
- def remove_color(base_prompt):
61
- prompt_list = base_prompt.split(", ")
62
- color_list = ["pink", "red", "orange", "brown", "yellow", "green", "blue", "purple", "blonde", "colored skin", "white hair"]
63
- cleaned_tags = [tag for tag in prompt_list if all(color.lower() not in tag.lower() for color in color_list)]
64
- return ", ".join(cleaned_tags)
65
-
66
-
67
- def execute_prompt(base_prompt):
68
- prompt_list = base_prompt.split(", ")
69
- execute_tags = ["sketch", "transparent background"]
70
- filtered_tags = [tag for tag in prompt_list if tag not in execute_tags]
71
- return ", ".join(filtered_tags)
72
-
73
- def prompt_preprocess(prompt):
74
- result=execute_prompt(prompt)
75
- result=remove_duplicates(result)
76
- result=remove_color(result)
77
- return result
 
1
+ from PIL import Image
2
+ import numpy as np
3
+ import cv2
4
+
5
+
6
+ #first stage sketch preprocess
7
+ def conventional_resize(img):
8
+ original_width, original_height = img.size
9
+ aspect_ratio = original_width / original_height
10
+
11
+ conventional_sizes = {
12
+ 1: (1024, 1024),
13
+ 4/3: (1152, 896),
14
+ 3/2: (1216, 832),
15
+ 16/9: (1344, 768),
16
+ 21/9: (1568, 672),
17
+ 3/1: (1728, 576),
18
+ 1/4: (512, 2048),
19
+ 1/3: (576, 1728),
20
+ 9/16: (768, 1344),
21
+ 2/3: (832, 1216),
22
+ 3/4: (896, 1152)
23
+ }
24
+
25
+ closest_aspect_ratio = 1.333333333 #min(conventional_sizes.keys(), key=lambda x: abs(x - aspect_ratio))
26
+ new_width, new_height = conventional_sizes[closest_aspect_ratio]
27
+
28
+ resized_img = img.resize((new_width, new_height), Image.LANCZOS)
29
+
30
+ return resized_img
31
+
32
+
33
+ def get_substrate(img, color=(255, 255, 255, 255)):
34
+ size=img.size
35
+ substrate = Image.new("RGBA", size, color)
36
+ return substrate.convert("RGB")
37
+
38
+
39
+ def sketch_process(img):
40
+ substrate=conventional_resize(get_substrate(img))
41
+ resized_img = conventional_resize(img)
42
+ return substrate, resized_img
43
+
44
+
45
+
46
+
47
+ #first stage prompt preprocess
48
+ def remove_duplicates(base_prompt):
49
+ prompt_list = base_prompt.split(", ")
50
+ seen = set()
51
+ unique_tags = []
52
+ for tag in prompt_list :
53
+ tag_clean = tag.lower().strip()
54
+ if tag_clean not in seen and tag_clean != "":
55
+ unique_tags.append(tag)
56
+ seen.add(tag_clean)
57
+ return ", ".join(unique_tags)
58
+
59
+
60
+ def remove_color(base_prompt):
61
+ prompt_list = base_prompt.split(", ")
62
+ color_list = ["pink", "red", "orange", "brown", "yellow", "green", "blue", "purple", "blonde", "colored skin", "white hair"]
63
+ cleaned_tags = [tag for tag in prompt_list if all(color.lower() not in tag.lower() for color in color_list)]
64
+ return ", ".join(cleaned_tags)
65
+
66
+
67
+ def execute_prompt(base_prompt):
68
+ prompt_list = base_prompt.split(", ")
69
+ execute_tags = ["sketch", "transparent background"]
70
+ filtered_tags = [tag for tag in prompt_list if tag not in execute_tags]
71
+ return ", ".join(filtered_tags)
72
+
73
+ def prompt_preprocess(prompt):
74
+ result=execute_prompt(prompt)
75
+ result=remove_duplicates(result)
76
+ result=remove_color(result)
77
+ return result