Sean Francis N. Ballais
Sean Francis N. Ballais

Reputation: 2488

Why Does OpenCV Won't Detect Eyes With Haar Cascades In Python?

I'm trying to detect a pair of eyes inside a rectangular shape which appears if a face is detected with OpenCV 2.4.x Python. Here's my code:

FACE_DETECT = "lbpcascade_frontalface.xml"
EYE_DETECT = "haarcascade_eye.xml"
DOWNSCALE = 4

webcam = cv2.VideoCapture(0)
face_classifier = cv2.CascadeClassifier(FACE_DETECT)
eye_classifier = cv2.CascadeClassifier(EYE_DETECT)

if webcam.isOpened(): # try to get the first frame
    rval, frame = webcam.read()
else:
    rval = False

while rval:
    minisize = (frame.shape[1] / DOWNSCALE,frame.shape[0] / DOWNSCALE)
    miniframe = cv2.resize(frame, minisize)
    faces = face_classifier.detectMultiScale(miniframe)
    eyes = eye_classifier.detectMultiScale(miniframe)
    for f in faces:
        fx, fy, fw, fh = [fv * DOWNSCALE for fv in f]
        cv2.rectangle(frame, (fx, fy), (fx + fw, fy + fh), (0, 0, 255))
        for (ex,ey, ew, eh) in eyes:
            cv2.rectangle(frame, (ex,ey), ((ex+ew), (ey+eh)), (50, 50, 50), 3)
            cv2.imshow('eyes = %s' % (eyes,), frame)

    cv2.imshow("cam", frame)

    rval, frame = webcam.read()

    key = cv2.waitKey(20)
    if key in [27, ord('Q'), ord('q')]: # exit on ESC
        break

My face detection code works in this one but the eye detection part doesn't (I just included the face detection code in case it might be of good use). I've added a few print() statements inside the loop sequence of the eyes:

for (ex,ey, ew, eh) in eyes:
    cv2.rectangle(frame, (ex,ey), ((ex+ew), (ey+eh)), (50, 50, 50), 3)
    cv2.imshow('eyes = %s' % (eyes,), frame)

However, no output appears.
If the face detection works, then the eye detection should work or at least go inside the eye loop. I've probably thought of a wrong algorithm. How can the eyes be detected?

Any help will be appreciated. :)

Upvotes: 0

Views: 2220

Answers (1)

Namit Juneja
Namit Juneja

Reputation: 498

what you have to do is search for the eyes in the face and not the whole image You can do it by cropping the image and obtaining only the face , then try to find the eyes in that image

sub_face = miniframe[fy:fy+fh, fx:fx+fw]
eyes = eye_classifier.detectMultiScale(sub_frame)
for (ex,ey, ew, eh) in eyes:
    cv2.rectangle(frame, (fx+ex,fy+ey), ((fx+ex+ew), (fy+ey+eh)), (50, 50, 50), 3)

Upvotes: 2

Related Questions