Python实现人脸识别算法并封装为类库(续)

引言

人脸识别技术在许多领域都有广泛的应用,如安全监控、门禁系统、智能设备等。本文将介绍如何使用Python实现一个人脸识别系统,并将其封装为一个类库。我们将逐步扩展和完善这个类库,增加代码优化、人脸照片存储到数据库、对特殊场景(如戴口罩、眼镜)的优化,以及灵活的识别距离设置。

1. 基本人脸识别实现

1.1 安装依赖

首先,我们需要安装一些必要的库:

pip install face_recognition opencv-python psycopg2-binary dlib

1.2 基本类库实现

我们创建一个 FaceRecognition 类,实现基本的人脸识别功能。

import cv2
import face_recognition
import numpy as np
import psycopg2
from psycopg2 import sql
from PIL import Image, ImageDraw
import configparser

class FaceRecognition:
    def __init__(self, db_config, config_file='config.ini'):
        """
        初始化FaceRecognition类。

        :param db_config: 数据库配置
        :param config_file: 配置文件路径
        """
        self.db_config = db_config
        self.config = configparser.ConfigParser()
        self.config.read(config_file)
        self.tolerance = float(self.config.get('FaceRecognition', 'tolerance'))
        self.model = self.config.get('FaceRecognition', 'model')
        self.known_face_encodings, self.known_face_names, self.tolerances = self.load_faces_from_db()

    def load_faces_from_db(self):
        """
        从数据库加载已知人脸信息。

        :return: 已知人脸的编码、名称和识别距离
        """
        known_face_encodings = []
        known_face_names = []
        tolerances = []

        conn = psycopg2.connect(**self.db_config)
        cursor = conn.cursor()
        cursor.execute("SELECT name, encoding, tolerance FROM faces")
        rows = cursor.fetchall()
        for row in rows:
            name, encoding_str, tolerance = row
            encoding = np.fromstring(encoding_str, dtype=float, sep=' ')
            known_face_encodings.append(encoding)
            known_face_names.append(name)
            tolerances.append(tolerance)

        cursor.close()
        conn.close()

        return known_face_encodings, known_face_names, tolerances

    def add_face_to_db(self, image_path, name, tolerance=None):
        """
        将新的人脸信息添加到数据库。

        :param image_path: 人脸图像的路径
        :param name: 人脸的名称
        :param tolerance: 识别距离
        """
        if tolerance is None:
            tolerance = self.tolerance

        # 加载图片
        image = face_recognition.load_image_file(image_path)
        # 编码人脸
        face_encoding = face_recognition.face_encodings(image)[0]
        encoding_str = ' '.join(map(str, face_encoding))

        conn = psycopg2.connect(**self.db_config)
        cursor = conn.cursor()
        query = sql.SQL("INSERT INTO faces (name, encoding, image_path, tolerance) VALUES (%s, %s, %s, %s)")
        cursor.execute(query, (name, encoding_str, image_path, tolerance))
        conn.commit()
        cursor.close()
        conn.close()

    def real_time_face_recognition(self, camera_index=0):
        """
        实现实时人脸识别。

        :param camera_index: 摄像头索引
        """
        # 打开摄像头
        video_capture = cv2.VideoCapture(camera_index)

        while True:
            # 读取一帧
            ret, frame = video_capture.read()
            if not ret:
                continue

            # 将帧转换为RGB
            rgb_frame = frame[:, :, ::-1]

            # 检测人脸位置
            face_locations = face_recognition.face_locations(rgb_frame, model=self.model)
            if not face_locations:
                continue

            # 编码人脸
            face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

            # 遍历检测到的人脸
            for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
                name = "Unknown"
                min_distance = float('inf')
                best_match_index = -1

                for i, (known_encoding, known_name, known_tolerance) in enumerate(zip(self.known_face_encodings, self.known_face_names, self.tolerances)):
                    # 计算欧氏距离
                    distance = face_recognition.face_distance([known_encoding], face_encoding)[0]
                    if distance < min_distance and distance <= known_tolerance:
                        min_distance = distance
                        name = known_name
                        best_match_index = i

                # 在帧上绘制矩形和标签
                cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
                cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
                font = cv2.FONT_HERSHEY_DUPLEX
                cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)

            # 显示结果
            cv2.imshow('Video', frame)

            # 按q键退出
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        # 释放摄像头
        video_capture.release()
        cv2.destroyAllWindows()

    def train_model(self, training_data):
        """
        训练人脸识别模型。

        :param training_data: 训练数据,格式为 [(image_path, name, tolerance)]
        """
        for image_path, name, tolerance in training_data:
            self.add_face_to_db(image_path, name, tolerance)

# 示例用法
if __name__ == "__main__":
    # 数据库配置
    db_config = {
   
        'dbname': 'your_dbname',
        'user': 'your_user',
        'password': 'your_password',
        'host': 'localhost',
        'port': '5432'
    }

    # 初始化FaceRecognition类
    face_recognition = FaceRecognition(db_config)

    # 添加新人脸
    face_recognition.add_face_to_db('path/to/new_face.jpg', 'New Face', tolerance=0.6)

    # 启动实时人脸识别
    face_recognition.real_time_face_recognition(camera_index=0)

    # 训练模型
    training_data = [
        ('path/to/training_face_1.jpg', 'Training Face 1', 0.6),
        ('path/to/training_face_2.jpg', 'Training Face 2', 0.5),
        # 添加更多训练数据
    ]
    face_recognition.train_model(training_data)

2. 代码优化

2.1 优化加载和编码已知人脸

我们将优化 load_and_encode_faces 方法,使其更高效且更易维护。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星辰@Sea

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值