Skip to content

How can a machine recognise faces ?

How can a machine recognise faces
Table of Contents
    The face is the most unique/complex feature of any human being as every person is different from one another. Humans are very good at recognising faces, though not so good at remembering names. Thanks to the generosity of human intellect, we can now teach a machine to recognise faces with the help of the face-recognition library!

    These are the steps in which the face-recognition library works:-

    1. To find faces in an image, convert the image to black and white as we don’t need a coloured image to find faces.
    2. Face landmark estimation: Train the machine to find the eyes and mouth to be roughly in the same position in the image. So that however the face is turned, the machine can detect that this is still the same person.
    3. Face encoding: The image is now sent to the pre-trained neural network so that this neural network generates 128 measurements to define a face.
    4. Face classification: The SVM classifier is then used to differentiate between these measurements and different faces.

    Fun fact: Face detection and face recognition are not one and the same. Face detection is just finding the faces. Face recognition on the other hand deals with finding a face as well as being able to tell which face belongs to who.

    Face-recognition library can not be installed directly without its dependencies. This is due to the fact that the face-recognition library was originally built using dlib and requires a C++ compiler (Visual Studio with C++ development). dlib is a C++ toolkit containing machine learning algorithms meant for real world applications. dlib was developed in C++ and requires cmake to run. Hence it is really important to follow all the guidelines as mentioned in the “Face Recognition in action” section so that you don’t get stuck with errors.

    Face Recognition in action

    We are going to implement face recognition on the infamous trio Harry Potter, Hermoine Granger and Ron Weasley from the renowned Harry Potter series as they celebrate Harry Potter’s 20th Anniversary: Return to Hogwarts. Let’s look at face recognition live in action:-

    1. Visit https://visualstudio.microsoft.com/→Downloads→Visual Studio 2022 Comminity→Free download. Download the VisualStudioSetup.exe file.
    2. Run VisualStudioSetup.exe. During installation, tick ☑ Desktop development with C++ and tick ☑ C++ CMake tools for windows. (Note: kindly check if your Windows OS is compatible with Visual Studio 2022 else Sign up with a Microsoft account and download the version of Visual Studio which is compatible with your Windows OS.)
    3. Pre-requisite for installing face-recognition libraryVisit https://www.python.org/ →Downloads→Download the latest version for Windows.
    4. Visit https://www.jetbrains.com/pycharm/. Download and install the Community free version of Pycharm.
    5. Launch Pycharm. Go to File→New project→”FaceRecognitionProject”.
    6. Go to Terminal →select Local terminal and run these commands one at a time in the same order to install all the latest versions of the dependencies:-
      pip install open-cv
      pip install numpy
      pip install cmake
      pip install dlib
      pip install face-recognition
    7. Download the People directory from here→People and add it to FaceRecognitionProject. (Note: You can add or delete one or more images to the ‘People’ directory as per your preference.)
      Harry

      Harry

      Hermione

      Hermione

      Ron

      Ron

    8. Add FaceRecognitionInAction.py from the programming section to FaceRecognitionProject and execute.
    9. Play the video Harry Potter – Return to Hogwarts Trailer Highlights (settings: quality=1080p and playback speed=0.25x) in front of the webcam of your Laptop/PC. (Note: kindly follow all the steps mentioned above to get assured results as per this post.)
    PROGRAMMING

    Program: FaceRecognitionInAction.py

    #FaceRecognitionInAction.py
    #Made by Wiztaqnia
    #Modified date 25/03/2023
    import cv2
    import numpy as np
    import face_recognition
    import os
    
    path= 'People' #'People' directory contains the images of people to be recognised in the webcam feed
    pics=[]
    NameOfPerson=[]
    myList=os.listdir(path)
    
    for i in myList:
        curImg=cv2.imread(f'{path}/{i}')                #cv2.imread(f'{current image present in the path}/{name of the image})
        pics.append(curImg)                             #import current image to pics[]
        NameOfPerson.append(os.path.splitext(i)[0])     #import text before .jpeg to NamesOfPeople
    
    def findEncodings(pics):                            #find encoding for each image in pics[]
        encodeList = []
        for img in pics:
            img=cv2.cvtColor(img,cv2.COLOR_BGR2RGB)     #convert the image from RGB to BGR format (default Color space for OpenCV is BGR)
            encode=face_recognition.face_encodings(img)[0]
            encodeList.append(encode)
        return encodeList
    
    encodeKnown=findEncodings(pics)
    
    cap=cv2.VideoCapture(0)
    while True:
        success, img = cap.read()
        imgSmall=cv2.resize(img,(0,0),None,0.25,0.25)               #reduce the size of the image from webcam feed to speed up the process
        imgSmall=cv2.cvtColor(imgSmall,cv2.COLOR_BGR2RGB)
    
        faceCurFrame=face_recognition.face_locations(imgSmall)
        encodesCurFrame=face_recognition.face_encodings(imgSmall,faceCurFrame)
    
        for encodeFace,facLoc in zip(encodesCurFrame,faceCurFrame): #find the face in webcam feed of closest match to the pics in 'People' directory
            match=face_recognition.compare_faces(encodeKnown,encodeFace)
            faceDis=face_recognition.face_distance(encodeKnown,encodeFace)
            matchIndex=np.argmin(faceDis)
            if match[matchIndex]:
                name=NameOfPerson[matchIndex].upper()
                y1,x2,y2,x1= facLoc
                y1,x2,y2,x1 = y1*4,x2*4,y2*4,x1*4                                          #convert the image from webcam feed to its original size
                cv2.rectangle(img,(x1,y1),(x2,y2),(255,255,0),2)                           #draw a cyan rectangle around the face
                cv2.rectangle(img, (x1, y2-35), (x2, y2), (255, 255, 0))                   #draw a cyan rectangle around the name of the person
                cv2.putText(img,name,(x1+6,y2-6),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,0),2) #display the name of the person
    
        cv2.imshow('Output',img)
        cv2.waitKey(1)
    

     

    OUTPUT
    video
    play-sharp-fill

    This post was inspired by https://youtu.be/sz25xxF_AVE

    References:

    For exclusive insights, tips and answers, please visit  Wiztaqnia Forum.

    Noor Fatimah I.H
    🍪⚙️
    ® 2024 Wiztaqnia | All Rights Reserved Wiztaqnia