JDAI-CV / FaceX-Zoo

A PyTorch Toolbox for Face Recognition
https://arxiv.org/pdf/2101.04407.pdf
Other
1.87k stars 433 forks source link

add_mask_one #23

Open zengwb-lx opened 3 years ago

zengwb-lx commented 3 years ago

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

zengwb-lx commented 3 years ago

222 就像这样的

FunkyKoki commented 3 years ago

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

zengwb-lx commented 3 years ago

我就是这样做的,先输入原图和一个脸的关键点得到加mask的image,然后输入另一个脸landmark和image,而且这个image我还深拷贝了一下,但是输出的是如上图,想不通怎么回事。你可以试一下,谢谢------------------ 原始邮件 ------------------ 发件人: "Champagne&nbsp;Jin"<notifications@github.com> 发送时间: 2021年1月31日(星期天) 中午1:47 收件人: "JDAI-CV/FaceX-Zoo"<FaceX-Zoo@noreply.github.com>; 抄送: "zengwb-lx"<1303281435@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [JDAI-CV/FaceX-Zoo] add_mask_one (#23)

FunkyKoki commented 3 years ago

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 commented 3 years ago

test1_mask1_several This is the result I got just now 😄

zengwb-lx commented 3 years ago

thanks for you reply.

FunkyKoki commented 3 years ago

😸

liujia761 commented 1 year ago

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

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