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 ...

Get Python Machine Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.