类型错误:使用 Dlib FaceUtils 进行面部对齐后,“矩形”对象不可迭代

Man*_* BN 2 python opencv dlib

我试图imutils.face_utils通过将 OpenCV 矩形转换为 Dlib 的矩形,在使用 Dlibs进行人脸识别之前对齐人脸。但我一直保持错误矩形不可迭代。这是代码

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

fa = FaceAligner(predictor)
Run Code Online (Sandbox Code Playgroud)

如何首先使用 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
Run Code Online (Sandbox Code Playgroud)

Sre*_*A R 5

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
Run Code Online (Sandbox Code Playgroud)