Git Product home page Git Product logo

Comments (3)

leoluopy avatar leoluopy commented on May 17, 2024

megaFace raw image seems not available publicly , following code can add the masks , but it may not be totally the same as FaceXZoo's implementation .


import math
import sys, os

import cv2
import yaml
import numpy as np
from tqdm import tqdm

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
from core.image_cropper.arcface_cropper.FaceRecImageCropper import FaceRecImageCropper
from core.model_handler.face_alignment.FaceAlignModelHandler import FaceAlignModelHandler
from core.model_handler.face_detection.FaceDetModelHandler import FaceDetModelHandler
from core.model_loader.face_alignment.FaceAlignModelLoader import FaceAlignModelLoader
from core.model_loader.face_detection.FaceDetModelLoader import FaceDetModelLoader

from addition_module.face_mask_adding.FMA3D.face_masker import FaceMasker


def distance2center(x1, y1, x2, y2, image):
    im_cx = int(image.shape[1] / 2)
    im_cy = int(image.shape[0] / 2)
    cx = ((x2 + x1) / 2).astype(int)
    cy = ((y2 + y1) / 2).astype(int)
    return math.sqrt(math.pow(im_cx - cx, 2) + math.pow(im_cy - cy, 2))


def Filter2centerBox(boundingBoxes, frame):
    min_distance = 999999999
    min_idx = -1
    for i, det in enumerate(boundingBoxes):
        distance = distance2center(det[0], det[1], det[2], det[3], frame)
        # cv2.rectangle(frame, (int(det[0]), int(det[1])), (int(det[2]), int(det[3])), (2, 255, 0), 1)
        # cv2.imshow("x", frame)
        # cv2.waitKey(0)
        if distance < min_distance:
            min_idx = i
            min_distance = distance
    return np.array([boundingBoxes[min_idx]])


def GetFaceLandMark(image_path):
    print("aligning {}".format(image_path))
    image = cv2.imread(image_path, cv2.IMREAD_COLOR)
    dets = faceDetModelHandler.inference_on_image(image)
    dets = Filter2centerBox(dets, image)

    assert (len(dets) == 1)  # only one face in picture
    landmarks = faceAlignModelHandler.inference_on_image(image, dets[0])
    return landmarks


input_root = "/home/leo/workspace/data_set/megaface/facescrub_images"
out_root = "/home/leo/workspace/data_set/megaface/facescrub_images_addmasked"

if __name__ == '__main__':
    with open('config/model_conf.yaml') as f:
        model_conf = yaml.load(f)

    model_path = 'models'
    # detect init
    scene = 'non-mask'
    model_category = 'face_detection'
    model_name = model_conf[scene][model_category]
    faceDetModelLoader = FaceDetModelLoader(model_path, model_category, model_name)
    modelDet, cfgDet = faceDetModelLoader.load_model()
    faceDetModelHandler = FaceDetModelHandler(modelDet, 'cuda:0', cfgDet)

    # alignment init
    model_category = 'face_alignment'
    model_name = model_conf[scene][model_category]
    faceAlignModelLoader = FaceAlignModelLoader(model_path, model_category, model_name)
    modelAli, cfgAli = faceAlignModelLoader.load_model()
    faceAlignModelHandler = FaceAlignModelHandler(modelAli, 'cuda:0', cfgAli)

    # face croper
    face_cropper = FaceRecImageCropper()

    # face masker
    face_masker = FaceMasker(is_aug=True)

    template_name = ['0.png', '1.png', '2.png', '3.png', '4.png', '5.png', '6.png', '7.png']
    idx = 0
    all_pathes = []
    for root, dirs, files in os.walk(input_root):
        for file in files:
            image_path = os.path.join(root, file)
            all_pathes.append(image_path)

    for image_path in tqdm(all_pathes):
        masked_face_path = image_path.replace(input_root, out_root)
        if os.path.exists(os.path.dirname(masked_face_path)) is False:
            os.makedirs(os.path.dirname(masked_face_path))
        lms = GetFaceLandMark(image_path)
        face_lms = list(lms.reshape(-1))
        face_masker.add_mask_one(image_path, face_lms, template_name[idx % 8], masked_face_path)
        idx += 1

from facex-zoo.

wang21jun avatar wang21jun commented on May 17, 2024

Just run 'add_mask_all.py' with the provided 'facescrub2template_name.txt' and 'facescrub_face_info.txt'.

from facex-zoo.

leoluopy avatar leoluopy commented on May 17, 2024

Just run 'add_mask_all.py' with the provided 'facescrub2template_name.txt' and 'facescrub_face_info.txt'.

thanks for reply , and i have tried the method you mentioned , it seems that the origin megaFace face pictures which is not aligned are needed . but the original raw megaface pictures probably are not available in the official website.
any suggestions ?

from facex-zoo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.