In this lesson, we will see how to identify our face in real-time through Face landmarks detection with OpenCV and Python. We will also be able to define the position of 68 points in our face.
Before going into detail on how to detect the facial landmarks using the Dlib library with Opencv and Python , I recommend following the tutorial on how to install dlib on windows how to install dlib on windows.
Face landmarks detection some code
The first important step for our Face landmarks detection project with OpenCV and Python is to import the necessary libraries for use.
Let’s move on to calling the shape_predictor_68_face_landmarks.dat file which will be used by our script to identify the points in our face. The video stream is still missing, which in my case comes from the webcam.

import cv2
import numpy as np
import dlib
cap = cv2.VideoCapture(0)
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
As we surely already know, OpenCV parses the video stream Frame by Frame so we use a While loop to apply the processing.
while True:
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
The images must first be transformed with COLOR_BGR2GRAY and then we get the object that contains the points of our face.
Face landmarks detection points extraction
Now only the code remains for extracting the coordinates and drawing the points found. It might seem complicated but thanks to the dlib library all this can be done with a few lines of code.
for face in faces:
x1 = face.left()
y1 = face.top()
x2 = face.right()
y2 = face.bottom()
#cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 3)
landmarks = predictor(gray, face)
for n in range(0, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(frame, (x, y), 4, (255, 0, 0), -1)
I hope the explanation was useful to you and surely if you download the ready-to-use code you will be able to understand the concept even better. In any case, I refer you to the official OpenCV document: Face landmarks detection Opencv .

Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Developers to build efficient computer vision software.
Problem facing installing dlib in my python. Please help
try conda install -c conda-forge dlib
Try downloading and installing cmake
https://stackoverflow.com/questions/41912372/dlib-installation-on-windows-10
Goto pypi.org/simple/dlib
Install a wheel file from there according to your system and install that file using
pip install
After downloading i am trying to import in pycharm but it shows an error that couldn’t import import dlib. Please solve..
first you need to update anaconda and you need to install cmake then conda install -c conda-forge dlib you can install by typing.
Hi
How can I cut both eye and mouth to show in separate window using landmark points?
Hi Neha,
Please check this series https://pysource.com/2019/01/07/eye-detection-gaze-controlled-keyboard-with-python-and-opencv-p-1/
You will learn how to cut both eyes. and you can use the same process for the mouth.
Hi
Thank you. I got everything. It is a really good and get deep understanding.
Hello, I get this error: RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat
please advise.
Have you fixed this error yet?
Hi, I recently ran into this error too, any suggestions and guidance to fixing this issue.
Thank you.
You have to download the .dat file and (http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2 ) and place in the same directory as the python script – or update the python script with the path to the download.
I do it but it don’t work
RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat
[ WARN:0] global D:\Build\OpenCV\opencv-4.1.2\modules\videoio\src\cap_msmf.cpp (674) SourceReaderCB::~SourceReaderCB terminating async callback
Pysource
2 weeks ago
hi, you need to download this file: https://github.com/AKSHAYUBHAT/TensorFace/blob/master/openface/models/dlib/shape_predictor_68_face_landmarks.dat
and put it on the same folder where you have your python file
still it is not working.
AttributeError: module ‘dlib’ has no attribute ‘get_frontal_face_detector’
Can u please explain me in resolving this issue
hi, I got this error–> AttributeError: module ‘dlib’ has no attribute ‘get_frontal_face_detector’
any help please?
how to get this file which name is: shape_predictor_68_face_landmarks.dat
Hi,
http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2
Hello , Thanks .
I cant get shape_predictor_68_face_landmarks.dat to work in pycharm.
Please help, my computer is giving the following error:
RuntimeError Traceback (most recent call last)
in
6
7 detector = dlib.get_frontal_face_detector()
—-> 8 predictor = dlib.shape_predictor(“shape_predictor_68_face_landmarks.dat”)
9
10 while True:
RuntimeError: Unable to open shape_predictor_68_face_landmarks.dat
I’m getting this error AttributeError: module ‘dlib’ has no attribute ‘get_frontal_face_detector’
Even I’m getting this attribute error of dlib.Did anyone with the solution please
I did not understand how dat file works. Is it originally a .py file or totally different? How can I build my own
shape_predictor_68_face_landmarks.dat file to fully understand how it works?
Thank You
Hi thank you for your good work. i am getting below error. would you advise ? i am new to this dilb
Traceback (most recent call last):
File “C:/Users/User/Desktop/py3 face detection/Database.py”, line 30, in
cv2.imshow(“Frame”, frame)
cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:651: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function ‘cvShowImage’
dlib was successfully installed. However, when I used dlib.get_frontal_face_detector(), I got an attribute error as follows:
AttributeError: module ‘dlib’ has no attribute ‘get_frontal_face_detector’
I searched on StackOverflow and github, but didn’t find a satisfactory solution.
Any kind of help would be appreciated. Thank you!
you needed to update c++ distribution ,update dotnet and install visual studio
how did u solve this ?
even i also have the same error.
AttributeError: module ‘dlib’ has no attribute ‘get_frontal_face_detector’
Can u please explain me in resolving this issue
hi i want to detect only point no. 3 and 15 out of 68 how can i do that
Hi
I don’t speak english sorry
mon probleme ce situe sur “_” du “_, frame = cap.read()” AttributeError:’NoneType’
someone help me please!
Thanks you