在两个摄像头上进行人脸识别

问题描述 投票:0回答:2

我有一个使用相机的人脸识别项目,没有任何问题。

我现在想通过两个摄像头同时执行此操作。

这是我针对一台相机的代码,我不知道如何使用两台相机来实现此目的。

import face_recognition
import cv2
import numpy as np
video_capture = cv2.VideoCapture('rtsp://admin:[email protected]:554/mode=real&idc=1&ids=2')


farid_image = face_recognition.load_image_file("farid.jpg")
farid_face_encoding = face_recognition.face_encodings(farid_image)[0]

# Load a second sample picture and learn how to recognize it.
roice_image = face_recognition.load_image_file("roice.jpg")
roice_face_encoding = face_recognition.face_encodings(roice_image)[0]

known_face_encodings = [
    farid_face_encoding,
    roice_face_encoding
]
known_face_names = [
    "farid",
    "roice"
]

while True:

    ret, frame = video_capture.read()
    rgb_frame = frame[:, :, ::-1]


    face_locations = face_recognition.face_locations(rgb_frame)
    face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)

    # Loop through each face in this frame of video
    for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
        # See if the face is a match for the known face(s)
        matches = face_recognition.compare_faces(known_face_encodings, face_encoding)

        name = "Unknown"

        # Calculate face distance
        face_distance = face_recognition.face_distance(known_face_encodings, face_encoding)
        # If a match was found in known_face_encodings, just use the first one.
        if True in matches:
           # first_match_index = matches.index(True)
            # Sort nearest distance
            name = known_face_names[np.argsort(face_distance)[0]]

        # Draw a box around the face
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

        # Draw a label with a name below the face
        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)

    # Display the resulting image
    cv2.imshow('Video', frame)

    # Hit 'q' on the keyboard to quit!
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

我可以简单地使用

cv2.VideoCapture()
模块添加更多摄像头,但如何更改
face_recognition
才能使用两个摄像头?

python-3.x opencv face-recognition
2个回答
0
投票

您可以尝试使其成为多线程。每个摄像头都有一个线程,对它看到的图像进行自己的面部识别。

它们将在各自的流上独立运行,但您可以从两个线程获取结果以组合信息以改进检测和/或识别。


0
投票

使用另一个线程仍然会让CPU被帧淹没,你可以使用多进程模块或让脚本在执行时带参数,并为每个摄像头输入rtsp_url并使用不同的url运行脚本,如下所示

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--rtsp-url", type=str, default='0')
    parser.add_argument("--data-dir", type=str, default='Report')
    parser.add_argument("--cam-id", type=str, default='0')
    args = parser.parse_args()
    Rtsp_url = args.rtsp_url

您可以使用“terminal keeper”vscode 扩展来自动执行具有不同参数的多个脚本,或者您可以遵循此存储库 Git 仓库:/M-M-Akash/Face_Recognition_System 您可以使用“多进程”而不是线程来实现真正的并行性

© www.soinside.com 2019 - 2024. All rights reserved.