1.pip install opencv
2.pip install face_recognition
3.通过摄像头实时在获取的帧上进行人脸识别(较卡顿)
facerecognition.py
# -*- coding: UTF-8 -*-
import face_recognition
import cv2
import os
import ft2
#中文支持,加载微软雅黑字体
ft = ft2.put_chinese_text('msyh.ttf')
# 获取摄像头# 0(默认)
video_capture = cv2.VideoCapture(0)
# 加载待识别人脸图像并识别。
basefacefilespath ="images"#faces文件夹中放待识别任务正面图,文件名为人名,将显示于结果中
baseface_titles=[] #图片名字列表
baseface_face_encodings=[] #识别所需人脸编码结构集
#读取人脸资源
for fn in os.listdir(basefacefilespath): #fn 人脸文件名
baseface_face_encodings.append(face_recognition.face_encodings(face_recognition.load_image_file(basefacefilespath+"/"+fn))[0])
fn=fn[:(len(fn)-4)]
baseface_titles.append(fn)#
while True:
# 获取一帧视频
ret, frame = video_capture.read()
# 人脸检测,并获取帧中所有人脸编码
face_locations = face_recognition.face_locations(frame)
face_encodings = face_recognition.face_encodings(frame, face_locations)
# 遍历帧中所有人脸编码
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# 与baseface_face_encodings匹配否?
for i,v in enumerate(basef