首页 > IT职业 > IT生活 > openCV实战项目--人脸考勤
# 检测人脸,获取 68 个关键点,获取特征描述符
def faceRegister(faceId=1, userName='default', interval=3, faceCount=3, resize_w=700, resize_h=400):
'''
faceId: 人脸 ID
userName: 人脸姓名
faceCount: 采集该人脸图片的数量
interval: 采集间隔
'''
cap = cv2.VideoCapture(0)
# 人脸检测模型
hog_face_detector = dlib.get_frontal_face_detector()
# 关键点 检测模型
shape_detector = dlib.shape_predictor('./weights/shape_predictor_68_face_landmarks.dat')
# resnet 模型
face_descriptor_extractor = dlib.face_recognition_model_v1('./weights/dlib_face_recognition_resnet_model_v1.dat')
while True:
ret, frame = cap.read()
# 镜像
frame = cv2.flip(frame,1)
# 转为灰度图
frame_gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
# 检测人脸
detections = hog_face_detector(frame,1)
for face in detections:
# 人脸框坐标 左上和右下
l, t, r, b =跟单网gendan5.com face.left(), face.top(), face.right(), face.bottom()
# 获取 68 个关键点
points = shape_detector(frame,face)
# 绘制关键点
for point in points.parts():
cv2.circle(frame,(point.x,point.y),2,(0,255,0),1)
# 绘制矩形框
cv2.rectangle(frame,(l,t),(r,b),(0,255,0),2)
cv2.imshow("face",frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows
faceRegister()
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946337/viewspace-2919463/ ,如需转载,请注明出处,否则将追究法律责任。