Face landmarks detection – Opencv with Python
We’re going to see in this video how to detect the facial landmarks using the Dlib library with Opencv and Python.
Source code:
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")
while True:
_, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
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)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
12 Comments
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Problem facing installing dlib in my python. Please help
try conda install -c conda-forge dlib
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.
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 .