Facial Landmark Detection in Real-Time using OpenCV & Dlib

Pavan Kunchala
Analytics Vidhya
Published in
2 min readJan 26, 2021

Facial landmarks can help us predict many things it could be used for emotion detection , distress detection or even whether a driver is falling asleep or not when driving . We are going to use a pre-trained model for detection of landmarks around the face .

Importing stuff

Let’s import the required libraries such as OpenCV , dlib & numpy

Define the Landmarks

There are a total of 68 landmarks that we are going to plot on this model (we can also alternate it to detect only 5 landmarks for real-time purposes in edge devices )

Draw Shapes

Now let’s define functions to draw the points and shapes of landmarks

Model & Predictor

Now let’s initialize the model ( we have two of them one for detecting 68 landmarks and other for 5 landmarks I am going to comment out 5 landmarks one). We will also use dlib’s shape predictor function for detecting the landmarks in the given face

Start the Cam

You can use Cv2.VideoCapture(0) for accessing the webcam in your computer but for convenience I have used this model on a video and we will be saving the output but you can also use it in real-time using the above function.

Run & Save

Let’s run this model using the given video input and I will be saving the output as output.mp4 but you can also see the output in real-time using cv2.imshow() function

You can find the code for the blog from here

PS: If you have any doubts you can mail me here , you can contact me on my linkedin from here and you can check out my other codes(it has really cool stuff) on my Github from here

I am also looking for Freelancing opportunities in the field of Deep Learning and Computer vision if you are willing to collaborate, mail me here( pavankunchalapk@gmail.com)

Have a wonderful day!

--

--

Pavan Kunchala
Analytics Vidhya

Machine learning & Computer Vision Engineer |Deep learning and Reinforcement learning enthusiast