Detect 68 Facial Landmarks Using Python (dlib + OpenCV)
------------------------------------------------------
Short: In this quick tutorial I show how to detect 68 facial landmark points using dlib and OpenCV, plus how to load the model, read an image, run the predictor and visualize the points.
Download shape_predictor_68_face_landmarks.dat: https://www.kaggle.com/datasets/sajikim/sh...
► TIMESTAMPS / CHAPTERS
00:00 Intro — What we’ll build
00:02 Import required libraries
00:07 Load Dlib predictor (shape_predictor_68_face_landmarks.dat)
00:13 Load input image (cv2.imread)
00:20 Detect faces & run predictor
00:45 Draw 68 landmark points (Output)
python full code
#import
import cv2
import dlib
Load models
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(
"C:/Users/shwet/OneDrive/Desktop/68_landmarks/shape_predictor_68_face_landmarks.dat"
)
Load image
img = cv2.imread("C:/Users/shwet/OneDrive/Desktop/68_landmarks/input.jpg")
if img is None:
print("❌ Image not found!")
exit()
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Detect faces
faces = detector(gray_img)
print("Faces detected:", len(faces))
Loop
for face in faces:
landmarks = predictor(gray_img, face)
for n in range(68):
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(img, (x, y), 2, (0, 0, 255), -1)
cv2.imshow("68 Landmarks", img)
cv2.waitKey(0)
cv2.destroyAllWindow()