Python人脸识别速度慢

4

我正在尝试构建一个软件,使用人脸识别库在实时中检测人脸。我已经用摄像头进行了尝试,并得到了令人满意的结果和相当稳定的帧率,但当我切换到.mp4视频时,fps非常低。我正在使用Python 3.6与OpenCV,并且这是我正在使用的代码:

import face_recognition
import cv2


# Load a sample picture and learn how to recognize it.
totti_image = face_recognition.load_image_file("totti.jpg")
totti_face_encoding = face_recognition.face_encodings(totti_image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    totti_face_encoding
]
known_face_names = [
    "Francesco Totti"
]
def get_faces(frame):
    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_frame = frame[:, :, ::-1]

    # Find all the faces and face enqcodings in the frame of video
    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, tolerance=0.50)

        name = "Unknown"

        # If a match was found in known_face_encodings, just use the first one.
        if True in matches:
            first_match_index = matches.index(True)
            name = known_face_names[first_match_index]

        # 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)

    return frame

在每个帧中都会调用“get_faces”函数,而我得到的性能大约是0.5 fps。 如果有人有建议以获得更好的输出fps,请告诉我,谢谢。

编辑: 我使用了以下示例(根据我的需求进行了调整),一切都运行得更好: 链接

最终代码:

import face_recognition
import cv2

# Load a sample picture and learn how to recognize it.
image = face_recognition.load_image_file("totti.jpg")
encoding = face_recognition.face_encodings(image)[0]

# Create arrays of known face encodings and their names
known_face_encodings = [
    encoding
]
known_face_names = [
    "Totti",
]

# Initialize some variables
face_locations = []
face_encodings = []
face_names = []

def get_faces(frame):

    # Resize frame of video to 1/10 size for faster face recognition processing
    small_frame = cv2.resize(frame, (0, 0), fx=0.1, fy=0.1)

    # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
    rgb_small_frame = small_frame[:, :, ::-1]

    # Find all the faces and face encodings in the current frame of video
    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:
        # See if the face is a match for the known face(s)
        matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
        name = "Person"

        # If a match was found in known_face_encodings, just use the first one.
        if True in matches:
            first_match_index = matches.index(True)
            name = known_face_names[first_match_index]

        face_names.append(name)


    # Display the results
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        # Scale back up face locations since the frame we detected in was scaled to 1/10 size
        top *= 10
        right *= 10
        bottom *= 10
        left *= 10

        # 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)

    return frame
1个回答

5
为了确定脚本中哪些部分运行时间最长,可以使用分析器。这将输出每个调用的执行时间,因此您可以更好地了解函数中哪些部分不够优化。请参阅Python分析器以获取有关如何分析您的代码的示例。
来自文档

加速人脸识别

如果您拥有具有多个CPU核心的计算机,则可以并行进行人脸识别。例如,如果您的系统有4个CPU核心,则可以通过并行使用所有CPU核心,在相同的时间内处理大约4倍的图像。如果您使用的是Python 3.4或更高版本,请传递一个--cpus <number_of_cpu_cores_to_use>参数:

face_recognition --cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/

您还可以传递--cpus -1来使用系统中的所有CPU核心。

使用一个核心和最大数量的核心在计算机上测试操作。如果这显著提高了执行时间,您最好的选择是将多进程实现到自己的脚本中。

2020-08-05更新

进一步研究此问题,因为它仍然受到关注。如果我们查看repository,似乎CLI只是进行一些调用,您可以编写脚本自己获取--cpus参数,以便将其实现到您自己的代码中。具体来说,您可以使用here中的代码程序化地而不是从命令行中使用。以类似的方式使用多进程调用API,或使用def process_images_in_process_pool(images_to_check, number_of_cpus, model):


谢谢您的回答,但我已经知道问题出在这些行上: face_locations = face_recognition.face_locations(rgb_frame) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)但我无法更改它们,因为这些是我需要的库函数。这是我在网上找到的最佳处理方式,但速度非常慢,所以我的问题不是找出哪些部分运行时间最长,而是是否有一种方法可以使用此代码或其他我没有发现的代码来获得更快的分析。 - Ajeje_Brazorf
@J.Blackadar,我可以在我的Python代码中添加face_recognition --cpus 4吗?我正在使用树莓派2进行人脸识别,但速度很慢。:( - M. D. P
1
@M.D.P 增加 CPU 只能通过单独使用 face_recognition 来实现。在您的实际代码中,您需要实现多进程来将计算负载分配到多个核心上。 - J. Blackadar
请问如何在代码中使用 --cpus 4,还是只能在命令行中使用? - MD5

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接