Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new function to add multiple masks for multiple faces in the image #34

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
99 194 99 211 100 226 102 242 104 258 107 273 110 289 114 304 120 319 126 333 135 346 145 358 157 369 169 379 183 387 199 392 217 393 236 390 252 384 265 375 277 363 287 351 296 339 303 324 308 310 311 295 314 279 315 263 316 247 316 232 316 216 316 200 313 184 114 200 127 186 146 182 165 183 183 187 182 196 164 194 146 193 130 195 226 185 243 179 263 176 282 179 296 192 280 189 263 187 245 189 227 193 205 198 206 219 206 240 207 262 190 206 183 247 176 265 185 273 196 276 209 279 222 274 232 271 241 262 232 245 222 205 137 206 146 199 158 195 170 198 179 206 169 207 158 209 147 208 158 202 233 203 242 195 253 191 265 193 275 200 266 203 255 205 243 204 255 196 162 303 180 301 199 300 209 302 219 299 241 298 263 299 250 315 234 326 212 332 190 328 174 317 169 304 185 306 210 308 236 304 255 301 238 312 211 318 185 314 158 202 255 197
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
528 144 529 156 530 169 531 181 534 194 536 206 540 218 545 230 551 241 559 250 569 259 580 265 592 271 605 275 618 277 632 278 646 276 658 273 667 267 673 259 679 250 684 241 687 231 689 221 691 210 691 200 691 190 691 180 690 169 689 159 687 150 685 140 683 130 559 137 568 124 582 120 597 119 610 122 610 129 597 128 583 128 571 131 639 120 648 115 659 112 670 114 677 123 668 120 659 120 650 122 641 126 627 139 631 151 634 163 638 175 616 147 615 174 611 188 620 191 628 192 637 192 644 189 650 187 655 182 649 169 639 145 575 149 581 143 590 140 599 142 606 148 599 150 590 152 582 151 590 146 642 143 647 137 655 133 663 135 669 140 664 143 656 145 649 145 655 138 608 226 619 217 632 211 639 212 645 210 656 213 665 220 659 226 651 232 641 234 629 234 618 231 613 225 624 221 639 219 652 218 661 220 653 222 640 224 625 225 590 146 655 139
22 changes: 22 additions & 0 deletions addition_module/face_mask_adding/FMA-3D/add_mask_multi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
@author: Yinglu Liu, Jun Wang
@modifier: Champagne Jin ([email protected])
@date: 20201012
"""

from face_masker import FaceMasker

if __name__ == '__main__':
is_aug = True
image_path = 'Data/test-data/test1.jpg'
template_name = '0.png'
masked_face_path = 'test1_mask1_several.jpg'

face_lms_files = ['Data/test-data/test1_landmark_res0.txt', 'Data/test-data/test1_landmark_res1.txt']
face_lmses = []
for face_lms_file in face_lms_files:
face_lms_str = open(face_lms_file).readline().strip().split(' ')
face_lmses.append([float(num) for num in face_lms_str])

face_masker = FaceMasker(is_aug)
face_masker.add_mask_multi(image_path, face_lmses, template_name, masked_face_path)
37 changes: 37 additions & 0 deletions addition_module/face_mask_adding/FMA-3D/face_masker.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,43 @@ def add_mask_one(self, image_path, face_lms, template_name, masked_face_path):
new_image = np.clip(new_image, -1, 1) #must clip to (-1, 1)!
imsave(masked_face_path, new_image)

def add_mask_multi(self, image_path, face_lmses, template_name, masked_face_path):
"""Add mask to one image.

Args:
image_path(str): the image to add mask.
face_lmses(str): list of face landmarks, [[x1, y1, x2, y2, ..., x106, y106], [x1, y1, x2, y2, ..., x106, y106], ...]
template_name(str): the mask template to be added on the current image,
got to '/Data/mask-data' for all template.
masked_face_path(str): the path to save masked image.
"""
import copy
image = imread(image_path)
ref_texture_src = self.template_name2ref_texture_src[template_name]
uv_mask_src = self.template_name2uv_mask_src[template_name]
if image.ndim == 2:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
[h, w, c] = image.shape
if c == 4:
image = image[:,:,:3]
image = image/255. #!!
image = copy.deepcopy(image)
for face_lms in face_lmses:
pos, vertices = self.get_vertices(face_lms, image) #3d reconstruction -> get texture.
texture = cv2.remap(image, pos[:,:,:2].astype(np.float32), None,
interpolation=cv2.INTER_NEAREST,
borderMode=cv2.BORDER_CONSTANT,borderValue=(0))
new_texture = self.get_new_texture(ref_texture_src, uv_mask_src, texture)
#remap to input image.(render)
vis_colors = np.ones((vertices.shape[0], 1))
face_mask = mesh.render.render_colors(vertices, self.prn.triangles, vis_colors, h, w, c = 1)
face_mask = np.squeeze(face_mask > 0).astype(np.float32)
new_colors = self.prn.get_colors_from_texture(new_texture)
new_image = mesh.render.render_colors(vertices, self.prn.triangles, new_colors, h, w, c = 3)
image = image * (1 - face_mask[:, :, np.newaxis]) + new_image * face_mask[:, :, np.newaxis]
image = np.clip(image, -1, 1) #must clip to (-1, 1)!
imsave(masked_face_path, image)

def get_vertices(self, face_lms, image):
"""Get vertices

Expand Down