Face Landmarks Detection using dlib – with source code – easy implementation – 2022

So guys in today’s blog we will implement the Face Landmarks Detection project using the dlib library. We will perform both the 68 point and 5 point detections. So without any further due.

Let’s do it…

Code for Face Landmarks Detection…

from imutils import face_utils
import numpy as np
import imutils
import dlib
import cv2

face_detector = dlib.get_frontal_face_detector()
landmark_detector = dlib.shape_predictor('shape_predictor_5_face_landmarks.dat')

cam = cv2.VideoCapture(0)

while True:
    ret,image = cam.read()
    if ret:
        image = imutils.resize(image, width=500)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        
        rects = face_detector(gray, 1)

        for (i, rect) in enumerate(rects):
            landmarks = landmark_detector(gray, rect,)
            landmarks = face_utils.shape_to_np(landmarks)
            (x, y, w, h) = face_utils.rect_to_bb(rect)
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.putText(image, f"Face {i + 1}", (x - 10, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
            for (x, y) in landmarks:
                cv2.circle(image, landmarks[-1], 1, (0, 0, 255), -1)
        cv2.imshow('Live feed',image)
    
    if cv2.waitKey(1)==27:
        break

cam.release()
cv2.destroyAllWindows()

# This was the code for Face Landmarks Detection.
  • Line 1-5 – Import required libraries.
  • Line 7 – Instantiating Face Detector using dlib.get_frontal_face_detector() which is the inbuilt function for face detection in dlib.
  • Line 8 – Instantiating Landmarks detector using dlib.shape_predictor(path) where the path to the landmark detection file is passed as an argument.
    • Use path to‘shape_predictor_5_face_landmarks.dat’ for 5 point detection.
    • Use path to‘shape_predictor_68_face_landmarks.dat’ for 68 point detection.
  • Line 10 – Instantiate VideoCapture object to access the webcam.
  • Line 13 – Read the image from the webcam.
  • Line 14 – Proceed, only when the webcam is returning anything.
  • Line 15 – Resize the image.
  • Line 16 – Convert the image from BGR to Grayscale.
  • Line 18 – Detect faces in the image. It will return rectangles.
  • Line 20 – Traverse in faces detected.
  • Line 21 – Detect landmarks.
  • Line 22 – Convert these landmarks to np array.
  • Line 23 – Convert face rectangle which our face_detector returned into bounding box coordinates.
  • Line 24-25 – Draw a rectangle and put text on rectangle around face detected.
  • Line 26-27 – Draw the landmarks.
  • Line 28 – Show the results.
  • Line 30-31 – If anyone hits the ESC key, break the code and exit.
  • Line 33-34 – Release the webcam and destroy all open windows.
Face Landmarks Detection
68 landmarks

Folders Hierarchy

Face Landmarks Detection

Download Source Code for Face Landmarks Detection…

Download 5 point dlib file…

Download 68 point dlib file…

Do let me know if there’s any query regarding Face Landmarks Detection by contacting me on email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Read my previous post: HARRY’S INVISIBILITY CLOAK 

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net.

Leave a Comment

Your email address will not be published.