+
95
-

回答

在 Python 中实现通过摄像头进行实时人脸识别并对指定人脸进行报警的功能,可以使用 OpenCV 和 face_recognition 库。以下是一个完整的解决方案,包括安装必要的库、编写代码和运行程序。

安装必要的库

首先,确保你已经安装了 opencv-python 和 face_recognition 库。如果没有安装,可以使用以下命令进行安装:

pip install opencv-pythonpip install face_recognition

编写代码

下面是一个完整的示例代码,展示如何通过摄像头进行实时人脸识别,并对指定人脸进行报警。
import cv2
import face_recognition
import numpy as np
import os
from playsound import playsound

# 加载指定人脸照片并进行编码
def load_known_face(image_path):
    image = face_recognition.load_image_file(image_path)
    face_encoding = face_recognition.face_encodings(image)[0]
    return face_encoding

# 初始化已知人脸
known_face_encodings = []
known_face_names = []

# 指定人脸照片路径
known_face_image_path = "path/to/known_face.jpg"
known_face_name = "Person Name"

# 加载并编码已知人脸
known_face_encodings.append(load_known_face(known_face_image_path))
known_face_names.append(known_face_name)

# 打开摄像头
video_capture = cv2.VideoCapture(0)

# 初始化一些变量
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    # 抓取一帧视频
    ret, frame = video_capture.read()

    # 每两帧处理一次
    if process_this_frame:
        # 将视频帧缩小以加快处理速度
        small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)

        # 将帧转换为RGB颜色空间
        rgb_small_frame = small_frame[:, :, ::-1]

        # 检测人脸位置
        face_locations = face_recognition.face_locations(rgb_small_frame)
        face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)

        face_names = []
        for face_encoding in face_encodings:
            # 检查人脸是否与已知人脸匹配
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"

            # 使用已知人脸的距离,找到最匹配的已知人脸
            face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
            best_match_index = np.argmin(face_distances)
            if matches[best_match_index]:
                name = known_face_names[best_match_index]

            face_names.append(name)

    process_this_frame = not process_this_frame

    # 显示结果
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # 将人脸位置还原到原始帧大小
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4

        # 画出人脸框
        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)

        # 如果检测到指定人脸,触发报警
        if name == known_face_name:
            print(f"Alert! {name} detected!")
            playsound('path/to/alarm_sound.mp3')

    # 显示视频帧
    cv2.imshow('Video', frame)

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

# 释放摄像头资源并关闭窗口
video_capture.release()
cv2.destroyAllWindows()

代码解释

加载指定人脸照片并进行编码:

使用 face_recognition.load_image_file 加载指定人脸照片。

使用 face_recognition.face_encodings 对人脸进行编码。

初始化已知人脸:

将指定人脸的编码和名字添加到已知人脸列表中。

打开摄像头:

使用 cv2.VideoCapture(0) 打开默认摄像头。

处理视频帧:

每两帧处理一次,以提高处理速度。

使用 face_recognition.face_locations 检测人脸位置。

使用 face_recognition.face_encodings 对人脸进行编码。

使用 face_recognition.compare_faces 检查人脸是否与已知人脸匹配。

显示结果:

在视频帧上画出人脸框和名字标签。

如果检测到指定人脸,触发报警(播放报警声音)。

释放资源:

释放摄像头资源并关闭窗口。

注意事项

路径配置:确保已知人脸照片和报警声音文件的路径正确。

性能优化:可以进一步优化代码以提高性能,例如使用多线程处理视频帧。

报警方式:可以根据需要更改报警方式,例如发送邮件或短信。

网友回复

我知道答案,我要回答