UnboundLocalError when two faces appear on input

Viewed 21
def face_detect(img):
    hog_rects = hog_detector(img, 0)
    hog_faces = np.zeros((0, 4), dtype=int)
    for (i, rect) in enumerate(hog_rects):
        (x, y, w, h) = rect_to_bb(rect)
        face = np.asarray((x, y, w, h), dtype=int)
        hog_faces = np.append(hog_faces, [face], axis=0)
    return hog_faces


def detect(img, cascade, minimumFeatureSize=(20, 20)):
    if cascade.empty():
        raise (Exception("There was a problem loading your Haar Cascade xml file."))
    rects = cascade.detectMultiScale(img, scaleFactor=1.3, minNeighbors=1, minSize=minimumFeatureSize)
    if len(rects) == 0:
        return []
    rects[:, 2:] += rects[:, :2]  # convert last coord from (width,height) to (maxX, maxY)
    return rects


def eye_detect(faces, gray, minEyeSize):
    # eyes = np.zeros((0, 4), dtype=int)
    for (x, y, w, h) in faces:
        roi_gray = gray[y:h, x:w]
        detected_eyes = detect(roi_gray, haarEyeCascade, minEyeSize)
        eyeFix = detected_eyes + [x, y, x, y]
        # eyes = np.append(eyes, eyeFix, axis=0)
    return eyeFix

I use the above function for dlib_face detector and loop through the detected faces to find eyes with the eye_detect function using OpenCV haar eyecascade. The input is VideoCapture input from OpenCV. The output of all the functions is a numpy array containing min x, min y max x, max y of the detected feature.

If there is only one face the code works fine. But as soon as a second face comes, it throws

UnboundLocalError: local variable 'eyeFix' referenced before assignment

I want it to only detect eyes on the existing face and not on new faces. What can I do to improve this code?

0 Answers
Related