计算机系统应用教程网站

网站首页 > 技术文章 正文

python摄像头识别检测已有照片人脸

btikc 2025-01-09 10:46:59 技术文章 15 ℃ 0 评论

需要安装的库:dlib==19.24.99,face_recognition,opencv等,python环境3.9.19

以识别2个人脸为例,代码如下

import cv2
import face_recognition

# 1. 加载目标人脸照片和提取特征
known_face_encodings = []
known_face_names = []

# 加载 mwj 的照片
mwj_image = face_recognition.load_image_file("lucy.jpg")
mwj_encoding = face_recognition.face_encodings(mwj_image)[0]
known_face_encodings.append(mwj_encoding)
known_face_names.append("lucy")

# 加载 sky 的照片
sky_image = face_recognition.load_image_file("lena.jpg")
sky_encoding = face_recognition.face_encodings(sky_image)[0]
known_face_encodings.append(sky_encoding)
known_face_names.append("lena")

# 2. 打开视频
video_capture = cv2.VideoCapture(0)  # 替换为 0 使用摄像头
# video_capture = cv2.VideoCapture("video.mp4")  # 替换为 0 使用摄像头

while video_capture.isOpened():
    ret, frame = video_capture.read()
    if not ret:
        break

    # 3. 在视频帧中检测人脸
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  # 转为 RGB
    face_locations = face_recognition.face_locations(rgb_frame)
    face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
        # 4. 将当前人脸与目标人脸进行比较
        matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
        name = "Unknown"

        # 如果匹配到人脸,标记名字
        if True in matches:
            match_index = matches.index(True)
            name = known_face_names[match_index]

        # 5. 在视频帧中标记人脸
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
        cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

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

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

# 释放资源
video_capture.release()
cv2.destroyAllWindows()


如果要检测2人以上的人脸可以使用以下方式实现,代码目录新建一个文件夹faces,里面放命名好人名的头像照片,这样不管3张5张还是更多都可以检测了

import cv2
import face_recognition
import os

# 1. 加载目标人脸照片和提取特征
known_face_encodings = []
known_face_names = []

# 设置存放人脸照片的目录
faces_directory = "faces"  # 替换为你的目录路径

# 遍历目录中的所有图片文件
for filename in os.listdir(faces_directory):
    if filename.endswith(".jpg") or filename.endswith(".png"):
        # 加载图片并提取特征
        image_path = os.path.join(faces_directory, filename)
        image = face_recognition.load_image_file(image_path)
        encoding = face_recognition.face_encodings(image)[0]

        # 将特征和名字添加到列表
        known_face_encodings.append(encoding)
        name = os.path.splitext(filename)[0]  # 使用文件名作为名字
        known_face_names.append(name)

print(f"已加载以下人脸: {known_face_names}")

# 2. 打开视频
video_capture = cv2.VideoCapture(0)  # 使用摄像头
# video_capture = cv2.VideoCapture("video.mp4")  # 使用视频文件

while video_capture.isOpened():
    ret, frame = video_capture.read()
    if not ret:
        break

    # 3. 在视频帧中检测人脸
    rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  # 转为 RGB
    face_locations = face_recognition.face_locations(rgb_frame)
    face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
        # 4. 将当前人脸与目标人脸进行比较
        matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
        name = "Unknown"

        # 如果匹配到人脸,标记名字
        if True in matches:
            match_index = matches.index(True)
            name = known_face_names[match_index]

        # 5. 在视频帧中标记人脸
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
        cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

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

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

# 释放资源
video_capture.release()
cv2.destroyAllWindows()


本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表