Site icon Machine Learning Projects

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

Machine Learning Projects

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

Below is an illustration of 68 Face Landmarks.

68 landmarks

So without any further due, let’s do it…

Install dlib

conda install -c conda-forge dlib

Code for Face Landmarks Detection…

from imutils import face_utils
import dlib
import cv2
 

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

cap = cv2.VideoCapture(0)
 
while True:
    # Getting out image by webcam 
    _, image = cap.read()
    # Converting the image to gray scale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        
    # Get faces into webcam's image
    rects = detector(gray, 0)
    
    # For each detected face, find the landmark.
    for (i, rect) in enumerate(rects):
        # Make the prediction and transfom it to numpy array
        shape = predictor(gray, rect)
        shape = face_utils.shape_to_np(shape)
    
        # Draw on our image, all the finded cordinate points (x,y) 
        for (x, y) in shape:
            cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
    
    # Show the image
    cv2.imshow("Output", image)
    
    if cv2.waitKey(5) & 0xFF == 27:
        break

cv2.destroyAllWindows()
cap.release()

Results

68 Face Landmark Result

5 Face Landmark Result

Folders Hierarchy…

Download Files

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 by 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.

Exit mobile version