face_landmarks_shape_predictor_81.dat 是一个深度学习模型文件,用于面部关键点检测,能够精准定位人脸上的81个特征点,广泛应用于人脸识别和姿态估计等领域。
dlib人脸特征库分类器使用81个点进行面部识别,并包含示例代码以通过摄像头识别人脸。
```python
import cv2
import dlib
from skimage import io
import numpy as np
# 使用特征提取器get_frontal_face_detector
detector = dlib.get_frontal_face_detector()
# 使用dlib的81点模型,利用作者训练好的预测器进行特征检测。
predictor = dlib.shape_predictor(shape_predictor_81_face_landmarks.dat)
cap=cv2.VideoCapture(0)
while True:
ret, img=cap.read()
# 识别人脸
dets = detector(img, 1)
for k, d in enumerate(dets):
print(f第{k+1}个人脸d的坐标:left: {d.left()}, right: {d.right()}, top: {d.top()}, bottom: {d.bottom()})
# 计算人脸面积
width = d.right() - d.left()
heigth = d.bottom() - d.top()
print(f人脸面积为:{(width * heigth)})
# 利用预测器进行特征点检测。
shape = predictor(img, d)
landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
cv2.rectangle(img,(d.left(),d.top()),(d.right(),d.bottom()),(0,255,0),1)
# 在图像上绘制特征点
for num in range(shape.num_parts):
cv2.circle(img, (shape.parts()[num].x, shape.parts()[num].y), 3, (0,255,0), -1)
cv2.imshow(img, img)
if cv2.waitKey(1) & 0xFF == ord(q):
break
```
这段代码使用了`dlib`库来检测人脸和面部特征点,并通过摄像头进行实时的人脸识别。