Spaces:
Sleeping
Sleeping
Upload create_mask.py
Browse files- create_mask.py +118 -0
create_mask.py
ADDED
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Author: aqeelanwar
|
2 |
+
# Created: 6 July,2020, 12:14 AM
|
3 |
+
# Email: [email protected]
|
4 |
+
|
5 |
+
from PIL import ImageColor
|
6 |
+
import cv2
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
COLOR = [
|
10 |
+
"#fc1c1a",
|
11 |
+
"#177ABC",
|
12 |
+
"#94B6D2",
|
13 |
+
"#A5AB81",
|
14 |
+
"#DD8047",
|
15 |
+
"#6b425e",
|
16 |
+
"#e26d5a",
|
17 |
+
"#c92c48",
|
18 |
+
"#6a506d",
|
19 |
+
"#ffc900",
|
20 |
+
"#ffffff",
|
21 |
+
"#000000",
|
22 |
+
"#49ff00",
|
23 |
+
]
|
24 |
+
|
25 |
+
|
26 |
+
def color_the_mask(mask_image, color, intensity):
|
27 |
+
assert 0 <= intensity <= 1, "intensity should be between 0 and 1"
|
28 |
+
RGB_color = ImageColor.getcolor(color, "RGB")
|
29 |
+
RGB_color = (RGB_color[2], RGB_color[1], RGB_color[0])
|
30 |
+
orig_shape = mask_image.shape
|
31 |
+
bit_mask = mask_image[:, :, 3]
|
32 |
+
mask_image = mask_image[:, :, 0:3]
|
33 |
+
|
34 |
+
color_image = np.full(mask_image.shape, RGB_color, np.uint8)
|
35 |
+
mask_color = cv2.addWeighted(mask_image, 1 - intensity, color_image, intensity, 0)
|
36 |
+
mask_color = cv2.bitwise_and(mask_color, mask_color, mask=bit_mask)
|
37 |
+
colored_mask = np.zeros(orig_shape, dtype=np.uint8)
|
38 |
+
colored_mask[:, :, 0:3] = mask_color
|
39 |
+
colored_mask[:, :, 3] = bit_mask
|
40 |
+
return colored_mask
|
41 |
+
|
42 |
+
|
43 |
+
def texture_the_mask(mask_image, texture_path, intensity):
|
44 |
+
assert 0 <= intensity <= 1, "intensity should be between 0 and 1"
|
45 |
+
orig_shape = mask_image.shape
|
46 |
+
bit_mask = mask_image[:, :, 3]
|
47 |
+
mask_image = mask_image[:, :, 0:3]
|
48 |
+
texture_image = cv2.imread(texture_path)
|
49 |
+
texture_image = cv2.resize(texture_image, (orig_shape[1], orig_shape[0]))
|
50 |
+
|
51 |
+
mask_texture = cv2.addWeighted(
|
52 |
+
mask_image, 1 - intensity, texture_image, intensity, 0
|
53 |
+
)
|
54 |
+
mask_texture = cv2.bitwise_and(mask_texture, mask_texture, mask=bit_mask)
|
55 |
+
textured_mask = np.zeros(orig_shape, dtype=np.uint8)
|
56 |
+
textured_mask[:, :, 0:3] = mask_texture
|
57 |
+
textured_mask[:, :, 3] = bit_mask
|
58 |
+
|
59 |
+
return textured_mask
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
# cloth_mask = cv2.imread("masks/templates/cloth.png", cv2.IMREAD_UNCHANGED)
|
64 |
+
# # cloth_mask = color_the_mask(cloth_mask, color=COLOR[0], intensity=0.5)
|
65 |
+
# path = "masks/textures"
|
66 |
+
# path, dir, files = os.walk(path).__next__()
|
67 |
+
# first_frame = True
|
68 |
+
# col_limit = 6
|
69 |
+
# i = 0
|
70 |
+
# # img_concat_row=[]
|
71 |
+
# img_concat = []
|
72 |
+
# # for f in files:
|
73 |
+
# # if "._" not in f:
|
74 |
+
# # print(f)
|
75 |
+
# # i += 1
|
76 |
+
# # texture_image = cv2.imread(os.path.join(path, f))
|
77 |
+
# # m = texture_the_mask(cloth_mask, texture_image, intensity=0.5)
|
78 |
+
# # if first_frame:
|
79 |
+
# # img_concat_row = m
|
80 |
+
# # first_frame = False
|
81 |
+
# # else:
|
82 |
+
# # img_concat_row = cv2.hconcat((img_concat_row, m))
|
83 |
+
# #
|
84 |
+
# # if i % col_limit == 0:
|
85 |
+
# # if len(img_concat) > 0:
|
86 |
+
# # img_concat = cv2.vconcat((img_concat, img_concat_row))
|
87 |
+
# # else:
|
88 |
+
# # img_concat = img_concat_row
|
89 |
+
# # first_frame = True
|
90 |
+
#
|
91 |
+
# ## COlor the mask
|
92 |
+
# thresholds = np.arange(0.1,0.9,0.05)
|
93 |
+
# for intensity in thresholds:
|
94 |
+
# c=COLOR[2]
|
95 |
+
# # intensity = 0.5
|
96 |
+
# if "._" not in c:
|
97 |
+
# print(intensity)
|
98 |
+
# i += 1
|
99 |
+
# # texture_image = cv2.imread(os.path.join(path, f))
|
100 |
+
# m = color_the_mask(cloth_mask, c, intensity=intensity)
|
101 |
+
# if first_frame:
|
102 |
+
# img_concat_row = m
|
103 |
+
# first_frame = False
|
104 |
+
# else:
|
105 |
+
# img_concat_row = cv2.hconcat((img_concat_row, m))
|
106 |
+
#
|
107 |
+
# if i % col_limit == 0:
|
108 |
+
# if len(img_concat) > 0:
|
109 |
+
# img_concat = cv2.vconcat((img_concat, img_concat_row))
|
110 |
+
# else:
|
111 |
+
# img_concat = img_concat_row
|
112 |
+
# first_frame = True
|
113 |
+
#
|
114 |
+
#
|
115 |
+
# cv2.imshow("k", img_concat)
|
116 |
+
# cv2.imwrite("combine_N95_left.png", img_concat)
|
117 |
+
# cv2.waitKey(0)
|
118 |
+
# cc = 1
|