首页 > 其他分享 >人脸识别Mediapipe+Dlib实现1 : N式人脸匹配

人脸识别Mediapipe+Dlib实现1 : N式人脸匹配

时间:2023-11-06 21:11:56浏览次数:32  
标签:Mediapipe 人脸识别 features cv2 face detection mp import Dlib

一、前言

本来是想进行加速运行的。最后只快了两三帧哈哈哈哈。本次采用的方法是用Mediapipe进行人脸识别,配合使用Dlib进行特征提取以及特征向量转化。目前可以实现1 : n式人脸匹配,随便改改就行n : m了,就是效率太低了
实现原帖【---->点击这里<----】

二、难点以及之后可继续学习的领域

  1. 人脸特征向量128D转化的模型训练
  2. 人脸特征提取的训练、更加熟练使用Mediapipe
  3. 完善模型添加人员的label、无需重新训练模型的方式
  4. 使用C++重构任务

三、实现目录

  1. 进行录入数据集
  2. 数据集的向量提取、模型训练
  3. 模型使用

四、实现基本原理

  1. 使用Mediapipe进行人脸识别,识别后利用Dlib的模型,进行64个关键点的特征提取。
  2. 然后使用Dlib的模型将64个关键点的信息转化为一个128维的空间特征向量,使用多张图片得到每个人的特征向量均值。
  3. 最后计算特征向量的欧氏距离。

五、开做吧!

(1)进行截取数据集,这里使用Mediapipe进行获取数据集

import cv2
import os
import mediapipe as mp
import time
import sys

    # 使用mediapipe进行初始化
mp_face_detection = mp.solutions.face_detection
mp_holistic = mp.solutions.holistic
face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5)
holistic = mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5)
def get_face(userName, imageCnt):

    # 采集的张数
    IMGCOUNT = imageCnt
    # 采集的人名
    user = userName
    # 存储位置
    output_dir = './faces/' + user #这里填编号+人名
    size = 128 # 图片边长
    # 如果文件没有就加一个
    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    # 打开摄像头
    camera = cv2.VideoCapture(0)
    start_time = time.time()
    index = 1
    counter = 0
    # 获取视频宽度
    frame_width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH))
    # 获取视频高度
    frame_height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT))
    fps = camera.get(cv2.CAP_PROP_FPS) #视频平均帧率

    while True:
        if index <= IMGCOUNT:
            # print('开始保存第 {} 张图片'.format(index))
            ref, image = camera.read()

            # 使用mediapipe进行提取特征
            # 转换BGR到RGB格式并进行人脸关键点检测
            image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            results = face_detection.process(image_rgb)
            hresults = holistic.process(image_rgb)

            # 获取roi区域
            roi = []
            if results.detections:
                for detection in results.detections:
                    bbox = detection.location_data.relative_bounding_box
                    h, w, _ = image.shape
                    x, y, w, h = int(bbox.xmin * w), int(bbox.ymin * h), \
                        int(bbox.width * w), int(bbox.height * h)

                    roi = image[y: y + h, x: x + w]
                     # 保存图片
                    face = cv2.resize(roi, (size, size))
                    cv2.imwrite(output_dir + '/' + str(index) + '.jpg', face)
                    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
                    index += 1

            # 计算视频帧率
            counter += 1
            if (time.time() - start_time) != 0:  # 实时显示帧数

                fps = int(counter / (time.time() - start_time))
                cv2.putText(image, "FPS {:d}".format(fps), (50, 50),
                            cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255),
                            3)
                cv2.imshow('frame', image)
                counter = 0
                start_time = time.time()
            if cv2.waitKey(1) == ord('q'): break
        else:
            break

    camera.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    get_face('wxz', 100)

(2)进行将数据集提取特征点并转化为特征向量,并把特征均值放到csv

import cv2
import os
import mediapipe as mp
import csv
import dlib
import numpy as np


# 返回单张图像的 128D 特征
def return_128d_features(path_img):
    img_rd = cv2.imread(path_img)
    image_rgb = cv2.cvtColor(img_rd, cv2.COLOR_BGR2RGB)
    results = face_detection.process(image_rgb)
    hresults = holistic.process(image_rgb)
    print("%-40s %-20s" % ("检测到人脸的图像 / image with faces detected:", path_img), '\n')
    # 因为有可能截下来的人脸再去检测,检测不出来人脸了
    # 所以要确保是 检测到人脸的人脸图像 拿去算特征
    if results.detections:

        bbox = results.detections[0].location_data.relative_bounding_box
        h, w, _ = img_rd.shape
        x, y, w, h = int(bbox.xmin * w), int(bbox.ymin * h), \
            int(bbox.width * w), int(bbox.height * h)
        rect = dlib.rectangle(x, y, x + w, y + h)
        shape = predictor(image_rgb, rect)
        face_descriptor = face_rec.compute_face_descriptor(image_rgb, shape)
    else:
        face_descriptor = 0
        print("no face")


    return face_descriptor


# 将文件夹中照片特征提取出来, 写入 CSV
def return_features_mean_personX(path_faces_personX, name):
    features_list_personX = []
    photos_list = os.listdir(path_faces_personX)
    if photos_list:
        for i in range(len(photos_list)):
            with open("./facedata/" + name + str(i) + ".csv", "w", # 保存每一张图片的特征的路径
                      newline="") as csvfile:
                writer = csv.writer(csvfile)
                # 调用return_128d_features()得到128d特征
                print("%-40s %-20s" % ("正在读的人脸图像 / image to read:", path_faces_personX + "/" + photos_list[i]))
                features_128d = return_128d_features(path_faces_personX + "/" + photos_list[i])
                try:
                    writer.writerow(features_128d)
                except:
                    continue
                # 遇到没有检测出人脸的图片跳过
                if features_128d != 0:
                    features_list_personX.append(features_128d)
    else:
        print("文件夹内图像文件为空 / Warning: No images in " + path_faces_personX + '/', '\n')

        # 计算 128D 特征的均值
    # N x 128D -&gt; 1 x 128D
    if features_list_personX:
        features_mean_personX = np.array(features_list_personX).mean(axis=0) # 特征均值
    else:
        features_mean_personX = '0'

    return features_mean_personX


# Dlib 人脸预测器
predictor = dlib.shape_predictor("./model/shape_predictor_68_face_landmarks.dat")

# Dlib 人脸识别模型
# Face recognition model, the object maps human faces into 128D vectors
face_rec = dlib.face_recognition_model_v1(
    "./model/dlib_face_recognition_resnet_model_v1.dat")

mp_face_detection = mp.solutions.face_detection
mp_holistic = mp.solutions.holistic
face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5)
holistic = mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5)

# 要读取人脸图像文件的路径
path_images_from_camera = "./faces/"

people = os.listdir(path_images_from_camera)
people.sort()


with open("features_all.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    for person in people:
        print("##### " + person + " #####")
        # Get the mean/average features of face/personX, it will be a list with a length of 128D
        features_mean_personX = return_features_mean_personX(path_images_from_camera + person, person)
        writer.writerow(features_mean_personX)
        print("特征均值 / The mean of features:", list(features_mean_personX))
        print('\n')

    print("所有人脸信息保存完毕")

(3)模型的使用

幸运的是这个结果还是非常准的,因为是通过特征的向量欧氏距离进行计算的。

import os
import dlib          # 人脸处理的库 Dlib
import csv # 存入表格
import time
import numpy as np   # 数据处理的库 numpy
import cv2        # 图像处理的库 OpenCv
import pandas as pd  # 数据处理的库 Pandas
import mediapipe as mp

# 导入特征向量模型
facerec = dlib.face_recognition_model_v1("./model/dlib_face_recognition_resnet_model_v1.dat")

# 计算两个128D向量间的欧式距离
def return_euclidean_distance(feature_1, feature_2):
    feature_1 = np.array(feature_1)
    feature_2 = np.array(feature_2)
    dist = np.sqrt(np.sum(np.square(feature_1 - feature_2)))
    return dist

# 处理存放所有人脸特征的 csv
path_features_known_csv = "features_all.csv"
csv_rd = pd.read_csv(path_features_known_csv, header=None)

# 用来存放所有录入人脸特征的数组
features_known_arr = []

# 读取已知人脸数据
for i in range(csv_rd.shape[0]):
    features_someone_arr = []
    for j in range(0, len(csv_rd.loc[i, :])):
        features_someone_arr.append(csv_rd.loc[i, :][j])
    features_known_arr.append(features_someone_arr)
print("Faces in Database:", len(features_known_arr))

# 有哪些人
file_names = os.listdir("./faces")

# 使用mediapipe进行初始化
mp_face_detection = mp.solutions.face_detection
mp_holistic = mp.solutions.holistic
face_detection = mp_face_detection.FaceDetection(min_detection_confidence=0.5)
holistic = mp_holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5)

# dlib的特征提取
predictor = dlib.shape_predictor('./model/shape_predictor_68_face_landmarks.dat')

cap = cv2.VideoCapture(0)
cap.set(3, 480)

while cap.isOpened():
    flag, img_rd = cap.read()

    # 取灰度
    image_rgb = cv2.cvtColor(img_rd, cv2.COLOR_BGR2RGB)
    results = face_detection.process(image_rgb)
    # 人脸数 faces

    # 待会要写的字体 font to write later
    font = cv2.FONT_HERSHEY_COMPLEX

    pos_namelist = []
    name_namelist = []

    if cv2.waitKey(1) == ord('q'):
        break
    # 如果识别到了人脸
    if results.detections:
        features_cap_arr = []

        bbox = results.detections[0].location_data.relative_bounding_box
        h, w, _ = img_rd.shape
        x, y, w, h = int(bbox.xmin * w), int(bbox.ymin * h), \
            int(bbox.width * w), int(bbox.height * h)

        rect = dlib.rectangle(x, y, x + w, y + h)
        shape = predictor(img_rd, rect)
        features_cap_arr.append(facerec.compute_face_descriptor(img_rd, shape))  #调用dlib的库


        pos_namelist.append(tuple([x, y - 5]))
        # 对于某张人脸,遍历所有存储的人脸特征
        e_distance_list = []

        for i in range(len(features_known_arr)):
            # 如果 person_X 数据不为空
            if str(features_known_arr[i][0]) != '0.0':
                e_distance_tmp = return_euclidean_distance(features_cap_arr[0], features_known_arr[i]) #计算两者距离
                e_distance_list.append(e_distance_tmp)
            else:
                # 空数据 person_X
                e_distance_list.append(999999999)


        # 找出最接近的一个人脸数据是第几个
        print(e_distance_list)
        similar_person_num = e_distance_list.index(min(e_distance_list)) # 距离最小的是第几个
        if min(e_distance_list) < 0.4: # 如果距离小于4
            name_namelist.append(file_names[similar_person_num])
        else:
            name_namelist.append("unknow")

        cv2.rectangle(image_rgb, (x, y), (x + w, y + h), (0, 255, 255), 2)
        cv2.putText(image_rgb, name_namelist[0], pos_namelist[0], font, 0.8, (0, 255, 255), 1, cv2.LINE_AA)
        image_rgb = cv2.cvtColor(image_rgb, cv2.COLOR_RGB2BGR)
        cv2.imshow("camera", image_rgb)

# 释放摄像头 release camera
cap.release()
# 删除建立的窗口 delete all the windows
cv2.destroyAllWindows()

标签:Mediapipe,人脸识别,features,cv2,face,detection,mp,import,Dlib
From: https://www.cnblogs.com/wxzcch/p/17813720.html

相关文章

  • ai换脸可以突破人脸识别吗?,详细的介绍!
    AI换脸技术,通常被称为深度伪造(deepfake)技术,是使用人工智能算法,特别是深度学习的方法,如卷积神经网络(CNN)或生成对抗网络(GAN),来替换视频或图像中人物的脸部的技术。这种技术可以创建非常逼真的视频或图片,以至于肉眼很难辨别真伪。在讨论AI换脸能否突破人脸识别之前,我们首先需要理解人......
  • 基于开源模型搭建实时人脸识别系统(五):人脸跟踪
    继续填坑,之前已经讲了人脸检测,人脸检测是定位出画面中人脸的位置,理论上把检测到的人脸进行提特征就能做人脸识别了,不过直接这样做是有缺陷,一是存在很大的资源浪费,毕竟同一个人出现在画面,我们实际上应该只需要做一次识别就知道他的身份(理想情况下),而不需要每一帧都去做;二是如果对每......
  • 人脸识别平台批量导入绑定设备的一种方法
        因为原先平台绑定设备是通过一个界面进行人工选择绑定或一个人一个人绑定设备。如下: 但有时候需要在几千个里选择出几百个,那这种方式就不大现实了,需要另外一种方法。 目前相到可以通过导入批量数据进行绑定的方式。一、前端主要是显示选择文件与设备<template><d......
  • QT连接OpenCV库实现人脸识别
    QT连接OpenCV库实现人脸识别_opencvqt人脸识别-CSDN博客 #include"mainwindow.h"#include<QApplication>#include<opencv2/opencv.hpp>#include<QMessageBox>usingnamespacestd;usingnamespacecv;intmain(intargc,char*argv[]){QAp......
  • 基于 AdaFace 提供适合低质量人脸识别的人脸特征向量输出服务
    写在前面工作原因,简单整理理解不足小伙伴帮忙指正对每个人而言,真正的职责只有一个:找到自我。然后在心中坚守其一生,全心全意,永不停息。所有其它的路都是不完整的,是人的逃避方式,是对大众理想的懦弱回归,是随波逐流,是对内心的恐惧——赫尔曼·黑塞《德米安》简单介绍通过AdaFace提......
  • 基于开源模型搭建实时人脸识别系统(四):人脸质量
    续人脸识别实战之基于开源模型搭建实时人脸识别系统(三):人脸关键点、对齐模型概览与模型选型_CodingInCV的博客-CSDN博客不论对于静态的人脸识别还是动态的人脸识别,我们都会面临一个问题,就是输入的人脸图像的质量可能会很差,比如人脸角度很大,人脸很模糊,人脸亮度很亮或很暗。这些质量......
  • 【PRC】鲁棒跨域伪标记和对比学习的无监督域自适应NIR-VIS人脸识别 Robust Cross-Doma
    【该文章为杨学长的文章,膜拜】 探索跨领域数据中的内在关系并学习领域不变表示 由于需要在低光照条件下实现24h的人脸识别,近红外加可见光的(NIR-VIS)人脸识别受到了更多的关注。但是数据标注是一个难点。该文章提出了RobustcrossdomainPseudo-labelingandContrastivelear......
  • 人脸识别
    “比奇堡双钻”项目选题一、项目目标及意义1.1项目目标1.2项目意义二、项目可行性分析2.1规模及难度2.2人员2.3成本预算2.3.1硬件和软件成本2.3.2培训成本2.3.3人力成本2.3.4数据收集和存储成本2.4时间要求2.5风险三、项目计划3.1软件开发模型3.2时间安排3.3人员......
  • Lnton羚通视频分析算法平台工地劳务实名制人脸识别管理方案
    Lnton羚通的算法算力云平台是一款优秀的解决方案,具有突出的特点。它提供高性能、高可靠性、高可扩展性和低成本的特性,使用户能够高效地执行复杂计算任务。此外,平台还提供丰富的算法库和工具,并支持用户上传和部署自定义算法,提升了平台的灵活性和个性化能力。在建筑工地场景中,施工人......
  • 监控汇聚/视频融合平台EasyCVR人脸识别功能应用的方案分享
    EasyCVR国标视频融合云平台采用端-边-云一体化架构,具备高效的视频接入、汇聚、管理、处理和分发等功能。该平台部署简单、轻量灵活,能够支持多种协议和设备类型的接入,包括GB28181、RTSP、Onvif、海康SDK、Ehome、大华SDK、RTMP推流等。在视频能力方面,平台支持视频直播、录像、回放......