我正在尝试构建一个使用人脸识别库来实时检测人脸的软件。我使用网络摄像头进行了尝试,结果很好,帧速率相当稳定,但是当我切换到 .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”在每一帧的 while 循环中被调用,我获得了大约 0.5 fps 的性能。 如果有人有建议以获得更好的输出 fps,请告诉我,谢谢。
编辑: 我使用了以下示例(根据我的需要对其进行调整)并且一切都运行得更好: link
最终代码:
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
最佳答案
要确定脚本的哪些部分运行时间最长,请使用探查器。这将输出每次调用的执行时间,因此您可以更好地了解函数的哪些部分是次优的。参见 The Python Profilers有关如何分析代码的示例。
来自documentation :
SPEEDING UP FACE RECOGNITION
Face recognition can be done in parallel if you have a computer with multiple CPU cores. For example if your system has 4 CPU cores, you can process about 4 times as many images in the same amount of time by using all your CPU cores in parallel. If you are using Python 3.4 or newer, pass in a --cpus <number_of_cpu_cores_to_use> parameter:
face_recognition --cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/
You can also pass in --cpus -1 to use all CPU cores in your system.
使用一个然后是最大数量的内核测试计算机上的操作。如果这显着缩短了执行时间,那么您最好的做法是在您自己的脚本中实现多处理。
更新2020-08-05
更多地关注这个问题,因为它仍然受到一些关注。如果我们看一下 repository ,看起来 CLI 只是进行了一些您可以自己编写脚本的调用,以将 --cpus
agrument 放入您自己的代码中。具体来说,您可以使用代码 here以编程方式而不是从命令行。使用多处理以类似的方式自行调用 API,或调用 def process_images_in_process_pool(images_to_check, number_of_cpus, model):
。
关于python人脸识别慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51842495/