我试图通过将 OpenCV 矩形转换为 Dlib 的矩形,在使用 Dlibs imutils.face_utils
进行人脸识别之前对齐人脸。但我不断收到错误 rectangle is not iterable。
这是代码
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
fa = FaceAligner(predictor)
我如何先使用 Dlib FaceUtils 对齐,然后使用 OpenCV 的 recognizer.predict()
进行预测?
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = detector(gray, 2)
# If faces are found, try to recognize them
for (x,y,w,h) in faces:
(x1, y1, w1, h1) = rect_to_bb(x,y,w,h)
faceAligned = fa.align(image, gray, (x1,y1,w1,h1))
label, confidence = recognizer.predict(faceAligned)
if confidence < threshold:
found_faces.append((label, confidence, (x, y, w, h)))
return found_faces
最佳答案
dlib 矩形对象不可迭代,将 for 循环更改为
for face in faces:
x = face.left()
y = face.top() #could be face.bottom() - not sure
w = face.right() - face.left()
h = face.bottom() - face.top()
(x1, y1, w1, h1) = rect_to_bb(x,y,w,h)
# rest same as above
关于python - 类型错误 : 'rectangle' object is not iterable after face alignment usinf Dlib FaceUtils,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46261185/