TypeError: cannot unpack non-iterable NoneType object while trying face recognition

101 views Asked by At

When I try to use this code for face recognition (I tried doing it by following murtaza's tutorial on how to program a drone, but whenever the rectangle around me disappear it crashes and sends the error mentioned above.)

def findFace(img):
    faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
    imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(imgGray, 1.2, 8)


    myFaceListC = \[\]
    myFaceListArea = \[\]


    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        cx = x + w // 2
        cy = y + h // 2
        area = w \* h
        cv2.circle(img, (cx,cy),5,(204,51,128),cv2.FILLED)
        myFaceListC.append(\[cx, cy\])
        myFaceListArea.append(area)
        if len(myFaceListArea) != 0:
            i = myFaceListArea.index(max(myFaceListArea))
            return img, \[myFaceListC\[i\], myFaceListArea\[i\]\]
        else:
            return img, \[\[0, 0\], 0\]

Cap = cv2.VideoCapture(0)
while True:
    \_, img = Cap.read()
    img, info = findFace(img)
    print("Area", info\[1\], "Center", info\[0\])
    findFace(img)
    cv2.imshow("Output", img)
    cv2.waitKey(1)

It returns this error:

Traceback (most recent call last):
  File "C:/Users/HP/PycharmProjects/ourdrone/FaceTracking.py", line 32, in \<module\>
    img, info = findFace(img)
TypeError: cannot unpack non-iterable NoneType object

Process finished with exit code 1
1

There are 1 answers

1
Kilian On BEST ANSWER

In your code, you need to unindent your 'if statement':

for (x, y, w, h) in faces:
    cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
    cx = x + w // 2
    cy = y + h // 2
    area = w \* h
    cv2.circle(img, (cx,cy),5,(204,51,128),cv2.FILLED)
    myFaceListC.append(\[cx, cy\])
    myFaceListArea.append(area)
if len(myFaceListArea) != 0:
    i = myFaceListArea.index(max(myFaceListArea))
    return img, \[myFaceListC\[i\], myFaceListArea\[i\]\]
else:
    return img, \[\[0, 0\], 0\]