microsoft / Deep3DFaceReconstruction

Accurate 3D Face Reconstruction with Weakly-Supervised Learning: From Single Image to Image Set (CVPRW 2019)
MIT License
2.17k stars 441 forks source link

Face detectors to get 5 landmarks #126

Open Ishve opened 3 years ago

Ishve commented 3 years ago

How to get public face detectors to get 5 landmarks, and save all images and corresponding landmarks in . can you please explain this step. i am unable to proceed further.

fefogarcia commented 3 years ago

Hi @Ishve, for the landmark detection you can use a dedicated model such as https://github.com/ipazc/mtcnn

It gives you the X and Y coordinates for five landmarks for the image, which you can then save as 5 separate lines in a .txt file with the same name as the image, in the input folder. It will look something like this (notice I'm using random numbers just to show you the format):

122    190
180    188
234    345
876    332
765    766

From what I understand (and worked for me), in each line the X and Y coordinates are separated by a tab.

fefogarcia commented 3 years ago

If it helps, here's the quick Python script I put together that takes an input dir as the argument and then goes through all the images, saving the text files with the landmarks

from mtcnn import MTCNN
import os
import cv2
import argparse

parser = argparse.ArgumentParser(description='Get landmarks from images.')
parser.add_argument('--input_dir', type=str, default="./input",
                    help='directory with the input images')

args = parser.parse_args()
input_dir = args.input_dir

detector = MTCNN()
for filename in os.listdir(input_dir):
    basename = os.path.splitext(filename)[0]

    image_path = f"{input_dir}/{filename}"
    text_path = f"{input_dir}/{basename}.txt"

    img = cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB)
    result = detector.detect_faces(img)
    keypoints = result[0]['keypoints']
    text_file = open(text_path, "a")
    for value in keypoints.values():
        text_file.write(f"{value[0]}\t{value[1]}\n")
    print(f"File successfully written: {text_path}")
    text_file.close()