Python人脸关键点检测代码示例
导入所需的库
import cv2 import numpy as np import dlib
加载人脸关键点检测器
detector = dlib.get_frontal_face_detector() predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
加载图片
img = cv2.imread('test.jpg')
将图片转换为灰度图像
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
检测人脸
faces = detector(gray)
遍历每张脸并绘制关键点
for face in faces: landmarks = predictor(gray, face) for n in range(68): x = landmarks.part(n).x y = landmarks.part(n).y cv2.circle(img, (x, y), 2, (0, 255, 0), -1)
显示图片
cv2.imshow('Facial Landmarks', img) cv2.waitKey(0) cv2.destroyAllWindows()
原文地址: http://www.cveoy.top/t/topic/lUF3 著作权归作者所有。请勿转载和采集!