Saturday, 28 November 2015

Face and Facial Feature Detection Using OpenCV and python

The below program detects eyes when closed also. I used right eye haar classifier from http://alereimondo.no-ip.org/OpenCV/34

In every frame of the video, it finds nose, mouth and eyes. No tracking algorithm  used.
Requirements to run the below code
opencv 3
python 2.7
numpy 1.91

import cv2

cap = cv2.VideoCapture('seq_bruges04_300frames.avi')
ret, img = cap.read()
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#righteye_cascade = cv2.CascadeClassifier('haarcascade_righteye_2splits.xml')
#lefteye_cascade = cv2.CascadeClassifier('haarcascade_lefteye_2splits.xml')
eye_cascade = cv2.CascadeClassifier('rightEye.xml')
mouth_cascade = cv2.CascadeClassifier('haarcascade_mcs_mouth.xml')
nose_cascade = cv2.CascadeClassifier('haarcascade_mcs_nose.xml')

while(cap.isOpened()):
    ret, img = cap.read()

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        img = 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)
        i = 0
        while i < len(eyes)-1:
            ex1,ey1,ew1,eh1 = eyes[i]
            ex2,ey2,ew2,eh2 = eyes[i+1]
            if abs(ex1-ex2) > 20 and abs(ey1-ey2)<10:
                cv2.rectangle(roi_color,(ex1,ey1),(ex1+ew1,ey1+eh1),(0,255,0),2)
                cv2.rectangle(roi_color,(ex2,ey2),(ex2+ew2,ey2+eh2),(0,255,0),2)
            #else:
                #cv2.rectangle(roi_color,(ex1,ey1),(ex1+ew1,ey1+eh1),(0,255,0),2)
            i = i+1
        nose = nose_cascade.detectMultiScale(roi_gray)
        for (nx,ny,nw,nh) in nose:
           cv2.rectangle(roi_color,(nx,ny),(nx+nw,ny+nh),(0,255,0),2)
        mouth = mouth_cascade.detectMultiScale(roi_gray)
        for (mx,my,mw,mh) in mouth:
            if (my > ny+(nh/2)):
                cv2.rectangle(roi_color,(mx,my),(mx+mw,my+mh),(0,255,0),2)
    cv2.imshow('img',img)
    if cv2.waitKey(10) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
Output:


No comments:

Post a Comment