python人脸识别源码
时间: 2023-10-12 08:19:41 浏览: 47
以下是基于Python的人脸识别源代码示例,使用OpenCV和dlib库:
```python
import cv2
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(0, 68):
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(img, (x, y), 2, (0, 255, 0), -1)
# 显示图像
cv2.imshow("Output", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
此代码将使用dlib中的68个特征点模型来检测人脸并绘制出来。请确保在代码中指定正确的模型文件路径。