HaarObjectDetector Class |
Namespace: Accord.Vision.Detection
The HaarObjectDetector type exposes the following members.
Name | Description | |
---|---|---|
HaarObjectDetector(HaarCascade) |
Constructs a new Haar object detector.
| |
HaarObjectDetector(HaarCascade, Int32) |
Constructs a new Haar object detector.
| |
HaarObjectDetector(HaarCascade, Int32, ObjectDetectorSearchMode) |
Constructs a new Haar object detector.
| |
HaarObjectDetector(HaarCascade, Int32, ObjectDetectorSearchMode, Single) |
Constructs a new Haar object detector.
| |
HaarObjectDetector(HaarCascade, Int32, ObjectDetectorSearchMode, Single, ObjectDetectorScalingMode) |
Constructs a new Haar object detector.
|
Name | Description | |
---|---|---|
Channel |
Gets or sets the color channel to use when processing color images.
| |
Classifier |
Gets the internal Cascade Classifier used by this detector.
| |
DetectedObjects |
Gets the detected objects bounding boxes.
| |
MaxSize |
Maximum window size to consider when searching objects.
| |
MinSize |
Minimum window size to consider when searching objects.
| |
ScalingFactor |
Gets or sets the scaling factor to rescale the window during search.
| |
ScalingMode |
Gets or sets the desired scaling method.
| |
SearchMode |
Gets or sets the desired searching method.
| |
Steady |
Gets how many frames the object has
been detected in a steady position.
| |
Suppression |
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.
| |
UseParallelProcessing |
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+).
|
Name | Description | |
---|---|---|
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ProcessFrame(Bitmap) |
Performs object detection on the given frame.
| |
ProcessFrame(UnmanagedImage) |
Performs object detection on the given frame.
| |
ToString | Returns a string that represents the current object. (Inherited from Object.) |
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
To(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.) | |
ToT | 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.) |
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:
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();