Search code examples
deep-learningcomputer-visionobject-detectionface-detectiondlib

use yolov4 face detection with face_recognition


Can I use yolov4 for object detection and use the face_recognition library to recognize detected faces, or do I need to use face detection provided by the face_recognition library in order to use its face recognition?


Solution

  • face_recognition library uses dlib's inbuilt algorithm specific for face-detection. It's claimed accuracy is 99%+. You cannot change that algorithm to YoloV4 or any other.

    The network architecture for face_recognition is based on ResNet-34, but with fewer layers and the number of filters reduced by half. The network was trained on a dataset of 3 million images on the Labeled Faces in the Wild (LFW) dataset.

    Have a look at Davis King (the creator of dlib) and Adam Geitgey (author of face_recognition) articles on how deep learning-based facial recognition works:

    High Quality Face Recognition with Deep Metric Learning

    Modern Face Recognition with Deep Learning

    However if that is not sufficient for your case, you can train YoloV4 to detect faces and then after detecting, crop that face and give that as input to face_recognition library.

    import face_recognition
    
    picture_of_me = face_recognition.load_image_file("me.jpg")
    my_face_encoding = face_recognition.face_encodings(picture_of_me)[0]
    
    # my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face!
    
    unknown_picture = face_recognition.load_image_file("unknown.jpg")
    unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]
    
    # Now we can see the two face encodings are of the same person with `compare_faces`!
    
    results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding)
    
    if results[0] == True:
        print("It's a picture of me!")
    else:
        print("It's not a picture of me!")