I need it to detect eyes (separately, both open or closed), crop them and save them as images. It works but not in every photo.
I tried everything I could think of. I tried different values for scaleFactor and minNeighbors, also tried to add min and max size for the eyes detected (did not make much difference).
I still get issues. It sometimes detects more than 2 eyes, sometimes only 1. Sometimes it mistakes even nostrils for eyes :D . Especially if the eyes are closed, the errors are very often.
What can I do to improve accuracy? This is very important for the rest of my program.
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eyes_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')
faces_detected = face_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=5)
(x, y, w, h) = faces_detected[0]
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1);
eyes = eyes_cascade.detectMultiScale(img[y:y + h, x:x + w], scaleFactor=1.1, minNeighbors=5)
count = 1
for (ex, ey, ew, eh) in eyes:
cv2.rectangle(img, (x + ex, y + ey), (x + ex + ew, y + ey + eh), (255, 255, 255), 1)
crop_img = img[y + ey:y + ey + eh, x + ex:x + ex + ew]
s1 = 'Images/{}.jpg'.format(count)
count = count + 1
cv2.imwrite(s1, crop_img)
For face detection stuff, my go-to would be
dlib(Python API). It is more involved and slower but it results in much higher quality results.Step 1 is converting from
OpenCVtodlib:Next, you can use the
dlibface detector to detect the faces (second argument means to upsample by 1x):Then find facial landmarks using a pre-trained 68 point predictor:
Note: From here you could get face chips
dlib.get_face_chip(img, faces[0])Now you can get bounding boxes and the locations of the eyes:
Here are all the mappings according to pyimagesearch:
Here's the results and the code I put together:
