#Haar Cascade opencv example. This one is for face detection in an image. Same could be applied to a video.
#whether an image or a video, it has to be converted into a grayscale


import numpy as np
import cv2 as cv

path1 = '/usr/local/lib/python2.7/dist-packages/cv2/data/haarcascade_frontalface_default.xml'
path2 = '/usr/local/lib/python2.7/dist-packages/cv2/data/haarcascade_eye.xml'

face_cascade = cv.CascadeClassifier(path1)
eye_cascade = cv.CascadeClassifier(path2)

img = cv.imread('face1.jpeg') #0 filter condition means that it reads a grayscale image
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
    cv.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)
    for (ex,ey,ew,eh) in eyes:
        cv.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
cv.imshow('img',img)
cv.waitKey(0)
cv.destroyAllWindows()
