In this article, you will learn an easy way to detect your face and eyes by using OpenCV.

OpenCV (Open Source Computer Vision) is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications. Written in optimized C/C++, the library can take advantage of multi-core processing.

Adopted all around the world, OpenCV has more than 47 thousand people of user community and estimated number of downloads exceeding 9 million. Usage ranges from interactive art, to mines detection, online maps and advanced robotics.


Step 1:Install Visual Studio 2017 and OpenCV

1.Install Visual Studio 2017 on your computer

Head over to and download Visual Studio Professional 2015. Unzip the downloaded file and double-click the 'vs_professional.exe', then the installation process will begin.

2.Install OpenCV

1) Head over to the site: and download the latest version of OpenCV (shown in the following figure). Choose the version according to your operating system.

In this tutorial we are going to install OpenCV 3.1 using Visual Studio 2015 professional on a 64-bit system running Windows 10.

2) Extract the downloaded OpenCV file Double click the downloaded OpenCV file, and then extract it

Step 2: Set the Environment Variables

1.To do this step, open the Control Panel and then System. Click the Advanced System Settings, last Environment Variables in turns as show in the following figure.

2.Edit the PATH environment variables and Add a new environment variable, then give it the value of F:\opencv\build\x64\vc14\bin. Note that change the value depends on the path where you have extracted your OpenCV in step 2.

Step 3: Create a new project in Visual Studio 2017

1.In Visual Studio 2017, create a new project to follow the steps in turns as the following figure shows

2.Select Win32 Console Application in Visual C++, then name your project and select a directory to store it

3.Choose the empty project and click finish

4.Add a new cpp file

Step 4: Configure OpenCV in Visual Studio 2017

1.Open the Property Manager and double click Debug|Win64

2.Select "Include Directories", and give it the following values:




Remember that change the value depending on the path you have extracted your OpenCV files to in step 2.

3.Add Library Directories, give the value of F:\opencv\build\x64\vc14\lib. Remember that changing the values depends on the path where you have extracted your OpenCV in step 2

4.Add additional dependences

Copy the following item and paste it in additional Dependences blankopencv_world310d.lib

Step 5: Paste the following code to the .cpp file your added in step 4.


using namespace std;
using namespace cv;

string harrEye = "..\\..\\..\\resources\\harr\\haarcascade_eye_tree_eyeglasses.xml";
string harrFace = "..\\..\\..\\resources\\harr\\haarcascade_frontalface_alt.xml";
CascadeClassifier faceCascade;
CascadeClassifier EyeCascade;
string windownName = "Capture faces and eyes ";
void detectAndDiapley(Mat frame);

int main()
    Mat frame;

    // load the cascades
    if (!EyeCascade.load(harrEye))
        cout << "load harrEye failed" << endl;
    if (!faceCascade.load(harrFace))
        cout << "load harrFace failed" << endl;

    // read the video stream
    VideoCapture capture(0);
    if (capture.isOpened())
        while (true)
            capture >> frame;

            // apply the cascaders to the frame
            if (!frame.empty())
                cout << "input video frame is empty" << endl;
            if (waitKey(30) >= 0)break;
    return 0;

void detectAndDiapley(Mat frame)
    vector<Rect> faces;
    Mat frameGray;
    cvtColor(frame, frameGray, CV_BGR2GRAY);
    equalizeHist(frameGray, frameGray);

    //Detect faces
    faceCascade.detectMultiScale(frameGray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
    for (int i = 0; i < faces.size(); i++)
        Point Vertex1(faces[i].x, faces[i].y);
        Point Vertex2(faces[i].x + faces[i].width, faces[i].y + faces[i].height);
        rectangle(frame, Vertex1, Vertex2, Scalar(0, 0, 255), 2, 8, 0);
        Mat faceROI = frameGray(faces[i]);
        vector<Rect> eyes;

        // detect eyes in each face
        EyeCascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
        for (int j = 0; j < eyes.size(); j++)
            Point center(faces[i].x + eyes[j].x + eyes[j].width / 2, faces[i].y + eyes[j].y + eyes[j].height / 2);          
      int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
            Size axes(eyes[j].width / 2, 13);
            ellipse(frame, center, axes, 0, 0, 360, Scalar(255, 255, 0), 2, 8, 0);
    // show the faces and eyes detected
    imshow(windownName, frame);

Step 6: Debug your Project

Set two options as following figure shows:

Press F5 to execute the face detection project, your PC camera will turn on and your face and eyes will be highlighted like so:


1. 2.