1、创建人脸采集
import numpy as np
import cv2 as cv
model_path = "../models/yunet.onnx"
faceDetector = cv.FaceDetectorYN.create(model_path, "", input_size=(640, 480))
capture = cv.VideoCapture(74)
capture.set(3, 640)
capture.set(4, 480)
while True:
ret, frame = capture.read()
if ret is not True:
break
faces = faceDetector.detect(frame)
if faces[1] is not None:
for idx, face in enumerate(faces[1]):
coords = face[:-1].astype(np.int32)
cv.rectangle(frame,(coords[0], coords[1]), (coords[0] + coords[2], coords[1] + coords[3]), (255, 0, 0), thickness=2)
cv.imshow('photo-collection-demo', frame)
c = cv.waitKey(1)
if c == 27:
break
elif c == 32:
face_id = input('请输入人脸ID,按回车键后系统自动保存视频帧图片==> ')
print('ID输入成功,正在保存帧照片......')
cv.imwrite(r"../images/" + face_id + ".jpg", frame)
print('照片保存成功!\n按esc可退出视频\n按空格键可继续保存帧图片\n')
else:
cv.imshow('photo-collection-demo', frame)
c = cv.waitKey(1)
if c == 27:
break
elif c == 32:
print('当前未检测到人脸,无法保存视频帧图片')
2、创建人脸识别程序:
import numpy as np
import cv2 as cv
import os
face_detection_model = "../models/yunet.onnx"
face_recognition_model = "../models/face_recognizer_fast.onnx"
images_path = "../images"
faceDetector = cv.FaceDetectorYN.create(face_detection_model, "", input_size=(640, 480)) # 初始化FaceRecognizerYN
recognizer = cv.FaceRecognizerSF.create(face_recognition_model, "") # 初始化FaceRecognizerSF
cosine_similarity_threshold = 0.363
l2_similarity_threshold = 1.128
def Gets_Facial_Features(images_path):
images_feature_list = []
ID_list = []
for image_name in os.listdir(images_path):
ID_list.append(image_name.split('.')[0])
image = cv.imread(images_path + '/' + image_name)
faces1 = faceDetector.detect(image)
aligned_face1 = recognizer.alignCrop(image, faces1[1][0])
image_feature = recognizer.feature(aligned_face1)
images_feature_list.append(image_feature)
return ID_list, images_feature_list
if __name__ == '__main__':
capture = cv.VideoCapture(74)
capture.set(3, 640) # 设置摄像头的帧的高为640
capture.set(4, 480) # 设置摄像头的帧的宽为480
ID_list, images_feature_list = Gets_Facial_Features(images_path)
while True:
ret, frame = capture.read()
if ret is not True:
print('摄像头未打开')
break
frame_faces = faceDetector.detect(frame)
if frame_faces[1] is not None:
for idx, face in enumerate(frame_faces[1]):
coords = face[:-1].astype(np.int32)
cv.rectangle(frame, (coords[0], coords[1]), (coords[0] + coords[2], coords[1] + coords[3]), (255, 0, 0), thickness=2)
cv.circle(frame, (coords[4], coords[5]), 2, (255, 0, 0), thickness=2)
cv.circle(frame, (coords[6], coords[7]), 2, (0, 0, 255), thickness=2)
cv.circle(frame, (coords[8], coords[9]), 2, (0, 255, 0), thickness=2)
cv.circle(frame, (coords[10], coords[11]), 2, (255, 0, 255), thickness=2)
cv.circle(frame, (coords[12], coords[13]), 2, (0, 255, 255), thickness=2)
aligned_face = recognizer.alignCrop(frame, frame_faces[1][idx])
frame_feature = recognizer.feature(aligned_face)
for index, image_feature in enumerate(images_feature_list):
cosine_score = recognizer.match(frame_feature, image_feature, 0)
l2_score = recognizer.match(frame_feature, image_feature, 1)
if (cosine_score >= cosine_similarity_threshold):
cv.putText(frame, ID_list[index], (coords[0] + 5, coords[1] - 5), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
i = 1
break
elif (l2_score <= l2_similarity_threshold):
cv.putText(frame, ID_list[index], (coords[0] + 5, coords[1] - 5), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
i = 1
break
cv.imshow('RP3588_face_DEMO', frame)
c = cv.waitKey(1)
if c == 27:
break
else:
cv.putText(frame, 'face is not detected', (1, 16), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
cv.imshow('photo-collection-demo', frame)
c = cv.waitKey(1)
if c == 27:
break
3、打开采集程序,如果有检测到有人脸,会出现图框,按下空格键就可以采集预识别的图像:
4、输入人名后,可以继续或者按ESC退出采集程序:
5、打开人脸识别程序,如果是录入了人脸的就会被识别: