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_mask_one #23

Open
zengwb-lx opened this issue Jan 29, 2021 · 8 comments
Open

add_mask_one #23

zengwb-lx opened this issue Jan 29, 2021 · 8 comments

Comments

@zengwb-lx
Copy link

你好,add_mask如果一张图有两个脸的话,无法同时给两个脸加上口罩,在遍历face_lms时会把前一次加的口罩去除掉,有什么好的建议修改加载一图多脸的情况吗? 谢谢

@zengwb-lx
Copy link
Author

222
就像这样的

@FunkyKoki
Copy link

add_mask_one 本质上就是一张脸一张脸进行处理的,但是 add_mask_one 会在最后存储加了mask的图像,一种很简单的方法就是在处理一张人脸之后得到的新图像上继续使用 add_mask_one 给另外一个人脸加mask。这个应该并不算难,只要有对应的人脸的landmark坐标就可以实现。

@zengwb-lx
Copy link
Author

zengwb-lx commented Jan 31, 2021 via email

@FunkyKoki
Copy link

FunkyKoki commented Feb 1, 2021

I made a simple implementation:

First, you need to get the corresponding facial landmark detection results by the face_sdk and copy them (.txt files) into /FaceX-Zoo/addition_module/face_mask_adding/FMA-3D/Data/test-data/ directory.

Second, add the following codes into class FaceMasker in /FaceX-Zoo/addition_module/face_mask_adding/FMA-3D/face_masker.py :

"""
@author: Yinglu Liu, Jun Wang
@modifier: Champagne Jin (643683905@qq.com)
@date: 20210201
"""

def add_mask_several(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))  # 原来的人脸texture
            new_texture = self.get_new_texture(ref_texture_src, uv_mask_src, texture) # 加上mask的人脸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)

At last, add a new file called add_mask_several.py in directory /FaceX-Zoo/addition_module/face_mask_adding/FMA-3D/, and paste the codes below into it:

"""
@author: Yinglu Liu, Jun Wang
@modifier: Champagne Jin (643683905@qq.com)
@date: 20210201
"""

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_several(image_path, face_lmses, template_name, masked_face_path)

Tips: the face_lms_files is the corresponding facial landmark detection files list.

Then, you just need to run it.
Good luck!

@FunkyKoki
Copy link

test1_mask1_several
This is the result I got just now 😄

@zengwb-lx
Copy link
Author

thanks for you reply.

@FunkyKoki
Copy link

😸

@liujia761
Copy link

你你,add_mask如果一张脸个脸,无法,无法无法给给给两两两个个,face_lms face_lms时会时会时会时会把把前前一次一次除掉的加加的情况如何? 谢谢

好喽,您好 我在用face-3d时没有找到readme中由a-f相关的代码,请问一下 这个您有实现吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants