When I run this program, everything works fine if there is a detectable face in front of the camera. The second it lose the face, program crashes and gives an error like this:
Traceback (most recent call last):
File "C:\Users\oguzs\PycharmProiects\DroneDeneme\FaceTracking.py", line 77, in <module>
img, info = findFace(img)
TypeError: cannot unpack non-iterable NoneType object
Here is my face detection code:
def findFace(img):
faceCascade = cv2.CascadeClassifier("Resources/haarcascade_frontalface_default.xml")
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(imgGray, scaleFactor=1.1, minNeighbors=3, flags=cv2.CASCADE_SCALE_IMAGE)
myFaceListC = []
myFaceListArea = []
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
cx = x + w // 2
cy = y + h // 2
area = w * h
cv2.circle(img, (cx, cy), 5, (0, 255, 0), 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]
This is where i call findFace()
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
img = cv2.resize(img, (w, h))
img, info = findFace(img)
cv2.imshow("Output", img)
cv2.waitKey(1)
I just started to learn openCV so any advice would be great :)
take a look at the code below. BTW i recommend you to use newer and better one