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_encodin

本文介绍了如何利用python的face_recognition库进行人脸识别。首先安装opencv和face_recognition库,然后通过摄像头捕获视频,实时检测并识别人脸。通过加载预先准备好的人脸图像,将捕获的帧中的人脸与已知人脸进行比对,匹配成功后在屏幕上显示匹配到的名字。代码中还包含了处理中文字符的支持。
最低0.47元/天 解锁文章
4457

被折叠的 条评论
为什么被折叠?



