1、face_recognition 人脸识别学习
- 检测人脸:face_locations = face_recognition.face_locations(frame)
返回值 (top,bottom,right,left)
人脸检测,可用opencv替换:
优势:pc端速度提高了一倍,树莓派上提高了接近九倍(520ms - 67ms)
#opencv的人脸检测
def detect_byOpencv(self,img):
start_detect_time = time.time()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.3, 5)
loctions = []
for (x,y,w,h) in faces:
top = y
right = x + w
bottom = y + h
left = x
loctions.append((top, right, bottom, left))
print ("opencv detect:",time.time()-start_detect_time)
return loctions
def face_locations(img, number_of_times_to_upsample=1, model="hog"):
返回图像中人脸边界框的数组
param img:图像(作为numpy数组)
upsample以查找面。数字越大,面越小
param model:要使用的人脸检测模型。““Hog”在CPU上不太准确,但速度更快。CNN“更准确。GPU/CUDA加速的深度学习模型(如有)。默认值为“hog”
返回:按css(上、右、下、左)顺序找到的面位置的元组列表
- 面部信息编码:face_encodings = face_recognition.face_encodings(frame, face_locations)
对脸部信息进行编码,比较耗时;pc端37ms,树莓派端570ms
def face_encodings(face_image, known_face_locations=None, num_jitters=1):
给定一个图像,返回图像中每个面的128维面编码
param face image:包含一个或多个面的图像.
param known face locations:可选-每个面的边界框(如果您已经知道)
计算编码时要对人脸重新采样多少次。越高越准确,但越慢(即100是100的速度越慢)
返回:128维面编码列表(图像中的每个面一个)
:param face image:包含一个或多个面的图像
:param known face locations:可选-每个面的边界框(如果您已经知道)。
计算编码时要对人脸重新采样多少次。越高越准确,但越慢(即100是100的速度越慢)
:返回:128维面编码列表(图像中的每个面一个)
- 识别人脸
tolerance越低,越准,但unknown率增高
for i, v in enumerate(total_face_encoding):
match = face_recognition.compare_faces([v], face_encoding, tolerance=0.35)#0.5
def compare_faces(known_face_encodings, face_encoding_to_check, tolerance=0.6):
将人脸编码列表与候选编码进行比较,看它们是否匹配。
param known_face_encodings:已知人脸编码列表
param face_encoding_to_check:要与列表进行比较的单个面编码
param tolerance:面与面之间的距离应视为匹配。越低越严格。0.6是典型的最佳性能
RETURN:一个真/假值列表,指示要检查的已知面部编码与面部编码匹配
- 下载匹配模板并编码
后面的编码方式需相同!
total_face_encoding.append(
face_recognition.face_encodings(
face_recognition.load_image_file(path + "/" + fn))[0])
face_recognition -