Building eye and nose detectors

The Haar cascades method can be extended to detect all types of objects. Let's see how to use it to detect the eyes and nose in the input video.

How to do it…

  1. Create a new Python file, and import the following packages:
    import cv2
    import numpy as np
  2. Load the face, eyes, and nose cascade files:
    # Load face, eye, and nose cascade files
    face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml')
    eye_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_eye.xml')
    nose_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_nose.xml')
  3. Check whether the files loaded correctly:
    # Check if face cascade file has been loaded
    if face_cascade.empty():
        raise IOError('Unable to load the face cascade classifier xml file')
    
    # Check if eye cascade file has been loaded
    if eye_cascade.empty():
        raise IOError('Unable to load the eye cascade classifier xml file')
    
    # Check if nose cascade file has been loaded
    if nose_cascade.empty():
        raise IOError('Unable to load the nose cascade classifier xml file')
  4. Initialize the video capture object:
    # Initialize video capture object and define scaling factor
    cap = cv2.VideoCapture(0)
  5. Define the scaling factor:
    scaling_factor = 0.5
  6. Keep looping until the user presses the Esc key:
    while True:
        # Read current frame, resize it, and convert it to grayscale
        ret, frame = cap.read()
  7. Resize the frame:
        frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, 
                interpolation=cv2.INTER_AREA)
  8. Convert the image to grayscale:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  9. Run the face detector on the grayscale image:
        # Run face detector on the grayscale image
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  10. As we know that eyes and noses are always on faces, we can run these detectors only in the face region:
        # Run eye and nose detectors within each face rectangle
        for (x,y,w,h) in faces:
  11. Extract the face ROI:
            # Grab the current ROI in both color and grayscale images
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = frame[y:y+h, x:x+w]
  12. Run the eye detector:
            # Run eye detector in the grayscale ROI
            eye_rects = eye_cascade.detectMultiScale(roi_gray)
  13. Run the nose detector:
            # Run nose detector in the grayscale ROI
            nose_rects = nose_cascade.detectMultiScale(roi_gray, 1.3, 5)
  14. Draw circles around the eyes:
            # Draw green circles around the eyes
            for (x_eye, y_eye, w_eye, h_eye) in eye_rects:
                center = (int(x_eye + 0.5*w_eye), int(y_eye + 0.5*h_eye))
                radius = int(0.3 * (w_eye + h_eye))
                color = (0, 255, 0)
                thickness = 3
                cv2.circle(roi_color, center, radius, color, thickness)
  15. Draw a rectangle around the nose:
            for (x_nose, y_nose, w_nose, h_nose) in nose_rects:
                cv2.rectangle(roi_color, (x_nose, y_nose), (x_nose+w_nose, 
                    y_nose+h_nose), (0,255,0), 3)
                break
  16. Display the image:
        # Display the image
        cv2.imshow('Eye and nose detector', frame)
  17. Wait for 1 ms before going to the next iteration. If the user presses the Esc key, then break the loop.
        # Check if Esc key has been pressed
        c = cv2.waitKey(1)
        if c == 27:
            break
  18. Release and destroy the objects before exiting the code.
    # Release video capture object and close all windows
    cap.release()
    cv2.destroyAllWindows()
  19. The full code is given in the eye_nose_detector.py file that's already provided to you for reference. If you run this code, you will see the eyes and nose being detected in the webcam video:
    How to do it…
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.174.57