运行环境macos m2芯片 Python 3.11.7,python3.9都能通过,windows系统应该也是一样的效果
import dlib
import cv2
import matplotlib.pyplot as plt
# Load the image
image_path = 'path_to_your_image.jpg' # Replace with the path to your image
image = cv2.imread(image_path)
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Initialize the dlib face detector
detector = dlib.get_frontal_face_detector()
# Initialize the shape predictor
predictor_path = 'shape_predictor_68_face_landmarks.dat' # Replace with the path to your .dat file
predictor = dlib.shape_predictor(predictor_path)
# Detect faces in the image
faces = detector(gray)
# Loop over each detected face
for face in faces:
# Get the landmarks
landmarks = predictor(gray, face)
# Loop over each landmark point
for n in range(0, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(image, (x, y), 2, (0, 255, 0), -1)
# Convert the image back to RGB for displaying with matplotlib
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Display the image with detected landmarks
plt.imshow(image_rgb)
plt.axis('off')
plt.show()
dlib 的亮点之一是其高精度的人脸检测和面部特征点检测功能,包含了用于检测 68 个面部特征点的预训练模型。
shape_predictor_68_face_landmarks.dat下载地址
链接: https://pan.baidu.com/s/1MhwLavLU2uWJnNo13gTLjQ 提取码: 5555
--来自百度网盘超级会员v9的分享