本代码使用了face_recognition三方库,它是需要numpy、dlib、cmake作支撑的。主要是用键值对存放图片和它对应的名字,最后打开摄像头,比对原有存放的人脸,用pyttsx3语音库来报出识别对象的名字。
下一步准备改用数据库存放姓名和照片,实现完毕后再与django对接实现web端的人脸识别。
以下是现在完成的代码,“下一步”还没有实现:
import cv2
import pyttsx3
import face_recognition
import numpy as np
#创建人脸检测级联分类器对象实例
face_cascade = cv2.CascadeClassifier("C:\\Users\\TIM\\Anaconda3\\Library\\etc\\haarcascades\\haarcascade_frontalface_default.xml")
#创建人眼检测级联分类器实例
#eye_cascade = cv2.CascadeClassifier("E:\\pycharm\\untitled3.6\\Lib\\site-packages\\cv2\\data\\haarcascade_eye.xml")
image = face_recognition.load_image_file(r'C:\Users\TIM\testface\c.jpg')
image2 = face_recognition.load_image_file(r'C:\Users\TIM\testface\b.jpg')
image3 = face_recognition.load_image_file(r'C:\Users\TIM\testface\a.jpg')
face_encoding = face_recognition.face_encodings(image)[0]
face_encoding2 = face_recognition.face_encodings(image2)[0]
face_encoding3 = face_recognition.face_encodings(image3)[0]
known_face_encodings = {
'王英杰': face_encoding3,
'费霞云': face_encoding2,
'田越': face_encoding,
}
# known_face_encodings = [face_encoding]
camera = cv2.VideoCapture(0)
while True:
#参数ret 为True 或者False,代表有没有读取到图片
#第二个参数frame表示截取到一帧的图片
ret, frame = camera.read()
# frame = cv2.imread(r'C:\Users\TIM\c.jpg')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray, 1.5, 3)
for (x, y, w, h) in face:
# 绘制矩形框,颜色值的顺序为BGR,即矩形的颜色为蓝色
img = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = frame[y:y + h, x:x + w]
# 在检测到的人脸区域内检测眼睛
#eyes = eye_cascade.detectMultiScale(roi_gray)
#for (ex, ey, ew, eh) in eyes:
#cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('wang', frame)
k = cv2.waitKey(1)
if k == ord('s'):
rgb_frame = frame[:, :, ::-1]
# 获取画面中的所有人脸位置及人脸特征码
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
# 对获取的每个人脸进行识别比对
for (top, right, bottom, left), face_encoding in list(zip(face_locations, face_encodings)):
print(face_encoding)
# 对其中一个人脸的比对结果(可能比对中人脸库中多个人脸)
for face in known_face_encodings:
print([np.array(known_face_encodings[face])])
matches = face_recognition.compare_faces([np.array(known_face_encodings[face])], face_encoding, tolerance=0.47)
# 默认只认为是比对中的第一个人脸.
print(matches)
if True in matches:
first_match_index = matches.index(True)
engine = pyttsx3.init()
engine.say('你好,{}'.format(face))
engine.runAndWait()
else:
engine = pyttsx3.init()
engine.say('你是谁,你不是{}'.format(face))
engine.runAndWait()
if k == ord('q'):
break
camera.release()