import cv2, sys, time

def _detectFace(img):
    
    # Convert to gray scale
    if len(img.shape) != 1:
        image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # Loading trained frontal face classifier 
    face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    
    # Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
    faces = face_classifier.detectMultiScale(image, 1.1, 5)
    
    return faces

def _detectEyes(img):
    
    # Convert to gray scale
    if len(img.shape) != 1:
        image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # Loading trained frontal face classifier 
    eye_classifier = cv2.CascadeClassifier('haarcascade_eye.xml')
    
    # Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
    eyes = eye_classifier.detectMultiScale(image, 1.1, 5)
    
    return eyes

if __name__ == "__main__":
    
    # Capturing video frame
    video_cap = cv2.VideoCapture(0)
        
    # To change the shape of video frame
    video_cap.set(cv2.CAP_PROP_FRAME_WIDTH, 720)
    video_cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 540)
    
    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('rect.mp4', fourcc, 20.0, (640,  480))
        
    # Loading trained frontal face classifier 
    face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    
    while True:
    
        ret, frame = video_cap.read()
        
        if ret:
            faces = _detectFace(frame)
            eyes = _detectEyes(frame)
    
            # Drawing rectangle around face if face found
            if len(faces) != 0:                   
                for face in faces:
                    frame =  cv2.rectangle(frame, (face[0], face[1]), (face[0]+face[2], face[1]+face[3]), (0, 0, 0), 2)
                # Putting text on frame if face detected
                cv2.putText(frame, 'Face Detected', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2, cv2.LINE_4)
            else:
                cv2.putText(frame, 'Face Not Detected', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2, cv2.LINE_4) 
 
            # Drawing rectangle around eye if found
            if len(eyes) != 0:
                for eye in eyes:
                    frame = cv2.rectangle(frame, (eye[0], eye[1]), (eye[0]+eye[2], eye[1]+eye[3]), (0, 0, 0), 2)
                cv2.putText(frame, 'Eyes Detected', (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2, cv2.LINE_4)
            else:
                cv2.putText(frame, 'Eyes Not Detected', (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2, cv2.LINE_4)
    
            # Display video   
            cv2.imshow("Image", frame)
            out.write(frame)
            if cv2.waitKey(5) == ord('q'):
                break
        else:
            break
    
    video_cap.release()
    out.release()
    cv2.destroyAllWindows()

import cv2, sys, time

def _detectFace(img):
    
    # Convert to gray scale
    if len(img.shape) != 1:
        image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # Loading trained frontal face classifier 
    face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    
    # Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
    faces = face_classifier.detectMultiScale(image, 1.1, 5)
    
    return faces

def _detectEyes(img):
    
    # Convert to gray scale
    if len(img.shape) != 1:
        image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # Loading trained frontal face classifier 
    eye_classifier = cv2.CascadeClassifier('haarcascade_eye.xml')
    
    # Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles.
    eyes = eye_classifier.detectMultiScale(image, 1.1, 5)
    
    return eyes

if __name__ == "__main__":
    
    # Capturing video frame
    video_cap = cv2.VideoCapture(0)
        
    # To change the shape of video frame
    video_cap.set(cv2.CAP_PROP_FRAME_WIDTH, 720)
    video_cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 540)
    
    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('rect.mp4', fourcc, 20.0, (640,  480))
        
    # Loading trained frontal face classifier 
    face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    
    while True:
    
        ret, frame = video_cap.read()
        
        if ret:
            faces = _detectFace(frame)
            eyes = _detectEyes(frame)
    
            # Drawing rectangle around face if face found
            if len(faces) != 0:                   
                for face in faces:
                    frame =  cv2.rectangle(frame, (face[0], face[1]), (face[0]+face[2], face[1]+face[3]), (0, 0, 0), 2)
                # Putting text on frame if face detected
                cv2.putText(frame, 'Face Detected', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2, cv2.LINE_4)
            else:
                cv2.putText(frame, 'Face Not Detected', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2, cv2.LINE_4) 
 
            # Drawing rectangle around eye if found
            if len(eyes) != 0:
                for eye in eyes:
                    frame = cv2.rectangle(frame, (eye[0], eye[1]), (eye[0]+eye[2], eye[1]+eye[3]), (0, 0, 0), 2)
                cv2.putText(frame, 'Eyes Detected', (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2, cv2.LINE_4)
            else:
                cv2.putText(frame, 'Eyes Not Detected', (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255), 2, cv2.LINE_4)
    
            # Display video   
            cv2.imshow("Image", frame)
            out.write(frame)
            if cv2.waitKey(5) == ord('q'):
                break
        else:
            break
    
    video_cap.release()
    out.release()
    cv2.destroyAllWindows()

# Convert to gray scale # Loading trained frontal face classifier # Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles. # Convert to gray scale # Loading trained frontal face classifier # Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles. # Capturing video frame # To change the shape of video frame # Define the codec and create VideoWriter object # Loading trained frontal face classifier # Drawing rectangle around face if face found # Putting text on frame if face detected # Drawing rectangle around eye if found # Display video