目标检测算法中训练人脸识别检测数据集,用 dlib 库和 facenet 模型来进行人脸识别,并使用 PyQt5建立构建基于深度学习卷积神经网络的人脸识别系统/小区门禁系统
以下文字及代码仅供同学参考。
预实现功能:
支持人脸录入,人脸识别,人脸管理
深度学习,卷积网络
使用dlib库自己facenet网络模型
精简的界面,本地数据存储人脸信息
anaconda与pycharm
带人脸管理功能

构建一个人脸识别与管理系统,用于小区门禁系统,需要实现以下功能:
- 人脸录入:用户可以录入新的人脸信息。
- 人脸识别:实时或通过图片进行人脸识别。
- 人脸管理:管理已录入的人脸信息,包括删除、修改等操作。
- 界面设计:精简的界面,支持本地数据存储。
使用 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
509

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



