Click here to Skip to main content
13,003,930 members (128,314 online)
Click here to Skip to main content
Add your own
alternative version


55 bookmarked
Posted 13 Mar 2013

Face Detection with 10 Lines of Code - VB.NET

, 23 Mar 2013
Rate this:
Please Sign up or sign in to vote.
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.

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.


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)


About the Author

Dejan Mauer
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 | ;)

You may also be interested in...

Comments and Discussions

Questioni saw your code and tried to implement it ..the code is as follows but i get errors with detector object as declaration expected.plz do help Pin
Member 1268653515-Aug-16 6:53
memberMember 1268653515-Aug-16 6:53 
QuestionFace Detection is not correct Pin
Member 1185784321-Feb-16 8:09
memberMember 1185784321-Feb-16 8:09 
AnswerRe: Face Detection is not correct Pin
Dynamike010-Apr-16 20:57
memberDynamike010-Apr-16 20:57 
QuestionWebcam recognition Pin
Member 1206954420-Oct-15 22:44
memberMember 1206954420-Oct-15 22:44 
QuestionНе работает пример. Pin
servmv5-Oct-14 9:28
memberservmv5-Oct-14 9:28 
QuestionI am unable to compare any other pictures other than the test picture that came with the code Pin
Surajit Das6-Jun-14 12:31
memberSurajit Das6-Jun-14 12:31 
AnswerRe: I am unable to compare any other pictures other than the test picture that came with the code Pin
Dynamike010-Apr-16 20:58
memberDynamike010-Apr-16 20:58 
QuestionAFORGE for .Net 3.5 Pin
Regan Meloche29-May-14 6:59
memberRegan Meloche29-May-14 6:59 
QuestionEye detection Pin
SOHAM GANDHI2-Feb-14 6:25
memberSOHAM GANDHI2-Feb-14 6:25 
AnswerRe: Eye detection Pin
Dejan Mauer2-Feb-14 9:55
memberDejan Mauer2-Feb-14 9:55 
Questionplz tell me how to check for other image Pin
Member 1054581123-Jan-14 19:41
memberMember 1054581123-Jan-14 19:41 
AnswerRe: plz tell me how to check for other image Pin
Dejan Mauer23-Jan-14 21:58
memberDejan Mauer23-Jan-14 21:58 
AnswerRe: plz tell me how to check for other image Pin
Surajit Das6-Jun-14 12:36
memberSurajit Das6-Jun-14 12:36 
AnswerRe: plz tell me how to check for other image Pin
Dynamike011-Apr-16 18:19
memberDynamike011-Apr-16 18:19 
QuestionFace detection from WebCam Pin
N. Henrik Lauridsen8-Nov-13 3:11
memberN. Henrik Lauridsen8-Nov-13 3:11 
AnswerRe: Face detection from WebCam Pin
Dejan Mauer11-Nov-13 5:16
memberDejan Mauer11-Nov-13 5:16 
GeneralRe: Face detection from WebCam Pin
Member 1206954420-Oct-15 4:25
memberMember 1206954420-Oct-15 4:25 
QuestionCustom Photos Pin
Viswanathan134630-Oct-13 5:13
memberViswanathan134630-Oct-13 5:13 
AnswerRe: Custom Photos Pin
Dejan Mauer30-Oct-13 7:57
memberDejan Mauer30-Oct-13 7:57 
AnswerRe: Custom Photos Pin
Member 450547324-Nov-13 9:56
memberMember 450547324-Nov-13 9:56 
GeneralRe: Custom Photos Pin
Dejan Mauer24-Nov-13 19:02
memberDejan Mauer24-Nov-13 19:02 
QuestionCompare faces saved on a Database ? Pin
magefesa1-Jul-13 5:23
membermagefesa1-Jul-13 5:23 
AnswerRe: Compare faces saved on a Database ? Pin
Dejan Mauer1-Jul-13 5:50
memberDejan Mauer1-Jul-13 5:50 
GeneralRe: Compare faces saved on a Database ? Pin
magefesa1-Jul-13 7:28
membermagefesa1-Jul-13 7:28 
QuestionFor my thesis Pin
Angeldhianne1-Jul-13 2:40
memberAngeldhianne1-Jul-13 2:40 
AnswerRe: For my thesis Pin
Dejan Mauer1-Jul-13 5:47
memberDejan Mauer1-Jul-13 5:47 
Questioneyes positions Pin
lamardiallo4830-May-13 8:16
memberlamardiallo4830-May-13 8:16 
AnswerRe: eyes positions Pin
Dejan Mauer30-May-13 20:50
memberDejan Mauer30-May-13 20:50 
GeneralRe: eyes positions Pin
lamardiallo4814-Jun-13 0:03
memberlamardiallo4814-Jun-13 0:03 
AnswerRe: eyes positions Pin
Thornik13-Apr-14 4:20
memberThornik13-Apr-14 4:20 
GeneralRe: eyes positions Pin
Dejan Mauer13-Apr-14 20:31
memberDejan Mauer13-Apr-14 20:31 
QuestionBrilliant, what am I missing? Pin
JamGr5-Apr-13 8:28
memberJamGr5-Apr-13 8:28 
AnswerRe: Brilliant, what am I missing? Pin
Dejan Mauer6-Apr-13 6:54
memberDejan Mauer6-Apr-13 6:54 
GeneralRe: Brilliant, what am I missing? Pin
JamGr6-Apr-13 8:25
memberJamGr6-Apr-13 8:25 
GeneralGood! Pin
Meshack Musundi19-Mar-13 21:09
mvpMeshack Musundi19-Mar-13 21:09 
QuestionWhere is the project ? Pin
cmarcotte13-Mar-13 14:47
membercmarcotte13-Mar-13 14:47 
AnswerRe: Where is the project ? Pin
Dejan Mauer14-Mar-13 1:30
memberDejan Mauer14-Mar-13 1:30 
QuestionNot really an article Pin
OriginalGriff12-Mar-13 22:52
mvpOriginalGriff12-Mar-13 22:52 
AnswerRe: Not really an article Pin
Dejan Mauer12-Mar-13 22:55
memberDejan Mauer12-Mar-13 22:55 
GeneralRe: Not really an article Pin
OriginalGriff12-Mar-13 23:36
mvpOriginalGriff12-Mar-13 23:36 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170627.1 | Last Updated 24 Mar 2013
Article Copyright 2013 by Dejan Mauer
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid