Create utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import csv
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
def normalize8(I):
|
8 |
+
mn = I.min()
|
9 |
+
mx = I.max()
|
10 |
+
mx -= mn
|
11 |
+
I = ((I - mn) / mx) * 255
|
12 |
+
return I.astype(np.uint8)
|
13 |
+
|
14 |
+
|
15 |
+
def overlay_transparent(background, overlay, x, y):
|
16 |
+
|
17 |
+
background_width = background.shape[1]
|
18 |
+
background_height = background.shape[0]
|
19 |
+
|
20 |
+
if x >= background_width or y >= background_height:
|
21 |
+
return background
|
22 |
+
|
23 |
+
h, w = overlay.shape[0], overlay.shape[1]
|
24 |
+
|
25 |
+
if x + w > background_width:
|
26 |
+
w = background_width - x
|
27 |
+
overlay = overlay[:, :w]
|
28 |
+
|
29 |
+
if y + h > background_height:
|
30 |
+
h = background_height - y
|
31 |
+
overlay = overlay[:h]
|
32 |
+
|
33 |
+
if overlay.shape[2] < 4:
|
34 |
+
overlay = np.concatenate(
|
35 |
+
[
|
36 |
+
overlay,
|
37 |
+
np.ones((overlay.shape[0], overlay.shape[1], 1), dtype=overlay.dtype)
|
38 |
+
* 255,
|
39 |
+
],
|
40 |
+
axis=2,
|
41 |
+
)
|
42 |
+
|
43 |
+
overlay_image = overlay[..., :3]
|
44 |
+
mask = overlay[..., 3:] / 255.0
|
45 |
+
|
46 |
+
background[y : y + h, x : x + w] = (1.0 - mask) * background[
|
47 |
+
y : y + h, x : x + w
|
48 |
+
] + mask * overlay_image
|
49 |
+
|
50 |
+
return background
|
51 |
+
|
52 |
+
|
53 |
+
def get_mask_points(mask_name):
|
54 |
+
if mask_name == "Front Man Mask":
|
55 |
+
mask_path = "./assets/front_man_mask.png"
|
56 |
+
csv_path = "./assets/front_man_mask.csv"
|
57 |
+
elif mask_name == "Guards Mask":
|
58 |
+
mask_path = "./assets/guards_mask.png"
|
59 |
+
csv_path = "./assets/guards_mask.csv"
|
60 |
+
elif mask_name == "Red Mask":
|
61 |
+
mask_path = "./assets/redmask.png"
|
62 |
+
csv_path = "./assets/red_mask.csv"
|
63 |
+
elif mask_name == "Blue Mask":
|
64 |
+
mask_path = "./assets/bluemask.png"
|
65 |
+
csv_path = "./assets/blue_mask.csv"
|
66 |
+
else:
|
67 |
+
raise ValueError(f"❌ Unknown mask name: {mask_name}")
|
68 |
+
|
69 |
+
mask_img = cv2.imread(mask_path, cv2.IMREAD_UNCHANGED)
|
70 |
+
if mask_img is None:
|
71 |
+
raise FileNotFoundError(f"❌ Could not load mask image: {mask_path}")
|
72 |
+
|
73 |
+
mask_img = mask_img.astype(np.float32) / 255.0
|
74 |
+
|
75 |
+
mask_points = {}
|
76 |
+
with open(csv_path) as csv_file:
|
77 |
+
csv_reader = csv.reader(csv_file, delimiter=",")
|
78 |
+
for i, row in enumerate(csv_reader):
|
79 |
+
mask_points[int(row[0])] = [float(row[1]), float(row[2])]
|
80 |
+
|
81 |
+
return mask_points, mask_img
|
82 |
+
|
83 |
+
def mask_overlay(image, faces, mask_up, mask_down, mask_name):
|
84 |
+
mask_points, mask_img = get_mask_points(mask_name)
|
85 |
+
mirror_point = {
|
86 |
+
234: 1,
|
87 |
+
93: 2,
|
88 |
+
132: 3,
|
89 |
+
58: 4,
|
90 |
+
172: 5,
|
91 |
+
136: 6,
|
92 |
+
150: 7,
|
93 |
+
149: 8,
|
94 |
+
176: 9,
|
95 |
+
148: 10,
|
96 |
+
152: 11,
|
97 |
+
377: 12,
|
98 |
+
400: 13,
|
99 |
+
378: 14,
|
100 |
+
379: 15,
|
101 |
+
365: 16,
|
102 |
+
397: 17,
|
103 |
+
288: 18,
|
104 |
+
361: 19,
|
105 |
+
323: 20,
|
106 |
+
454: 21,
|
107 |
+
356: 22,
|
108 |
+
389: 23,
|
109 |
+
251: 24,
|
110 |
+
284: 25,
|
111 |
+
332: 26,
|
112 |
+
297: 27,
|
113 |
+
338: 28,
|
114 |
+
10: 29,
|
115 |
+
109: 30,
|
116 |
+
67: 31,
|
117 |
+
103: 32,
|
118 |
+
54: 33,
|
119 |
+
21: 34,
|
120 |
+
162: 35,
|
121 |
+
127: 36,
|
122 |
+
}
|
123 |
+
|
124 |
+
mask_points = mask_points
|
125 |
+
src_pts = []
|
126 |
+
for i in sorted(mask_points.keys()):
|
127 |
+
try:
|
128 |
+
src_pts.append(np.array(mask_points[i]))
|
129 |
+
except ValueError:
|
130 |
+
continue
|
131 |
+
src_pts = np.array(src_pts, dtype="float32")
|
132 |
+
extend_y = [
|
133 |
+
1,
|
134 |
+
2,
|
135 |
+
3,
|
136 |
+
4,
|
137 |
+
5,
|
138 |
+
6,
|
139 |
+
7,
|
140 |
+
8,
|
141 |
+
9,
|
142 |
+
10,
|
143 |
+
11,
|
144 |
+
12,
|
145 |
+
13,
|
146 |
+
14,
|
147 |
+
15,
|
148 |
+
16,
|
149 |
+
17,
|
150 |
+
18,
|
151 |
+
19,
|
152 |
+
20,
|
153 |
+
21,
|
154 |
+
]
|
155 |
+
minimize_y = [22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36]
|
156 |
+
face_points = {}
|
157 |
+
for i in faces[0]:
|
158 |
+
for j in mirror_point.keys():
|
159 |
+
if i[0] == j:
|
160 |
+
if mirror_point[i[0]] in minimize_y:
|
161 |
+
face_points[mirror_point[j]] = [
|
162 |
+
float(i[1]),
|
163 |
+
float(i[2] - int(mask_up)),
|
164 |
+
]
|
165 |
+
else:
|
166 |
+
if mirror_point[i[0]] in extend_y:
|
167 |
+
face_points[mirror_point[j]] = [
|
168 |
+
float(i[1]),
|
169 |
+
float(i[2] + int(mask_down)),
|
170 |
+
]
|
171 |
+
else:
|
172 |
+
face_points[mirror_point[j]] = [float(i[1]), float(i[2])]
|
173 |
+
dst_pts = []
|
174 |
+
for i in sorted(face_points.keys()):
|
175 |
+
try:
|
176 |
+
dst_pts.append(np.array(face_points[i]))
|
177 |
+
except ValueError:
|
178 |
+
continue
|
179 |
+
dst_pts = np.array(dst_pts, dtype="float32")
|
180 |
+
M, _ = cv2.findHomography(src_pts, dst_pts)
|
181 |
+
transformed_mask = cv2.warpPerspective(
|
182 |
+
mask_img,
|
183 |
+
M,
|
184 |
+
(image.shape[1], image.shape[0]),
|
185 |
+
None,
|
186 |
+
cv2.INTER_LINEAR,
|
187 |
+
cv2.BORDER_CONSTANT,
|
188 |
+
borderValue=[0, 0, 0, 0],
|
189 |
+
)
|
190 |
+
png_image = normalize8(transformed_mask)
|
191 |
+
new_image = overlay_transparent(image, png_image, 0, 0)
|
192 |
+
# cv2.imwrite("output.png", new_image)
|
193 |
+
return new_image
|