HaarObjectDetector Class
Click or drag to resize
Accord.NET (logo)

HaarObjectDetector Class

Viola-Jones Object Detector based on Haar-like features.
Inheritance Hierarchy
SystemObject
  Accord.Vision.DetectionHaarObjectDetector

Namespace:  Accord.Vision.Detection
Assembly:  Accord.Vision (in Accord.Vision.dll) Version: 3.8.0
Syntax
public class HaarObjectDetector : IObjectDetector
Request Example View Source

The HaarObjectDetector type exposes the following members.

Constructors
  NameDescription
Public methodHaarObjectDetector(HaarCascade)
Constructs a new Haar object detector.
Public methodHaarObjectDetector(HaarCascade, Int32)
Constructs a new Haar object detector.
Public methodHaarObjectDetector(HaarCascade, Int32, ObjectDetectorSearchMode)
Constructs a new Haar object detector.
Public methodHaarObjectDetector(HaarCascade, Int32, ObjectDetectorSearchMode, Single)
Constructs a new Haar object detector.
Public methodHaarObjectDetector(HaarCascade, Int32, ObjectDetectorSearchMode, Single, ObjectDetectorScalingMode)
Constructs a new Haar object detector.
Top
Properties
  NameDescription
Public propertyChannel
Gets or sets the color channel to use when processing color images.
Public propertyClassifier
Gets the internal Cascade Classifier used by this detector.
Public propertyDetectedObjects
Gets the detected objects bounding boxes.
Public propertyMaxSize
Maximum window size to consider when searching objects.
Public propertyMinSize
Minimum window size to consider when searching objects.
Public propertyScalingFactor
Gets or sets the scaling factor to rescale the window during search.
Public propertyScalingMode
Gets or sets the desired scaling method.
Public propertySearchMode
Gets or sets the desired searching method.
Public propertySteady
Gets how many frames the object has been detected in a steady position.
Public propertySuppression
Gets or sets the minimum threshold used to suppress rectangles which have not been detected sufficient number of times. This property only has effect when SearchMode is set to Average.
Public propertyUseParallelProcessing
Gets or sets a value indicating whether this HaarObjectDetector should scan the image using multiple threads. This setting can only be changed to true on .NET version which support the Parallel Tasks framework (4.0+).
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodProcessFrame(Bitmap)
Performs object detection on the given frame.
Public methodProcessFrame(UnmanagedImage)
Performs object detection on the given frame.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks

The Viola-Jones object detection framework is the first object detection framework to provide competitive object detection rates in real-time proposed in 2001 by Paul Viola and Michael Jones. Although it can be trained to detect a variety of object classes, it was motivated primarily by the problem of face detection.

The implementation of this code has used Viola and Jones' original publication, the OpenCV Library and the Marilena Project as reference. OpenCV is released under a BSD license, it is free for both academic and commercial use. Please be aware that some particular versions of the Haar object detection framework are patented by Viola and Jones and may be subject to restrictions for use in commercial applications. The code has been implemented with full support for tilted Haar features from the ground up.

References:

Examples

The first example shows how to detect faces from a single image using the detector.

// In order to use a HaarObjectDetector, first we have to tell it
// which type of objects we would like to detect. And in a Haar detector,
// different object classifiers are specified in terms of a HaarCascade.

// The framework comes with some built-in cascades for common body
// parts, such as Face and Nose. However, it is also possible to
// load a cascade from cascade XML definitions in OpenCV 2.0 format.

// In this example, we will be creating a cascade for a Face detector:
var cascade = new Accord.Vision.Detection.Cascades.FaceHaarCascade();

// Note: In the case we would like to load it from XML, we could use:
// var cascade = HaarCascade.FromXml("filename.xml");

// Now, create a new Haar object detector with the cascade:
var detector = new HaarObjectDetector(cascade, minSize: 50, 
    searchMode: ObjectDetectorSearchMode.NoOverlap);

// Note that we have specified that we do not want overlapping objects,
// and that the minimum object an object can have is 50 pixels. Now, we
// can use the detector to classify a new image. For instance, consider
// the famous Lena picture:

Bitmap bmp = Accord.Imaging.Image.Clone(Resources.lena_color);

// We have to call ProcessFrame to detect all rectangles containing the 
// object we are interested in (which in this case, is the face of Lena):
Rectangle[] rectangles = detector.ProcessFrame(bmp);

// The answer will be a single rectangle of dimensions
// 
//   {X = 126 Y = 112 Width = 59 Height = 59}
// 
// which indeed contains the only face in the picture.

The second example shows how to process an entire video using FileVideoReader class, detecting faces from each frame, and saving those detections back to disk in the form of individual frames and as a .mp4 file (using FileVideoWriter).

// Let's test the detector using a sample video from 
// the collection of test videos in the framework:
TestVideos ds = new TestVideos(basePath);
string fileName = ds["crowd.mp4"];

// In this example, we will be creating a cascade for a Face detector:
var cascade = new Accord.Vision.Detection.Cascades.FaceHaarCascade();

// Now, create a new Haar object detector with the cascade:
var detector = new HaarObjectDetector(cascade, minSize: 25,
    searchMode: ObjectDetectorSearchMode.Average,
    scalingMode: ObjectDetectorScalingMode.SmallerToGreater,
    scaleFactor: 1.1f)
{
    Suppression = 5 // This should make sure we only report regions as faces if 
    // they have been detected at least 5 times within different cascade scales.
};

// Now, let's open the video using FFMPEG:
var video = new VideoFileReader();
video.Open(fileName);

// And then check the contents of one of the frames:
Bitmap frame = video.ReadVideoFrame(frameIndex: 0);

// Creating bitmaps and locking them is an expensive 
// operation. Instead, let's allocate once and reuse
BitmapData bitmapData = frame.LockBits(ImageLockMode.ReadWrite);
UnmanagedImage unmanagedImage = new UnmanagedImage(bitmapData);

// We will create a color marker to show the faces 
var objectMarker = new RectanglesMarker(Color.Red);

// This example is going to show two different ways to save results to disk. The 
// first is to save the results frame-by-frame, saving each individual frame as 
// a separate .png file. The second is to save them as a video in .mp4 format.

// To save results as a movie clip in mp4 format, you can use:
VideoFileWriter writer = new VideoFileWriter();
writer.Open(Path.Combine(basePath, "detected_faces.mp4"), frame.Width, frame.Height);

// Now, for each frame of the video
for (int frameIndex = 0; frameIndex < video.FrameCount; frameIndex++)
{
    // Read the current frame into the bitmap data
    video.ReadVideoFrame(frameIndex, bitmapData);

    // Feed the frame to the tracker
    Rectangle[] faces = detector.ProcessFrame(unmanagedImage);

    // Mark the location of the tracker object in red color
    objectMarker.Rectangles = faces;
    objectMarker.ApplyInPlace(unmanagedImage); // overwrite the frame

    // Save it to disk: first saving each frame separately:
    frame.Save(Path.Combine(basePath, "frame_{0}.png".Format(frameIndex)));

    // And then, saving as a .mp4 file:
    writer.WriteVideoFrame(bitmapData);
}

// The generated video can be seen at https://1drv.ms/v/s!AoiTwBxoR4OAoLJhPozzixD25XcbiQ
video.Close();
writer.Close();

The generated video file can be found here.

See Also