step1:准备好opencv,numpy和face_recognition三个库
step2:准备好人脸图像
step3:利用opencv读取三张图片
step4:将图片转为特征向量,并将向量和名字添加到列表中,一一对应
step5:摄像头展示,读取摄像头图片帧
step6:将图片缩放到1/4,进行处理时候更加的迅速
step7: opencv摄像头读取的是BGR格式,这里转为face_recognition需要的RGB形式,同时将图像原图和图像中的所有人脸面部以及对应的所有特征信息放入faces_encodings列表中
step8:一一遍历该图像帧中所有人脸的面部特征,并计算特征向量的比较,compare_faces返回的是一个True和False的列表,同时获取当前人脸面部信息和数据库中的人脸面部信息的特征向量差值,即欧氏距离,姓名默认为unknown,用距离最小的图像的下标去在True和False的列表中去查看是否匹配,最终结果存放于detect_names中
step9:最后进行一个人脸检测框以及姓名标签的绘制,循环遍历一张图像中的所有人脸面部,以及一一对应的检测出的人脸的姓名,利用face_locations对应的人脸的(top, right, bottom, left)信息,进行绘制
step10:最后进行opencv的imshow显示图片,最后按q退出循环
全部代码:
效果图演示:
先来了解下face_recognition的各个函数的作用
开始进行识别:
frame1 = cv2.imread(r'images/liudehua.jpeg')
frame2 = cv2.imread(r'images/shileipeng.png')
frame3 = cv2.imread(r'images/linluocheng.jpg')
frame1_encoding = face_recognition.face_encodings(frame1)[0]
encodings.append(frame1_encoding)
names.append('liudehua')
frame2_encoding = face_recognition.face_encodings(frame2)[0]
encodings.append(frame2_encoding)
names.append('shileipeng')
frame3_encoding = face_recognition.face_encodings(frame3)[0]
encodings.append(frame3_encoding)
names.append('linluocheng')
while True:
ret, img = cap.read()
change_img = cv2.resize(img,(0,0),fx=0.25,fy=0.25)
frame_RGB = change_img[:, :, ::-1]
faces_locations = face_recognition.face_locations(frame_RGB)
faces_encodings = face_recognition.face_encodings(frame_RGB, faces_locations)
for face_encoding in faces_encodings:
matches = face_recognition.compare_faces(encodings,face_encoding)
distances = face_recognition.face_distance(encodings, face_encoding)
min_distance_index = np.argmin(distances)
name = 'unknown'
if matches[min_distance_index]:
name = names[min_distance_index]
detect_names.append(name)
for (top, right, bottom, left), name in zip(faces_locations, detect_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(img, name, (left, top - 6), font, 1.0, (255, 255, 255), 1)
cv2.imshow('test',img)
key = cv2.waitKey(1)
if key == ord('q'):
break
import face_recognition
import cv2
import numpy as np
cap = cv2.VideoCapture(0,cv2.CAP_DSHOW)
encodings = []
names = []
font = cv2.FONT_HERSHEY_SIMPLEX
detect_names=[]
class init():
frame1 = face_recognition.load_image_file(r'images/liudehua.jpeg')
frame2 = face_recognition.load_image_file(r'images/shileipeng.png')
frame3 = face_recognition.load_image_file(r'images/linluocheng.jpg')
frame1_encoding = face_recognition.face_encodings(frame1)[0]
encodings.append(frame1_encoding)
names.append('liudehua')
frame2_encoding = face_recognition.face_encodings(frame2)[0]
encodings.append(frame2_encoding)
names.append('shileipeng')
frame3_encoding = face_recognition.face_encodings(frame3)[0]
encodings.append(frame3_encoding)
names.append('linluocheng')
class show():
while True:
ret, img = cap.read()
change_img = cv2.resize(img,(0,0),fx=0.25,fy=0.25)
frame_RGB = change_img[:, :, ::-1]
faces_locations = face_recognition.face_locations(frame_RGB)
faces_encodings = face_recognition.face_encodings(frame_RGB, faces_locations)
for face_encoding in faces_encodings:
matches = face_recognition.compare_faces(encodings,face_encoding)
distances = face_recognition.face_distance(encodings, face_encoding)
min_distance_index = np.argmin(distances)
name = 'unknown'
if matches[min_distance_index]:
name = names[min_distance_index]
detect_names.append(name)
for (top, right, bottom, left), name in zip(faces_locations, detect_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(img, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(img, name, (left, top - 6), font, 1.0, (255, 255, 255), 1)
cv2.imshow('test',img)
key = cv2.waitKey(1)
if key == ord('q'):
break
if __name__ == '__main__':
init()
show()
cap.release()
cv2.destroyAllWindows()