Click here to Skip to main content
15,882,113 members
Articles / Programming Languages / C++
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.86/5 (24 votes)
17 Sep 2013CPOL3 min read 272.2K   9.6K   64   39
How to detect and recognize faces using OpenCV (step by step instructions for beginners)

Introduction

I have to face many difficult situations when I configure OpenCV on Windows 7 using Visual Studio 2012, install Python to run the script crop_face.py, 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.

Image 1

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

Image 2

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:

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

to:

C++
#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:

Image 3

3.2. Linker -> General

Image 4

3.3 Linker -> Input

Image 5

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
Note

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

Step 5: Install Python to run script crop_face.py

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 crop_face.py 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:

Image 6

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

Image 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.

 Image 8

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):

Image 9 

F5 and enjoy. 

Image 10

Conclusion

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

License

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


Written By
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionUpgrade to a more recent version of OpenCV Pin
Eric Schwerzel3-Mar-21 12:41
Eric Schwerzel3-Mar-21 12:41 
QuestionIssue with prediciton Pin
Daryl Goh22-Mar-17 2:48
Daryl Goh22-Mar-17 2:48 
QuestionAyuda!!!!! Pin
Member 1270656127-Aug-16 6:31
Member 1270656127-Aug-16 6:31 
Questionno detect faces Pin
Member 1268455617-Aug-16 21:04
Member 1268455617-Aug-16 21:04 
Questionimage based face recognition Pin
Member 1245305622-May-16 20:57
Member 1245305622-May-16 20:57 
QuestionHow to create CSV file? Pin
Member 1054330128-Apr-16 9:03
Member 1054330128-Apr-16 9:03 
Questionthanks DangBao, is this code for C# or C++ Pin
jai Lopez25-Apr-16 9:40
jai Lopez25-Apr-16 9:40 
im looking to merge a openCV code with the AI C#/C++ version found on this site
AnswerRe: thanks DangBao, is this code for C# or C++ Pin
DangBao25-Apr-16 11:21
DangBao25-Apr-16 11:21 
QuestionHOW TO INSTALL PIL Pin
Lovetronic21-Dec-15 17:37
Lovetronic21-Dec-15 17:37 
GeneralMời hợp tác Pin
ngonguyenthuyet20-May-15 23:06
ngonguyenthuyet20-May-15 23:06 
QuestionIndentation error while executing the crop_face.py source code Pin
Member 1168380211-May-15 23:38
Member 1168380211-May-15 23:38 
Questiongiving me alwys my name .. Pin
Elkanah modi19-Apr-15 19:58
Elkanah modi19-Apr-15 19:58 
QuestionMy thankful Pin
Member 115735471-Apr-15 11:07
Member 115735471-Apr-15 11:07 
Questionerror message in visual studio 2012 Pin
Member 1146516719-Feb-15 15:04
Member 1146516719-Feb-15 15:04 
Questioni have an error LNK1104: cannot open file 'oprncv_core245d.lib' Pin
Member 1124894326-Nov-14 22:01
Member 1124894326-Nov-14 22:01 
QuestionAlways showing the predicted name of the first entry in CSV.ext file Pin
coder02930-Sep-14 2:05
coder02930-Sep-14 2:05 
Questionerro Pin
黃奕傑1-Sep-14 23:20
黃奕傑1-Sep-14 23:20 
Questionchange to your name Pin
hongshiong9-Jul-14 23:01
hongshiong9-Jul-14 23:01 
Questioni can't open file DetectsAndRecognizeFaces.zip Pin
Member 106088629-Jul-14 19:08
Member 106088629-Jul-14 19:08 
QuestionAdding a feature Pin
Member 107703601-May-14 1:00
Member 107703601-May-14 1:00 
Questioninstructions hard to follow Pin
Anthony Griffiths20-Apr-14 4:09
Anthony Griffiths20-Apr-14 4:09 
AnswerRe: instructions hard to follow Pin
DangBao20-Apr-14 15:43
DangBao20-Apr-14 15:43 
QuestionSuggestion to complete the task Pin
DocRobo17-Apr-14 9:42
DocRobo17-Apr-14 9:42 
AnswerRe: Suggestion to complete the task Pin
DangBao18-Apr-14 3:40
DangBao18-Apr-14 3:40 
QuestionException thrown Pin
Najmuzzaman Mohammad9-Apr-14 7:41
Najmuzzaman Mohammad9-Apr-14 7:41 

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.