Manu BN
Manu BN

Reputation: 53

TypeError: 'rectangle' object is not iterable after face alignment usinf Dlib FaceUtils

I am trying to align faces before face recognition using Dlibs imutils.face_utils by converting the OpenCV rect to Dlib's rect. But I keep gettng error rectangle is not iterable. Here is the code

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

fa = FaceAligner(predictor)

How do I align first using Dlib FaceUtils and then do prediction using OpenCV's 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

Upvotes: 3

Views: 6169

Answers (1)

Sreeragh A R
Sreeragh A R

Reputation: 3021

dlib rectangle object is not iterable, change your for loop to

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

Upvotes: 6

Related Questions