python人脸识别代码
时间: 2023-07-03 20:26:58 浏览: 34
以下使用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(68):
x = landmarks.part(n).x
y = landmarks.part(n).y
cv2.circle(img, (x, y), 2, (0, 255, 0), -1)
# 绘制人脸框
x1, y1, x2, y2 = face.left(), face.top(), face.right(), face.bottom()
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
# 显示图像
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
注意:这只是一个简单的人脸检测和面部特征点检测示例,还有许多其他方法可以实现更准确的人脸识别。