Real-Time Face Landmark Detection using OpenCV in Python

The Real-Time Face Landmark Detection OpenCV Python was developed using Python OpenCV, Face detection is a computer technology being used in a variety of applications that identify human faces in digital images.

Face detection also refers to the psychological process by which humans locate and attend to faces in a visual scene.

A Face Landmark Detection OpenCV Python is the process of detecting landmarks or regions of interest (key points) on the face like Eyebrows, Eyes, Nose, and Mouth.

In this article, the system can detect the face of the human in real-time using a web camera.

About The Project

This OpenCV Python Project also includes a downloadable Python Project With Source Code for free, just find the downloadable source code below and click to start downloading.

By the way, if you are new to Python programming and don’t know what Python IDE to use, I have here a list of the Best Python IDE for Windows, Linux, and Mac OS that will suit you.

I also have here How to Download and Install the Latest Version of Python on Windows.

To start executing Real-Time Face Landmark Detection With Source Code, make sure that you have installed Python 3.9 and PyCharm on your computer.

How To Run The Face Landmark Detection using OpenCV Python With Source Code

Time needed: 5 minutes

These are the steps on how to run Real-Time Face Landmark Detection OpenCV Python With Source Code

  • Step 1: Download the given source code below.

    First, download the given source code below and unzip the source code.
    face landmarks detection download source code

  • Step 2: Import the project to your PyCharm IDE.

    Next, import the source code you’ve downloaded to your PyCharm IDE.
    face landmarks detection open project

  • Step 3: Run the project.

    Lastly, run the project with the command “py main.py”
    face landmarks detection run project

Installed Libraries

import cv2
import dlib

Complete Source Code

import cv2
import dlib

cap = cv2.VideoCapture(0)

hog_face_detector = dlib.get_frontal_face_detector()

dlib_facelandmark = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

while True:
    _, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    faces = hog_face_detector(gray)
    for face in faces:

        face_landmarks = dlib_facelandmark(gray, face)

        for n in range(0, 16):
            x = face_landmarks.part(n).x
            y = face_landmarks.part(n).y
            cv2.circle(frame, (x, y), 1, (0, 255, 255), 1)


    cv2.imshow("Face Landmarks", frame)

    key = cv2.waitKey(1)
    if key == 50:
        break
cap.release()
cv2.destroyAllWindows()

Output:

Real Time Face Landmark Detection OpenCV Python With Source Code Output
Real-Time Face Landmark Detection OpenCV Python With Source Code Output

Download the Source Code below

Summary

In this article, Left Eye Detection OpenCV Python’s most successful application of eye detection would probably be photo taking.

When you take a photo of your friends, the eye detection algorithm built into your digital camera detects where the eyes are and adjusts the focus accordingly.

Related Articles

Inquiries

If you have any questions or suggestions about Real-Time Face Landmark Detection OpenCV Python With Source Code, please feel free to leave a comment below.

Leave a Comment