目标检测算法中训练人脸识别检测数据集,用 dlib 库和 facenet 模型来进行人脸识别,并使用 PyQt5建立构建基于深度学习卷积神经网络的人脸识别系统/小区门禁系统

目标检测算法中训练人脸识别检测数据集,用 dlib 库和 facenet 模型来进行人脸识别,并使用 PyQt5建立构建基于深度学习卷积神经网络的人脸识别系统/小区门禁系统


以下文字及代码仅供同学参考。
在这里插入图片描述


预实现功能:


支持人脸录入,人脸识别,人脸管理
深度学习,卷积网络
使用dlib库自己facenet网络模型
精简的界面,本地数据存储人脸信息
anaconda与pycharm
带人脸管理功能
在这里插入图片描述
构建一个人脸识别与管理系统,用于小区门禁系统,需要实现以下功能:

  1. 人脸录入:用户可以录入新的人脸信息。
  2. 人脸识别:实时或通过图片进行人脸识别。
  3. 人脸管理:管理已录入的人脸信息,包括删除、修改等操作。
  4. 界面设计:精简的界面,支持本地数据存储。

使用 dlib 库和 facenet 模型来进行人脸识别,并使用 PyQt5 构建 GUI 界面。以下是详细的步骤和代码示例。

1. 环境搭建

安装依赖
# 创建虚拟环境(可选)
conda create -n face_recognition python=3.8
conda activate face_recognition

# 安装依赖库
pip install dlib opencv-python-headless numpy PyQt5

2. 数据准备

创建一个文件夹来存储人脸数据:

mkdir dataset
cd dataset
mkdir known_faces unknown_faces

3. 人脸录入

编写一个脚本来录入人脸数据:

import cv2
import os
import dlib
from imutils import face_utils

def capture_face(name):
    cap = cv2.VideoCapture(0)
    detector = dlib.get_frontal_face_detector()
    
    count = 0
    while True:
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = detector(gray)
        
        for face in faces:
            x, y, w, h = face.left(), face.top(), face.width(), face.height()
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
            
            if count < 10:  # Capture 10 images
                cv2.imwrite(f"dataset/known_faces/{name}_{count}.jpg", frame[y:y+h, x:x+w])
                count += 1
        
        cv2.imshow("Capture Face", frame)
        if cv2.waitKey(1) & 0xFF == ord('q') or count >= 10:
            break
    
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    name = input("Enter the name of the person: ")
    capture_face(name)

4. 人脸识别

编写一个脚本进行人脸识别:

import cv2
import os
import numpy as np
import dlib
from imutils import face_utils
from facenet_pytorch import MTCNN, InceptionResnetV1

def load_embeddings():
    embeddings = {}
    model = InceptionResnetV1(pretrained='vggface2').eval()
    mtcnn = MTCNN()
    
    for filename in os.listdir("dataset/known_faces"):
        img_path = os.path.join("dataset/known_faces", filename)
        img = cv2.imread(img_path)
        img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        boxes, _ = mtcnn.detect(img_rgb)
        
        if boxes is not None:
            box = boxes[0]
            img_cropped = img[int(box[1]):int(box[3]), int(box[0]):int(box[2])]
            img_emb = mtcnn(img_cropped.unsqueeze(0))
            embedding = model(img_emb).detach().numpy()[0]
            embeddings[filename.split("_")[0]] = embedding
    
    return embeddings

def recognize_face(frame, embeddings):
    model = InceptionResnetV1(pretrained='vggface2').eval()
    mtcnn = MTCNN()
    
    boxes, _ = mtcnn.detect(frame)
    if boxes is not None:
        for box in boxes:
            x, y, w, h = int(box[0]), int(box[1]), int(box[2]) - int(box[0]), int(box[3]) - int(box[1])
            face_img = frame[y:y+h, x:x+w]
            face_img = cv2.cvtColor(face_img, cv2.COLOR_BGR2RGB)
            face_img = mtcnn(face_img.unsqueeze(0))
            
            if face_img is not None:
                embedding = model(face_img).detach().numpy()[0]
                min_distance = float('inf')
                recognized_name = "Unknown"
                
                for name, emb in embeddings.items():
                    distance = np.linalg.norm(embedding - emb)
                    if distance < min_distance:
                        min_distance = distance
                        recognized_name = name
                
                cv2.putText(frame, recognized_name, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
                cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
    
    return frame

if __name__ == "__main__":
    embeddings = load_embeddings()
    
    cap = cv2.VideoCapture(0)
    
    while True:
        ret, frame = cap.read()
        frame = recognize_face(frame, embeddings)
        cv2.imshow("Face Recognition", frame)
        
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    cap.release()
    cv2.destroyAllWindows()

在这里插入图片描述

5. 构建 GUI 应用程序

使用 PyQt5 构建一个简单的 GUI 应用程序:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QWidget, QFileDialog
from PyQt5.QtGui import QPixmap
import cv2
import os
import numpy as np
import dlib
from imutils import face_utils
from facenet_pytorch import MTCNN, InceptionResnetV1

class FaceRecognitionApp(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()
        self.model = InceptionResnetV1(pretrained='vggface2').eval()
        self.mtcnn = MTCNN()
        self.embeddings = self.load_embeddings()

    def initUI(self):
        self.setWindowTitle("人脸识别与管理系统")
        self.setGeometry(100, 100, 800, 600)

        layout = QVBoxLayout()

        self.image_label = QLabel(self)
        self.image_label.setText("请选择一张图片进行检测")
        self.image_label.setAlignment(Qt.AlignCenter)
        layout.addWidget(self.image_label)

        self.load_button = QPushButton("加载图像", self)
        self.load_button.clicked.connect(self.load_image)
        layout.addWidget(self.load_button)

        self.recognize_button = QPushButton("识别人脸", self)
        self.recognize_button.clicked.connect(self.recognize_face)
        layout.addWidget(self.recognize_button)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

    def load_image(self):
        options = QFileDialog.Options()
        file_name, _ = QFileDialog.getOpenFileName(self, "选择图像文件", "", "Images (*.png *.jpg *.jpeg)", options=options)
        if file_name:
            self.image_path = file_name
            pixmap = QPixmap(file_name)
            self.image_label.setPixmap(pixmap.scaled(640, 640))

    def recognize_face(self):
        if hasattr(self, 'image_path'):
            img = cv2.imread(self.image_path)
            img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            boxes, _ = self.mtcnn.detect(img_rgb)
            
            if boxes is not None:
                for box in boxes:
                    x, y, w, h = int(box[0]), int(box[1]), int(box[2]) - int(box[0]), int(box[3]) - int(box[1])
                    face_img = img[y:y+h, x:x+w]
                    face_img = cv2.cvtColor(face_img, cv2.COLOR_BGR2RGB)
                    face_img = self.mtcnn(face_img.unsqueeze(0))
                    
                    if face_img is not None:
                        embedding = self.model(face_img).detach().numpy()[0]
                        min_distance = float('inf')
                        recognized_name = "Unknown"
                        
                        for name, emb in self.embeddings.items():
                            distance = np.linalg.norm(embedding - emb)
                            if distance < min_distance:
                                min_distance = distance
                                recognized_name = name
                        
                        cv2.putText(img, recognized_name, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
                        cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
            
            height, width, channel = img.shape
            bytes_per_line = 3 * width
            q_img = ImageQt.Image.fromarray(img).convert("RGB").rgbSwapped()
            self.image_label.setPixmap(QPixmap.fromImage(q_img))

    def load_embeddings(self):
        embeddings = {}
        
        for filename in os.listdir("dataset/known_faces"):
            img_path = os.path.join("dataset/known_faces", filename)
            img = cv2.imread(img_path)
            img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            boxes, _ = self.mtcnn.detect(img_rgb)
            
            if boxes is not None:
                box = boxes[0]
                img_cropped = img[int(box[1]):int(box[3]), int(box[0]):int(box[2])]
                img_emb = self.mtcnn(img_cropped.unsqueeze(0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值