Click here to Skip to main content
15,790,267 members
Articles / Desktop Programming / Win32

Face Detection with 10 Lines of Code - VB.NET

Rate me:
Please Sign up or sign in to vote.
4.94/5 (22 votes)
23 Mar 2013CPOL2 min read 205.8K   43.5K   62   9
Probably the easiest way to detect faces in images by using framework


Finding faces in an image can be quite a challenging issue. There are some commercial and Open Source frameworks available to developers in order to make the task of finding faces easier. A commercially available library that I can recommend is Neurotechnology VeryLook. Probably the most famous Open Source framework for face detection is Intel's OpenCV (Open Computer Vision) which is capable of a whole lot more than just detecting faces. As it is based on the C++ programming language, it is not easy to use in .NET environment. EmguCV solves this issue by providing a .NET interface to OpenCV functions. While these frameworks are powerful, they are not so easy to use.

Image 1

In this article, I will explain the simplest way of detecting faces by using the Open-Source library, which is built on top of the AForge imaging library that was mentioned in my post about using web camera with VB.NET.

Image 2


I am using face detection while developing solutions for biometric enrollment systems. Finding a face is an essential step in making the proper facial photo by international standards (ICAO). Only standardized images can be used in national documents, such are biometric passports, visas and national IDs.

Using the Code

The code sample is an extract of the attached project and in this article, I will focus only on the main functionality - finding a face with library.

Dim detector As HaarObjectDetector
Dim cascade As New FaceHaarCascade
detector = New HaarObjectDetector(cascade, 30)   

First of all, we need to declare class HaarObjectDetector. This class encapsulates algorithm for finding different objects in picture. Objects are described in so called cascades. Many of them are available in open-source projects (face, left eye, right eye, both eyes, nose, mouth, ...) already incorporates cascade for finding face objects FaceHaarCascade, so you don't need to deal with cascade files.

detector.SearchMode = ObjectDetectorSearchMode.Average
detector.ScalingFactor = 1.5
detector.ScalingMode = ObjectDetectorScalingMode.GreaterToSmaller
detector.UseParallelProcessing = True
detector.Suppression = 3

Dim sw As Stopwatch = Stopwatch.StartNew
Dim faceObjects As Rectangle() = detector.ProcessFrame(PictureBox1.Image)

There are some parameters that have to be set for HaarObjectDetector class. Most important property is SearchMode which tells the detector which method to use while searching. In the example above, we use Average mode. With this mode, we mark object as face if it is found at least three times, what is set in Suppression property.

For best results, you need to play a little bit with the above properties. Most of the time, I only search for one face in image, so I usually set SearchMode to Single and I set ScailingMode to GreaterToSmaller, as the face in my case is usually the biggest object in the picture.

Dim g As Graphics = Graphics.FromImage(PictureBox1.Image)
For Each face In faceObjects
   g.DrawRectangle(Pens.DeepSkyBlue, face)


The last step is to draw a rectangle around the found face. The return structure from the detector is an array of Rectangles. With the for statement, we iterate through all of the found face rectangles and draw them on the Graphics of the source image.

Points of Interest

You can now easily combine web camera image (Fabrika LAB: WebCam Video) and the face detection explained here. Wow!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) CENT SI d.o.o.
Slovenia Slovenia
I have strange hobby - programming.
While I am not programming I am tasting some good beers Wink | ;)

Comments and Discussions

QuestionDetect image is not working Pin
mr.shahidamin7-Sep-21 22:08
professionalmr.shahidamin7-Sep-21 22:08 
QuestionImplementation of Image Binarizer using Machine Learning in .NetCore (C#) Pin
Member 1469295617-Dec-19 15:55
Member 1469295617-Dec-19 15:55 
QuestionThis Program works only with your Photo only. Pin
S.N.Ramkumar8-Dec-19 3:04
S.N.Ramkumar8-Dec-19 3:04 
Questiona very good article Pin
nathan200012-Nov-19 9:52
nathan200012-Nov-19 9:52 
QuestionWhat is the (http) source for package Pin
Member 1319810822-Mar-18 9:26
Member 1319810822-Mar-18 9:26 
QuestionThe program Pin
Member 1348475325-Oct-17 4:17
Member 1348475325-Oct-17 4:17 
AnswerRe: The program Pin
90079431-Oct-17 14:53
90079431-Oct-17 14:53 
GeneralRe: The program Pin
9007942-Nov-17 12:33
9007942-Nov-17 12:33 
GeneralRe: The program Pin
waleb1617-Dec-18 21:05
waleb1617-Dec-18 21:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.