def test_find_haar_features():
    img = Factory.Image(testimage)
    img1 = img.copy()
    face = HaarCascade("face.xml")  # old HaarCascade
    f = img.find_haar_features(face)
    f2 = img1.find_haar_features("face_cv2.xml")  # new cv2 HaarCascade
    assert len(f) > 0
    assert len(f2) > 0
    f.draw()
    f2.draw()
    f[0].width
    f[0].height
    f[0].length
    f[0].area

    results = [img, img1]
    name_stem = "test_find_haar_features"
    perform_diff(results, name_stem)

    # incorrect cascade name
    f3 = img.find_haar_features(cascade="incorrect_cascade.xml")
    assert_equals(f3, None)

    # incorrect cascade object
    f4 = img.find_haar_features(cascade=img1)
    assert_equals(f4, None)

    # Empty image
    img2 = Factory.Image((100, 100))
    f5 = img2.find_haar_features("face_cv2.xml")
    assert_equals(f5, None)
def test_haarcascade():
    img = Factory.Image(testimage)
    faces = img.find_haar_features(FACECASCADE)

    if faces:
        faces.draw()
        img.save(testoutput)
    else:
        assert False

    cascade = HaarCascade(FACECASCADE, "face_cascade")
    assert_equals(cascade.get_name(), "face_cascade")

    fhandle = os.path.join(DATA_DIR, "HaarCascades", "face.xml")
    assert_equals(cascade.get_fhandle(), fhandle)

    cascade.set_name("eye_cascade")
    assert_equals(cascade.get_name(), "eye_cascade")

    new_fhandle = os.path.join(DATA_DIR, "HaarCascades", "eye.xml")
    cascade.load(new_fhandle)
    assert_equals(cascade.get_fhandle(), new_fhandle)

    emptycascade = HaarCascade()
Example #3
0
def find_haar_features(img,
                       cascade,
                       scale_factor=1.2,
                       min_neighbors=2,
                       use_canny=cv2.cv.CV_HAAR_DO_CANNY_PRUNING,
                       min_size=(20, 20),
                       max_size=(1000, 1000)):
    """
    **SUMMARY**

    A Haar like feature cascase is a really robust way of finding the
    location of a known object. This technique works really well for a few
    specific applications like face, pedestrian, and vehicle detection. It
    is worth noting that this approach **IS NOT A MAGIC BULLET** . Creating
    a cascade file requires a large number of images that have been sorted
    by a human.vIf you want to find Haar Features (useful for face
    detection among other purposes) this will return Haar feature objects
    in a FeatureSet.

    For more information, consult the cv2.CascadeClassifier documentation.

    To see what features are available run img.list_haar_features() or you
    can provide your own haarcascade file if you have one available.

    Note that the cascade parameter can be either a filename, or a
    HaarCascade loaded with cv2.CascadeClassifier(),
    or a SimpleCV HaarCascade object.

    **PARAMETERS**

    * *cascade* - The Haar Cascade file, this can be either the path to a
      cascade file or a HaarCascased SimpleCV object that has already been
      loaded.

    * *scale_factor* - The scaling factor for subsequent rounds of the
      Haar cascade (default 1.2) in terms of a percentage
      (i.e. 1.2 = 20% increase in size)

    * *min_neighbors* - The minimum number of rectangles that makes up an
      object. Ususally detected faces are clustered around the face, this
      is the number of detections in a cluster that we need for detection.
      Higher values here should reduce false positives and decrease false
      negatives.

    * *use-canny* - Whether or not to use Canny pruning to reject areas
     with too many edges (default yes, set to 0 to disable)

    * *min_size* - Minimum window size. By default, it is set to the size
      of samples the classifier has been trained on ((20,20) for face
      detection)

    * *max_size* - Maximum window size. By default, it is set to the size
      of samples the classifier has been trained on ((1000,1000) for face
      detection)

    **RETURNS**

    A feature set of HaarFeatures

    **EXAMPLE**

    >>> faces = HaarCascade(
        ...         "./SimpleCV/data/Features/HaarCascades/face.xml",
        ...         "myFaces")
    >>> cam = Camera()
    >>> while True:
    >>>     f = cam.get_image().find_haar_features(faces)
    >>>     if f is not None:
    >>>          f.show()

    **NOTES**

    OpenCV Docs:
    - http://opencv.willowgarage.com/documentation/python/
      objdetect_cascade_classification.html

    Wikipedia:
    - http://en.wikipedia.org/wiki/Viola-Jones_object_detection_framework
    - http://en.wikipedia.org/wiki/Haar-like_features

    The video on this pages shows how Haar features and cascades work to
    located faces:
    - http://dismagazine.com/dystopia/evolved-lifestyles/8115/
    anti-surveillance-how-to-hide-from-machines/

    """
    if isinstance(cascade, basestring):
        cascade = HaarCascade(cascade)
        if not cascade.get_cascade():
            return None
    elif isinstance(cascade, HaarCascade):
        pass
    else:
        logger.warning('Could not initialize HaarCascade. '
                       'Enter Valid cascade value.')
        return None

    haar_classify = cv2.CascadeClassifier(cascade.get_fhandle())
    objects = haar_classify.detectMultiScale(img.to_gray(),
                                             scaleFactor=scale_factor,
                                             minNeighbors=min_neighbors,
                                             minSize=min_size,
                                             flags=use_canny)

    if objects is not None and len(objects) != 0:
        return FeatureSet(
            [Factory.HaarFeature(img, o, cascade, True) for o in objects])

    return None
def test_haarcascade():
    img = Factory.Image(testimage)
    faces = img.find_haar_features(FACECASCADE)

    if faces:
        faces.draw()
        img.save(testoutput)
    else:
        assert False

    cascade = HaarCascade(FACECASCADE, "face_cascade")
    assert_equals(cascade.get_name(), "face_cascade")

    fhandle = os.path.join(DATA_DIR, "HaarCascades", "face.xml")
    assert_equals(cascade.get_fhandle(), fhandle)

    cascade.set_name("eye_cascade")
    assert_equals(cascade.get_name(), "eye_cascade")

    new_fhandle = os.path.join(DATA_DIR, "HaarCascades", "eye.xml")
    cascade.load(new_fhandle)
    assert_equals(cascade.get_fhandle(), new_fhandle)

    emptycascade = HaarCascade()
def find_haar_features(img, cascade, scale_factor=1.2, min_neighbors=2,
                       use_canny=cv2.cv.CV_HAAR_DO_CANNY_PRUNING,
                       min_size=(20, 20), max_size=(1000, 1000)):
    """
    **SUMMARY**

    A Haar like feature cascase is a really robust way of finding the
    location of a known object. This technique works really well for a few
    specific applications like face, pedestrian, and vehicle detection. It
    is worth noting that this approach **IS NOT A MAGIC BULLET** . Creating
    a cascade file requires a large number of images that have been sorted
    by a human.vIf you want to find Haar Features (useful for face
    detection among other purposes) this will return Haar feature objects
    in a FeatureSet.

    For more information, consult the cv2.CascadeClassifier documentation.

    To see what features are available run img.list_haar_features() or you
    can provide your own haarcascade file if you have one available.

    Note that the cascade parameter can be either a filename, or a
    HaarCascade loaded with cv2.CascadeClassifier(),
    or a SimpleCV HaarCascade object.

    **PARAMETERS**

    * *cascade* - The Haar Cascade file, this can be either the path to a
      cascade file or a HaarCascased SimpleCV object that has already been
      loaded.

    * *scale_factor* - The scaling factor for subsequent rounds of the
      Haar cascade (default 1.2) in terms of a percentage
      (i.e. 1.2 = 20% increase in size)

    * *min_neighbors* - The minimum number of rectangles that makes up an
      object. Ususally detected faces are clustered around the face, this
      is the number of detections in a cluster that we need for detection.
      Higher values here should reduce false positives and decrease false
      negatives.

    * *use-canny* - Whether or not to use Canny pruning to reject areas
     with too many edges (default yes, set to 0 to disable)

    * *min_size* - Minimum window size. By default, it is set to the size
      of samples the classifier has been trained on ((20,20) for face
      detection)

    * *max_size* - Maximum window size. By default, it is set to the size
      of samples the classifier has been trained on ((1000,1000) for face
      detection)

    **RETURNS**

    A feature set of HaarFeatures

    **EXAMPLE**

    >>> faces = HaarCascade(
        ...         "./SimpleCV/data/Features/HaarCascades/face.xml",
        ...         "myFaces")
    >>> cam = Camera()
    >>> while True:
    >>>     f = cam.get_image().find_haar_features(faces)
    >>>     if f is not None:
    >>>          f.show()

    **NOTES**

    OpenCV Docs:
    - http://opencv.willowgarage.com/documentation/python/
      objdetect_cascade_classification.html

    Wikipedia:
    - http://en.wikipedia.org/wiki/Viola-Jones_object_detection_framework
    - http://en.wikipedia.org/wiki/Haar-like_features

    The video on this pages shows how Haar features and cascades work to
    located faces:
    - http://dismagazine.com/dystopia/evolved-lifestyles/8115/
    anti-surveillance-how-to-hide-from-machines/

    """
    if isinstance(cascade, basestring):
        cascade = HaarCascade(cascade)
        if not cascade.get_cascade():
            return None
    elif isinstance(cascade, HaarCascade):
        pass
    else:
        logger.warning('Could not initialize HaarCascade. '
                       'Enter Valid cascade value.')
        return None

    haar_classify = cv2.CascadeClassifier(cascade.get_fhandle())
    objects = haar_classify.detectMultiScale(
        img.to_gray(), scaleFactor=scale_factor,
        minNeighbors=min_neighbors, minSize=min_size,
        flags=use_canny)

    if objects is not None and len(objects) != 0:
        return FeatureSet(
            [Factory.HaarFeature(img, o, cascade, True) for o in objects])

    return None