我正在尝试构建一个使用面部识别库实时检测面部的软件。我使用网络摄像头进行了尝试,并获得了可喜的结果和相当稳定的帧频,但是当我切换到.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
答案 0 :(得分:3)
要确定脚本的哪些部分运行时间最长,请使用 profiler 。这将输出执行每个调用的时间,因此您可以更好地了解函数的哪些部分不是最佳的。有关如何配置代码的示例,请参见The Python Profilers。
快速识别面部
如果您的计算机具有以下功能,则可以并行进行人脸识别 多个CPU内核。例如,如果您的系统具有4个CPU内核,则您 可以在相同的时间内处理大约4倍的图像 并行使用所有CPU内核。如果您使用的是Python 3.4或 较新的,传入--cpus参数:
face_recognition --cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/
您还可以传入--cpus -1以使用系统中的所有CPU内核。
使用一个,然后使用最大核数来测试计算机上的操作。如果这可以显着缩短执行时间,那么最好的做法是在自己的脚本中实施多处理。