I am trying to detect faces (specifically, faces with opened eyes) using OpenCV haar cascade classifier. However, I had a problem detecting the faces that do not have eyebrows and/or jaw, as shown in the following image. I had tried many haar cascade for face detection such as haarcascade_frontalface_default.xml, haarcascade_frontalface_alt_tree.xml, etc. But all of these did not work.
Here is my code:
import cv2
import os
import glob
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye_tree_eyeglasses.xml')
count = 0
path = "./test/*.png"
for index, filename in enumerate(glob.glob(path)):
img = cv2.imread(filename)
basename = os.path.splitext(os.path.basename(filename))[0]
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
# cv2.rectangle(img,(x,y),(x+w, y+h),(255,0,0), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
if len(eyes) >= 2:
count = count + 1
output_dir = './test/output'
cv2.imwrite(f'{output_dir}/{basename}.png', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Thank you in advance!