基于OpenCV+dilb建立自己的人脸数据集及进行简单人脸识别

一、原理

参考前面的博客基于OpenCv+Python+Dlib实现简单人脸数据采集

二、代码实现

1.提取人脸

输入需要录制的人的姓名用来创建对应文件夹来保存图片,通过摄像头捕获到的图片进行人脸检测,当检测到人脸后用矩形进行标注。按下s键进行保存,ESC键盘退出。

import cv2
import dlib
import os
import sys
import random

# 存储位置
output_dir = './output/person/test'
size = 64


# 改变图片的亮度与对比度

def relight(img, light=1, bias=0):
    w = img.shape[1]
    h = img.shape[0]
    # image = []
    for i in range(0, w):
        for j in range(0, h):
            for c in range(3):
                tmp = int(img[j, i, c] * light + bias)
                if tmp > 255:
                    tmp = 255
                elif tmp < 0:
                    tmp = 0
                img[j, i, c] = tmp
    return img


# 使用dlib自带的frontal_face_detector作为我们的特征提取器
detector = dlib.get_frontal_face_detector()
# 打开摄像头 参数为输入流,可以为摄像头或视频文件
camera = cv2.VideoCapture(0)

name=input("请输入录入人的姓名:")
index = 1
ok = True
output_dir+=  '/' +name
if not os.path.exists(output_dir):
    os.makedirs(output_dir)
while ok:

    # 从摄像头读取照片
    # 读取摄像头中的图像,ok为是否读取成功的判断参数
    ok, img = camera.read()

    # 转换成灰度图像
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 使用detector进行人脸检测
    dets = detector(img_gray, 1)
    for i, d in enumerate(dets):
        x1 = d.top() if d.top() > 0 else 0
        y1 = d.bottom() if d.bottom() > 0 else 0
        x2 = d.left() if d.left() > 0 else 0
        y2 = d.right() if d.right() > 0 else 0
        # 截取人脸方框
        face = img[x1:y1, x2:y2]

        # 绘制矩形框标注人脸
        cv2.rectangle(img, tuple([x2, x1]), tuple([y2, y1]), (0, 255, 255), 2)
        print('Being processed picture %s' % index)
        # 调整图片的对比度与亮度, 对比度与亮度值都取随机数,这样能增加样本的多样性
        face = relight(face, random.uniform(0.5, 1.5), random.randint(-50, 50))

        # 重设图片大小
        face = cv2.resize(face,(size,size))

    # 展示摄像头读取到的图片
    cv2.imshow(name, img)
    key = cv2.waitKey(1)
    # ESC退出
    if key == 27:
        break
    # s保存
    elif key == 115:
        # 保存图片
        cv2.imwrite(output_dir +'/' + str(index) + '.jpg', face)
        print("save success ")
        index += 1




2.获取人脸的特征点

通过遍历存放人脸图片的文件夹来获取所有人的人脸数据,再对每个人的人脸数据进行提前特征值,最后把计算出每个人的特征均值保存到文件中。

# 从人脸图像文件中提取人脸特征存入 CSV
# Features extraction from images and save into features_all.csv

# return_128d_features()          获取某张图像的128D特征
# compute_the_mean()              计算128D特征均值

from cv2 import cv2 as cv2
import os
import dlib
from skimage import io
import csv
import numpy as np

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

# Dlib 正向人脸检测器
detector = dlib.get_frontal_face_detector()

# Dlib 人脸预测器
predictor = dlib.shape_predictor("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("dlib_face_recognition_resnet_model_v1.dat")


# 返回单张图像的 128D 特征
def return_128d_features(path_img):
    img_rd = io.imread(path_img)
    img_gray = cv2.cvtColor(img_rd, cv2.COLOR_BGR2RGB)
    # 提取人脸坐标
    faces = detector(img_gray, 1)

    # 因为有可能截下来的人脸再去检测,检测不出来人脸了
    # 所以要确保是 检测到人脸的人脸图像 拿去算特征
    if len(faces) != 0:
        # 提取人脸特征
        shape = predictor(img_gray, faces[0])
        # 通过特征点进行人脸识别
        face_descriptor = face_rec.compute_face_descriptor(img_gray, shape)
    else:
        face_descriptor = 0

    return face_descriptor


# 将文件夹中照片特征提取出来, 写入 CSV
def return_features_mean_person(path_faces_person):
    features_list_person = []
    photos_list = os.listdir(path_faces_person)
    if photos_list:
        for i in range(len(photos_list)):
            # 调用return_128d_features()得到128d特征
            features_128d = return_128d_features(path_faces_person + "/" + photos_list[i])
            # 遇到没有检测出人脸的图片跳过
            if features_128d == 0:
                i += 1
            else:
                # 把提取到的特征点加入列表
                features_list_person.append(features_128d)

    else:
        print("文件夹内图像文件为空 / Warning: No images in " + path_faces_person + '/', '\n')
        
    print("有效人脸数量:",len(features_list_person))
    
    # 计算 128D 特征的均值
    if features_list_person:
        features_mean_person = np.array(features_list_person).mean(axis=0)
    else:
        features_mean_person = '0'

    return features_mean_person


people = os.listdir(path_images_from_camera)
people.sort()
print("人名:",people)
with open("./output/features/test.csv", "w", newline="") as csvfile:
    writer = csv.writer(csvfile)
    for person in people:
        print("正在计算" + person + "特征均值...")
        features_mean_person = return_features_mean_person(path_images_from_camera + person)
        writer.writerow(features_mean_person)
        print(person + "特征均值计算完毕")
        print("dlib特征均值 :", list(features_mean_person))
        print()
    print("所有录入人脸数据存入 / Save all the features of faces registered into: /output/features/test.csv")


人脸识别

通过摄像头捕获到的图片来检测人脸,把检测到人脸数据取特征值和已有的数据集进行对比,找到误差范围内的人。如果找不到则显示unknown,否则显示对应人的名字。

# 摄像头实时人脸识别
import os
import winsound  # 系统音效
from playsound import playsound  # 音频播放
import dlib  # 人脸处理的库 Dlib
import csv  # 存入表格
import time
import sys
import numpy as np  # 数据处理的库 numpy
from cv2 import cv2 as cv2  # 图像处理的库 OpenCv
import pandas as pd  # 数据处理的库 Pandas

# 人脸识别模型,提取128D的特征矢量
# face recognition model, the object maps human faces into 128D vectors
# Refer this tutorial: http://dlib.net/python/index.html#dlib.face_recognition_model_v1
facerec = dlib.face_recognition_model_v1(
    "dlib_face_recognition_resnet_model_v1.dat")


# 计算两个128D向量间的欧式距离
# compute the e-distance between two 128D features
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 = "./output/features/test.csv"
csv_rd = pd.read_csv(path_features_known_csv, header=None)

# 用来存放所有录入人脸特征的数组
# the array to save the features of faces in the database
features_known_arr = []

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

# Dlib 检测器和预测器
# The detector and predictor will be used
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')

# 创建 cv2 摄像头对象

cap = cv2.VideoCapture(0)

# cap.set(propId, value)
# 设置视频参数,propId 设置的视频参数,value 设置的参数值
cap.set(3, 480)

# cap.isOpened() 返回 true/false 检查初始化是否成功
# when the camera is open
while cap.isOpened():

    flag, img_rd = cap.read()
    kk = cv2.waitKey(1)

    # 取灰度
    img_gray = cv2.cvtColor(img_rd, cv2.COLOR_RGB2GRAY)

    # 人脸数 faces
    faces = detector(img_gray, 0)

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

    # 存储当前摄像头中捕获到的所有人脸的坐标/名字
    # the list to save the positions and names of current faces captured
    pos_namelist = []
    name_namelist = []

    # 按下 ESC 键退出
    if kk == 27:
        break
    else:
        # 检测到人脸 when face detected
        if len(faces) != 0:
            # 获取当前捕获到的图像的所有人脸的特征,存储到 features_cap_arr
            # get the features captured and save into features_cap_arr
            features_cap_arr = []
            for i in range(len(faces)):
                shape = predictor(img_rd, faces[i])
                features_cap_arr.append(facerec.compute_face_descriptor(img_rd, shape))

            # 遍历捕获到的图像中所有的人脸
            # traversal all the faces in the database
            for k in range(len(faces)):
                print("##### camera person", k + 1, "#####")
                # 让人名跟随在矩形框的下方
                # 确定人名的位置坐标
                # 先默认所有人不认识,是 unknown
                # set the default names of faces with "unknown"
                name_namelist.append("unknown")

                # 每个捕获人脸的名字坐标 the positions of faces captured
                pos_namelist.append(
                    tuple([faces[k].left(), int(faces[k].bottom() + (faces[k].bottom() - faces[k].top()) / 4)]))

                # 对于某张人脸,遍历所有存储的人脸特征
                # for every faces detected, compare the faces in the database
                e_distance_list = []
                for i in range(len(features_known_arr)):
                    # 如果 person_X 数据不为空
                    if str(features_known_arr[i][0]) != '0.0':
                        print("with person", str(i + 1), "the e distance: ", end='')
                        e_distance_tmp = return_euclidean_distance(features_cap_arr[k], features_known_arr[i])
                        print(e_distance_tmp)
                        e_distance_list.append(e_distance_tmp)
                    else:
                        # 空数据 person_X
                        e_distance_list.append(999999999)
                # 找出最接近的一个人脸数据是第几个
                # Find the one with minimum e distance
                similar_person_num = e_distance_list.index(min(e_distance_list))
                print("Minimum e distance with person", int(similar_person_num) + 1)

                # 计算人脸识别特征与数据集特征的欧氏距离
                # 距离小于0.4则标出为可识别人物
                if min(e_distance_list) < 0.4:
                    # 这里可以修改摄像头中标出的人名
                    # Here you can modify the names shown on the camera
                    # 1、遍历文件夹目录
                    folder_name = './output/person/test'
                    # 最接近的人脸
                    sum = similar_person_num + 1
                    key_id = 1  # 从第一个人脸数据文件夹进行对比
                    # 获取文件夹中的文件名:1wang、2zhou、3...
                    file_names = os.listdir(folder_name)
                    for name in file_names:
                        # print(name+'->'+str(key_id))
                        if sum == key_id:
                            # winsound.Beep(300,500)# 响铃:300频率,500持续时间
                            name_namelist[k] = name[0:]  # 人名删去第一个数字(用于视频输出标识)
                        key_id += 1
                else:
                    print("Unknown person")

                # 矩形框
                # draw rectangle
                for kk, d in enumerate(faces):
                    # 绘制矩形框
                    cv2.rectangle(img_rd, tuple([d.left(), d.top()]), tuple([d.right(), d.bottom()]), (0, 255, 255), 2)
                print('\n')

            # 在人脸框下面写人脸名字
            # write names under rectangle
            for i in range(len(faces)):
                cv2.putText(img_rd, name_namelist[i], pos_namelist[i], font, 0.8, (0, 255, 255), 1, cv2.LINE_AA)

    print("Faces in camera now:", name_namelist, "\n")

    # cv2.putText(img_rd, "Press 'q': Quit", (20, 450), font, 0.8, (84, 255, 159), 1, cv2.LINE_AA)
    cv2.putText(img_rd, "Face Recognition", (20, 40), font, 1, (0, 0, 255), 1, cv2.LINE_AA)
    cv2.putText(img_rd, "Visitors: " + str(len(faces)), (20, 100), font, 1, (0, 0, 255), 1, cv2.LINE_AA)

    # 窗口显示 show with opencv
    cv2.imshow("camera", img_rd)

# 释放摄像头 release camera
cap.release()

# 删除建立的窗口 delete all the windows
cv2.destroyAllWindows()

三、结果

1.提取人脸效果

提取人脸过程,按下s键进行保存
在这里插入图片描述
在这里插入图片描述
提取到的图片
在这里插入图片描述

2.提取特征值

在这里插入图片描述
在这里插入图片描述

3.人脸预测

在这里插入图片描述

四、总结

通过建立人脸数据集,再提取检测到的人脸特征值,对两者进行比对,误差在一定范围内就认为是同一个人。

五、参考

添加链接描述

  • 6
    点赞
  • 79
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
人脸识别门禁系统是一种基于人脸识别技术的智能门禁系统,其可通过对人脸进行采集、识别和比对,实现对门禁的控制和管理。本文将详细阐述基于python+openCV+dlib+mysql的人脸识别门禁系统的设计与实现。 一、技术选型 本系统主要采用以下技术: 1. Python:作为主要编程语言,用于实现整个系统的逻辑控制和算法设计。 2. OpenCV:作为图像处理库,用于实现人脸检测、特征提取和人脸识别等核心功能。 3. Dlib:作为人脸识别库,用于实现人脸特征点检测和人脸识别等功能。 4. MySQL:作为数据库系统,用于存储人脸特征和相关信息。 二、系统设计 本系统主要包括以下功能模块: 1. 人脸采集模块:用于采集用户的人脸图像,并将其存储到本地或远程数据库中。 2. 人脸检测模块:用于检测人脸区域,提取人脸特征,并将其存储到数据库中。 3. 人脸识别模块:用于识别用户的人脸特征,并与数据库中的人脸特征进行比对,以确定用户身份。 4. 门禁控制模块:根据用户身份结果,控制门禁的开关。 5. 数据库管理模块:用于管理数据库中的人脸特征和相关信息。 三、系统实现 1. 人脸采集模块 人脸采集模块主要是通过摄像头对用户的人脸进行拍摄和保存。代码如下: ```python import cv2 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() cv2.imshow("capture", frame) if cv2.waitKey(1) & 0xFF == ord('q'): #按q键退出 cv2.imwrite("face.jpg", frame) #保存人脸图像 break cap.release() cv2.destroyAllWindows() ``` 2. 人脸检测模块 人脸检测模块主要是通过OpenCV中的CascadeClassifier类进行人脸检测,再通过Dlib中的shape_predictor类进行人脸特征点检测和特征提取。代码如下: ```python import cv2 import dlib detector = dlib.get_frontal_face_detector() #人脸检测器 predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") #特征点检测器 img = cv2.imread("face.jpg") #读取人脸图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #转换为灰度图像 faces = detector(gray, 0) #检测人脸 for face in faces: landmarks = predictor(gray, face) #检测特征点 for n in range(68): x = landmarks.part(n).x y = landmarks.part(n).y cv2.circle(img, (x, y), 2, (0, 255, 0), -1) #绘制特征点 cv2.imshow("face", img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 3. 人脸识别模块 人脸识别模块主要是通过Dlib中的face_recognition类进行人脸特征提取和比对。代码如下: ```python import face_recognition known_image = face_recognition.load_image_file("known_face.jpg") #读取已知的人脸图像 unknown_image = face_recognition.load_image_file("unknown_face.jpg") #读取待识别的人脸图像 known_encoding = face_recognition.face_encodings(known_image)[0] #提取已知人脸的特征 unknown_encoding = face_recognition.face_encodings(unknown_image)[0] #提取待识别人脸的特征 results = face_recognition.compare_faces([known_encoding], unknown_encoding) #比对人脸特征 if results[0]: print("Match") else: print("No match") ``` 4. 门禁控制模块 门禁控制模块主要是通过GPIO控制门禁的开关。代码如下: ```python import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.OUT) GPIO.output(11, GPIO.HIGH) #开门 time.sleep(5) #等待5秒 GPIO.output(11, GPIO.LOW) #关门 GPIO.cleanup() #清理GPIO资源 ``` 5. 数据库管理模块 数据库管理模块主要是通过MySQLdb模块实现对MySQL数据库的连接和操作,包括新建数据库、新建表、插入数据、查询数据等。代码如下: ```python import MySQLdb #连接数据库 conn = MySQLdb.connect(host="localhost", user="root", passwd="123456", db="test", charset="utf8") #新建表 cursor = conn.cursor() sql = "CREATE TABLE `face` (`id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) NOT NULL, `encoding` text NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;" cursor.execute(sql) #插入数据 name = "张三" encoding = "0.1,0.2,0.3,0.4" sql = "INSERT INTO `face` (`name`, `encoding`) VALUES (%s, %s)" cursor.execute(sql, (name, encoding)) conn.commit() #查询数据 sql = "SELECT * FROM `face` WHERE `name`=%s" cursor.execute(sql, (name,)) result = cursor.fetchone() print(result) cursor.close() conn.close() ``` 四、总结 本文主要介绍了基于python+openCV+dlib+mysql的人脸识别门禁系统的设计与实现。该系统主要采用了Python作为主要编程语言,OpenCVDlib作为图像处理和人脸识别库,MySQL作为数据库系统。通过对这些技术的应用,实现了人脸采集、检测、识别和门禁控制等核心功能。该系统可以应用于各类场景的门禁控制和身份验证,具有较高的实用价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值