Click here to Skip to main content
Click here to Skip to main content

Tagged as

Detect and Recognize Faces using OpenCV for Beginners - Step by Step

, 17 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to detect and recognize faces using OpenCV (step by step instructions for beginners)


I have to face many difficult situations when I configure OpenCV on Windows 7 using Visual Studio 2012, install Python to run the script, and create test data to detect and recognize my faces. So I decided to write out my results from beginning to end to detect and recognize my faces. I hope this report will be useful for beginners to study OpenCV.

The content of this report is to guide in a step by step manner to enable the reader to configure and run OpenCV on Windows. I do not focus on explaining the meaning of OpenCV library. You can get more details about OpenCV here.

I have copied all the source code for this report from this article and refer to the guide to configure OpenCV on Windows from many other websites.

In this report, I used OpenCV 2.4.5 on Windows 7 using Visual Studio 2012.

Let’s start.

Step 1: Download OpenCV 2.4.5

You can download OpenCV2.4.5 on Windows from here.

You extract OpenCV-2.4.5 with the specified path. For example:

Step 2: Create a New C++ Project in Visual Studio 2012

Open Visual Studio 2012.

Select File -> New -> Project

Create empty project Win32 Application in Visual C++.

Create a new file *.cpp. I named it facerec_video.cpp.

Copy the source code src/facerec_video.cpp from here.

Please change this source code:

#include "opencv2/core.hpp"
#include "opencv2/contrib.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/objdetect.hpp"


#include <opencv2/core/core.hpp>
#include <opencv2/contrib/contrib.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>

Step 3: Configure OpenCV in Visual Studio 2012

3.1. Configuration Properties -> C/C++ -> General -> Additional Include Directories

Add your OpenCV path to Additional Include Directories:

3.2. Linker -> General

3.3 Linker -> Input

Please press F7 -> Build successful (If your project has an error, please review from step 1 to step 3 carefully.)

Step 4: Setup Windows Environment

Control Panel\All Control Panel Items\System

Click ‘Advanced system settings’

  • Click ‘Environment Variables’ in Tab Advanced
  • Click New… in ‘User variables for Administrator’
    • Variable name: OPENCV_DIR
    • Variable value: D:\01_baotd1\00_OpenSource\OpenCV_245\build
  • Select Path in ‘System Variables’ and click Edit…
  • Add to the end: ;%OPENCV_DIR%\x86\vc11\bin

If you cannot run the OpenCV, you must restart your computer or your Visual Studio.

Step 5: Install Python to run script

In this report, I used Python and library image as follows:

  • python-2.7
  • PIL-1.1.7.win32-py2.7

Step 6: Create Test Data to OpenCV Recognize Your Faces

In order for OpenCV to recognize your faces, you need to create your faces. You should prepare one or more of your pictures with extensions *.jpg (OpenCV can detect and recognize more image types. In my report, I used image *.jpg) and placed them in one folder. In order to run script correctly, you must provide the position of left eye and right eye. I used Paint to get the position of my left eye and right eye. You move your mouse to the left eye position, the status bar of Paint will display the position (x,y) of left eye. Please note this position. And do the same process for right eye and other images. Please take a look at my example below:

I updated the script for creating image easily. I attached the updated script in this report. Here is the example to run on Windows 7:

Step 7: Create csv.ext File

Here is the example of csv.ext file. When you crop your faces successfully, you should put all your crop faces to one folder. Please notice that the csv.ext file must contain at least two objects.


Step 8: Run and Enjoy

In order to easily run, I modified the original source code as follows (I do not like to run the application with command line):


F5 and enjoy. 


This is the first report that I created on CodeProject. I hope beginners will get some benefit from this report.


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


About the Author


Vietnam Vietnam
No Biography provided

Comments and Discussions

Questionerror message in visual studio 2012 PinmemberMember 1146516719-Feb-15 16:04 
Questioni have an error LNK1104: cannot open file 'oprncv_core245d.lib' PinmemberMember 1124894326-Nov-14 23:01 
QuestionAlways showing the predicted name of the first entry in CSV.ext file Pinmembercoder02930-Sep-14 3:05 
Questionerro Pinmember黃奕傑2-Sep-14 0:20 
Questionchange to your name Pinmemberhongshiong10-Jul-14 0:01 
Questioni can't open file PinmemberMember 106088629-Jul-14 20:08 
QuestionAdding a feature PinmemberMember 107703601-May-14 2:00 
Questioninstructions hard to follow PinmemberAnthony Griffiths20-Apr-14 5:09 
AnswerRe: instructions hard to follow PinmemberDangBao20-Apr-14 16:43 
QuestionSuggestion to complete the task PinmemberDocRobo17-Apr-14 10:42 
AnswerRe: Suggestion to complete the task PinmemberDangBao18-Apr-14 4:40 
QuestionException thrown PinmemberNajmuzzaman Mohammad9-Apr-14 8:41 
AnswerRe: Exception thrown PinmemberDangBao18-Apr-14 4:46 
Questionadding group classification [modified] PinmemberHao Ran28-Mar-14 5:56 
AnswerRe: adding group classification PinmemberDangBao28-Mar-14 14:22 
QuestionBlank Grey Screen PinmemberMember 1055669025-Mar-14 5:37 
AnswerRe: Blank Grey Screen PinmemberDangBao28-Mar-14 14:13 
GeneralThe Aligning Face Image ( PinmemberMember 1060127416-Feb-14 22:17 
GeneralRe: The Aligning Face Image ( PinmemberDangBao17-Feb-14 4:35 
Questionthanks Pinmembertonylander7-Jan-14 9:19 
QuestionFace detection and recognition PinmemberDeepika Ganesan6-Nov-13 2:05 
AnswerRe: Face detection and recognition PinmemberDangBao6-Nov-13 11:51 
Questionmaybe i should learn python well Pinmemberqianlizhixing14-Oct-13 17:40 
Questioninteresting article! ^^ PinmemberJuly Kieu20-Sep-13 7:45 
GeneralVery good Pinmemberluunhat040619-Sep-13 17:42 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150305.1 | Last Updated 18 Sep 2013
Article Copyright 2013 by DangBao
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid