Beispiel #1
0
def align_multi(cfg, image, min_confidence=0.97, limits=None):
    boxes = []
    landmarks = []
    detector = MTCNN()
    faces = detector.detect_faces(image)
    refrence = get_reference_facial_points(default_square=True)
    for face in faces:
        if face['confidence'] < min_confidence:
            continue
        boxes.append(face['box'])
        landmark = []
        for name, points in face['keypoints'].items():
            landmark.append(list(points))
        landmarks.append(landmark)
    if limits:
        boxes = boxes[:limits]
        landmarks = landmarks[:limits]

    faces = []
    for landmark in landmarks:
        warped_face = warp_and_crop_face(image,
                                         landmark,
                                         reference_pts=refrence,
                                         crop_size=(cfg['input_size'],
                                                    cfg['input_size']))
        faces.append(warped_face)

    return np.array(boxes), np.array(landmarks), np.array(faces)
def align_face(img_path, resize_face_size=112):
    img = Image.open(img_path)
    bounding_boxes, landmarks = detect_faces(img)
    scale = resize_face_size / 112.
    reference = get_reference_facial_points(default_square = True) * scale
    facial5points = [[landmarks[0][j], landmarks[0][j + 5]] for j in range(5)]
    warped_face = warp_and_crop_face(np.array(img), facial5points, reference, crop_size=(resize_face_size, resize_face_size))
    return warped_face
Beispiel #3
0
def prepare_facebank(cfg, model):
    names = ['Unknown']
    embeddings = []
    detector = MTCNN()
    for name in os.listdir(cfg['face_bank']):
        if os.path.isfile(name):
            continue
        else:
            emb = []
            for file in tqdm(os.listdir(os.path.join(cfg['face_bank'], name))):
                if not os.path.isfile(
                        os.path.join(cfg['face_bank'], name, file)):
                    continue
                else:
                    image = cv2.imread(
                        os.path.join(cfg['face_bank'], name, file))
                    image = cv2.resize(image,
                                       (cfg['input_size'], cfg['input_size']))
                    face = detector.detect_faces(image)
                    if len(face) > 0:
                        face = face[0]
                        refrence = get_reference_facial_points(
                            default_square=True)

                        landmark = []
                        for _, points in face['keypoints'].items():
                            landmark.append(list(points))

                        warped_face = warp_and_crop_face(
                            image,
                            landmark,
                            reference_pts=refrence,
                            crop_size=(cfg['input_size'], cfg['input_size']))
                        image = np.array(warped_face)

                        image = image.astype(np.float32) / 255.
                        if len(image.shape) == 3:
                            image = np.expand_dims(image, 0)
                        emb.append(l2_norm(model(image)).numpy())
            if len(emb) == 0:
                continue
            emb = np.array(emb)
            mean = np.mean(emb, axis=0)
            embeddings.append(mean)
        names.append(name)
    embeddings = np.array(embeddings)
    names = np.array(names)

    np.save(os.path.join('data', 'facebank.npy'), embeddings)
    np.save(os.path.join('data', 'names.npy'), names)

    return embeddings, names
Beispiel #4
0
def align(cfg, image):
    detector = MTCNN()
    face = detector.detect_faces(image)[0]
    refrence = get_reference_facial_points(default_square=True)

    landmark = []
    for name, points in face['keypoints'].items():
        landmark.append(list(points))

    warped_face = warp_and_crop_face(image,
                                     landmark,
                                     reference_pts=refrence,
                                     crop_size=(cfg['input_size'],
                                                cfg['input_size']))
    return warped_face
                        help="specify your destination dir",
                        default="./data/test_Aligned",
                        type=str)
    parser.add_argument(
        "-crop_size",
        "--crop_size",
        help="specify size of aligned faces, align and crop with padding",
        default=112,
        type=int)
    args = parser.parse_args()

    source_root = args.source_root  # specify your source dir
    dest_root = args.dest_root  # specify your destination dir
    crop_size = args.crop_size  # specify size of aligned faces, align and crop with padding
    scale = crop_size / 112.
    reference = get_reference_facial_points(default_square=True) * scale

    cwd = os.getcwd()  # delete '.DS_Store' existed in the source_root
    os.chdir(source_root)
    os.system("find . -name '*.DS_Store' -type f -delete")
    os.system("find . -name '*.ipynb_checkpoints' -prune -exec rm -rf {} \;")
    os.chdir(cwd)

    if not os.path.isdir(dest_root):
        os.mkdir(dest_root)

    for subfolder in tqdm(os.listdir(source_root)):
        if not os.path.isdir(os.path.join(dest_root, subfolder)):
            os.mkdir(os.path.join(dest_root, subfolder))
        for image_name in os.listdir(os.path.join(source_root, subfolder)):
            print("Processing\t{}".format(
        default="/media/hyo/文档/Dataset/face_dataset_v2/lfw_aligned",
        type=str)
    parser.add_argument(
        "-crop_size",
        "--crop_size",
        help="specify size of aligned faces, align and crop with padding",
        default=224,
        type=int)
    args = parser.parse_args()

    source_root = args.source_root  # specify your source dir
    dest_root = args.dest_root  # specify your destination dir
    crop_size = args.crop_size  # specify size of aligned faces, align and crop with padding
    scale = crop_size / 112.
    # reference = get_reference_facial_points(default_square = True) * scale
    original_5_points = get_reference_facial_points(default_square=True)
    # original_5_points[:, 1] = original_5_points[:, 1] - 15
    reference = (original_5_points) * scale

    cwd = os.getcwd()  # delete '.DS_Store' existed in the source_root
    os.chdir(source_root)
    os.system("find . -name '*.DS_Store' -type f -delete")
    os.chdir(cwd)

    if not os.path.isdir(dest_root):
        os.mkdir(dest_root)

    # for image_name in tqdm(os.listdir(source_root)):
    #     if not os.path.isdir(os.path.join(dest_root, subfolder)):
    #         os.mkdir(os.path.join(dest_root, subfolder))
    #         # for image_name in os.listdir(os.path.join(source_root, subfolder)):
from tqdm import tqdm
import argparse


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description = "face alignment")
    parser.add_argument("-source_root", "--source_root", help = "specify your source dir", default = "./data/test", type = str)
    parser.add_argument("-dest_root", "--dest_root", help = "specify your destination dir", default = "./data/test_Aligned", type = str)
    parser.add_argument("-crop_size", "--crop_size", help = "specify size of aligned faces, align and crop with padding", default = 112, type = int)
    args = parser.parse_args()

    source_root = args.source_root # specify your source dir
    dest_root = args.dest_root # specify your destination dir
    crop_size = args.crop_size # specify size of aligned faces, align and crop with padding
    scale = crop_size / 112.
    reference = get_reference_facial_points(default_square = True) * scale

    cwd = os.getcwd() # delete '.DS_Store' existed in the source_root
    os.chdir(source_root)
    os.system("find . -name '*.DS_Store' -type f -delete")
    os.chdir(cwd)

    if not os.path.isdir(dest_root):
        os.mkdir(dest_root)

    for subfolder in tqdm(os.listdir(source_root)):
        if not os.path.isdir(os.path.join(dest_root, subfolder)):
            os.mkdir(os.path.join(dest_root, subfolder))
        for image_name in os.listdir(os.path.join(source_root, subfolder)):
            print("Processing\t{}".format(os.path.join(source_root, subfolder, image_name)))
            img = Image.open(os.path.join(source_root, subfolder, image_name))