人脸识别技术在现代社会中扮演着越来越重要的角色,从安全监控到个性化用户体验,它的身影无处不在。在本博客中,我们将探讨如何使用face_recognition
库来实现一个视频流中的实时人脸识别。
概述
本文致力于实现人脸检测项目,该项目不仅能够从视频流中实时检测和识别人脸,还能进一步判断人脸是否张嘴。这项额外的功能有助于区分视频中的人脸是真实存在的还是图片、视频剪辑等伪造内容。举例来说,您可以参照本文提供的示例,为系统添加新的动作检测功能,如眨眼和转头等动作。在进行人脸验证时,您可以随机组合一组动作要求用户执行。如果用户未能在规定时间内完成这些动作,系统将判定人脸验证失败。这样的机制增强了验证过程的安全性和可靠性。
以下内容将详细介绍项目的四个核心部分:构建人脸数据库、实现张嘴检测功能、进行人脸的实时检测与识别,以及将检测结果以可视化的方式呈现。这些环节共同构成了一个全面的人脸验证解决方案,旨在提供高效、准确的识别服务。
准备人脸图片库
我的人脸图片库的文件结构为一级目录为图片库名称,二级目录为对应的人名,三级目录为人名对应的人脸图片(注意:图片中只能存在该人一张脸,路径建议不要有中文)。图片可以是.jpg,.png格式。
制作人脸数据库的makeFaceEncodingLib.py
makeFaceEncodingLib.py
是一个用于创建人脸编码数据库的脚本。它通过分析一组已知身份的图片,生成每个人脸的特征编码,并将这些编码存储在数据库中,以便后续的识别过程使用。
import os
import face_recognition
import ast
import numpy as np
import json
class FaceEncodingLib:
def __init__(self,lib_path):
directory = os.path.dirname(lib_path)
if not os.path.exists(directory):
# 如果目录不存在,则创建目录
os.makedirs(directory)
if not os.path.exists(lib_path):
# 如果文件不存在,则创建文件
open(lib_path, 'a').close()
# print(f"文件 '{lib_path}' 已创建。")
self.lib_path = lib_path
self.face_encodings = {}
def make_face_lib(self, face_folder):
for person_name in os.listdir(face_folder):
person_folder = os.path.join(face_folder, person_name)
encodings = []
for image_file in os.listdir(person_folder):
image_path = os.path.join(person_folder, image_file)
image = face_recognition.load_image_file(image_path)
encoding = face_recognition.face_encodings(image)[0] #每张图片只有一个人脸
encodings.append(encoding.tolist())
self.face_encodings[person_name] = encodings
with open(self.lib_path, 'w') as file:
json.dump(self.face_encodings, file, indent=4)
def load_face_lib(self):
with open(self.lib_path, 'r') as file:
self.face_encodings = json.load(file)
# print(f"已加载 '{self.lib_path}'。")
def add_face_to_lib(self, face_folder):
self.load_face_lib()
new_names = []
for person_name in os.listdir(face_folder):
if person_name in self.face_encodings:
continue
else:
new_names.append(person_name)
person_folder = os.path.join(face_folder, person_name)
encodings = []
for image_file in os.listdir(person_folder):
image_path = os.path.join(person_folder, image_file)
image = face_recognition.load_image_file(image_path)
encoding = face_recognition.face_encodings(image)[0] #每张图片只有一个人脸
encodings.append(encoding.tolist())
self.face_encodings[person_name] = encodings
with open(self.lib_path, 'w') as file:
json.dump(self.face_encodings, file, indent=4)
if new_names:
print(f"新增{new_names}数据已写入 '{self.lib_path}'。")
# libpath = "/home/zhoukang/GithubProject/face_reg_master/face_encoding_lib/lib.txt"
# face_folder = "/home/zhoukang/dataset/image/known"
# faceEncodingLib = FaceEncodingLib(libpath)
# faceEncodingLib.make_face_lib(face_folder)
生成的人脸数据库文件内容如下,不需要提前创建空文件,代码会检查指定路径下是否有文件,如果没有,会自动创建:
face_encoding_lib.txt
{
"AoBaMa": [
[
-0.04698127135634422,
0.11568425595760345,
0.07694509625434875,
0.012189071625471115,
0.003552430309355259,
-0.015561307780444622,
-0.10225330293178558,
-0.08539304882287979,
0.17924216389656067,
-0.1594679206609726,
0.2602607309818268,
0.06947591155767441,
-0.19648624956607819,
-0.13786761462688446,
0.0800618827342987,
0.11740974336862564,
-0.18707014620304108,
-0.09171440452337265,
-0.10898447036743164,
-0.07599669694900513,
-0.030940856784582138,
0.012973425909876823,
0.11444678157567978,
0.055909279733896255,
-0.12910205125808716,
-0.4061811566352844,
-0.04442097991704941,
-0.14777310192584991,
-0.0025415634736418724,
-0.1710074096918106,
-0.09442616254091263,
0.03824620321393013,
-0.15249015390872955,
-0.057147182524204254,
-0.020095467567443848,
-0.0017956988885998726,
-0.01978248730301857,
-0.0010614925995469093,
0.21213343739509583,
0.042255908250808716,
-0.14911791682243347,
0.05194048583507538,
-0.03325088322162628,
0.22742372751235962,
0.26070332527160645,
0.0851793885231018,
0.02810218557715416,
-0.031441718339920044,
0.15681341290473938,
-0.2645900845527649,
0.04644179344177246,
0.1558387279510498,
0.07189097255468369,
0.03015509434044361,
0.10073961317539215,
-0.1961713582277298,
-0.07440462708473206,
0.06522148102521896,
-0.08218405395746231,
0.04435121268033981,
0.01078705582767725,
-0.05520990490913391,
0.007997343316674232,
0.035012856125831604,
0.22852802276611328,
0.11237428337335587,
-0.1355539858341217,
-0.02520371600985527,
0.12039197236299515,
-0.08181384950876236,
-0.009902472607791424,
0.025467559695243835,
-0.16078172624111176,
-0.19973687827587128,
-0.24500666558742523,
0.046205684542655945,
0.3267871141433716,
0.1516401767730713,
-0.18830375373363495,
-0.04017651081085205,
-0.16449512541294098,
0.027364101260900497,
0.030816862359642982,
0.052031975239515305,
-0.07667862623929977,
-0.08185460418462753,
-0.059264302253723145,
0.029098335653543472,
0.14400523900985718,
0.06174206733703613,
-0.06401927024126053,
0.20638079941272736,
-0.03457768261432648,
0.09527434408664703,
0.007656199857592583,
0.006947179790586233,
-0.1283358782529831,
-0.04836852476000786,
-0.16992738842964172,
-0.008588451892137527,
-0.03997272253036499,
-0.0167672298848629,
-0.016716938465833664,
0.11703445762395859,
-0.17535124719142914,
0.12042904645204544,
-0.003188052447512746,
-0.008692312054336071,
0.01684371568262577,
0.12957079708576202,
-0.10339426249265671,
-0.043619245290756226,
0.06909753382205963,
-0.2405875325202942,
0.2362385243177414,
0.22492918372154236,
0.05780321732163429,
0.16976143419742584,
0.061278171837329865,
0.03657356649637222,
-0.029212625697255135,
-0.009622236713767052,
-0.14237217605113983,
-0.10185068845748901,
0.044171467423439026,
0.04137367010116577,
0.05731340870261192,
-0.007031507324427366
],
[
-0.09676194936037064,
0.12042399495840073,
0.05106069892644882,
-0.051098428666591644,
0.028641145676374435,
-0.005404854193329811,
-0.08761100471019745,
-0.11248961091041565,
0.1929294466972351,
-0.08010173588991165,
0.22121968865394592,
0.10412289947271347,
-0.1979617029428482,
-0.19429588317871094,
0.056788913905620575,
0.0877159