在两台相机上执行面部识别

时间:2018-08-11 06:37:31

标签: python-3.x opencv face-recognition

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

我现在想同时使用两台摄像机进行此操作。

这是我的一台摄像机的代码,我不知道如何为此目的使用两台摄像机。

import face_recognition
import cv2
import numpy as np
video_capture = cv2.VideoCapture('rtsp://admin:11111@192.168.1.13: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进行更改才能使用两台摄像机?

1 个答案:

答案 0 :(得分:0)

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

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