Basic Face Detection, Opencv 3 Visual Studio 2015

Basic Opencv Face Detection Tutorial

Basic Opencv C++ tutorial how to detect the face from video image and any source you can achieve.
If you have little bit skills with programing you can just achieve the result under 10 minutes. I would like to reccomend also the installation by NUGET package. It is the fastest approach in Visual Studio how to start coding with opencv.. 

Problem with CascadeClassifier detectMultiScale Opencv 3.0.0. Windows prebuild libs

Problem is that in many cases with 32 bit Opencv 3.0.0 Windows version in debug and in release mode. I thing any of haarcascade_xxx return vector<Rect> Faces (testing number of cascades is 7). The length of the vector is incomprehensibly bigger than I can expect with my parameters and experiences with previous Opencv version..

vector<Rect> of detection is to big 

This is some kind of bug that I try to find in opencv source. What is surprising is fact that 64 bit version of Opencv works fine in my case. 

 I am using Visual Studio 2015 with 64 bit Opencv 3.0.0

 Here is my tutorial how to install Opencv 3.0.0 libs for Visual Studio 2015. 
Install Opencv for Visual Studio 2015
You can simple prepare the project inside the Visual Studio 2015 by Nuget Packages. This approach is easy for beginers and better than standard installation with all the environmental variables problems. Just follow the installation steps inside here 

Face detection basic Opencv C++ code example

There is nothing special, but you can also find the example how to replace the face with transparent mask or any pictures. This is at least fun. 



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

using namespace cv;
using namespace std;


int main(int argc, char** argv)
{
 // capture from web camera init

 VideoCapture cap(0);
 cap.open(0);

 Mat img;

// Load cascate classifier placed in sulution folder
 CascadeClassifier detector;
 string cascadeName = "haarcascade_frontalface_alt.xml";
 bool loaded = detector.load(cascadeName);
// Parameters of detectMultiscale Cascade Classifier
 int groundThreshold = 2;
 double scaleStep = 1.1;
 Size minimalObjectSize(80, 80);
 Size maximalObjectSize(200, 200);

// Vector of returned faces
 vector found;

 for (;;)
 {
 
      // Image from camera to Mat

      cap >> img;
     // Convert input to greyscale 
      Mat image_grey;
      cvtColor(img, image_grey, CV_BGR2GRAY);

      // why not
      found.clear();
  
      // Detect faces
      detector.detectMultiScale(image_greyHead, found, scaleStep, groundThreshold, 0         | 2, minimalObjectSize,maximalObjectSize);
      // Draw the results into mat retrieved from webcam
      if (found.size() > 0) {
            for (int i = 0; i <= found.size() - 1; i++) {
             rectangle(img, found[i].br(), found[i].tl(), Scalar(0, 0, 0), 1, 8, 0);
             }
      }

    //Show the results

     imshow("wooohooo", img);
     int key2 = waitKey(20);

 }
 return 0;
}

Next Post Previous Post
18 Comments
  • stperic
    stperic April 22, 2016 at 2:40 PM

    I have got this error:xxx.cpp(56): error C2065: 'image_greyHead': undeclared identifier

    Can you share the value of this identifier?

  • fakhri wu
    fakhri wu May 15, 2016 at 12:27 PM

    'image_greyHead': undeclared identifier what should i do?

    • RaulBad
      RaulBad June 2, 2016 at 9:06 PM

      Replace image_greyHead to image_grey lol

  • Tony Muilenburg
    Tony Muilenburg June 18, 2016 at 8:28 PM

    also point to the correct folder for the haar classifer

  • YouLoseBellyFat
    YouLoseBellyFat September 4, 2016 at 5:09 AM

    VBasic Code Examples

  • Unknown
    Unknown October 7, 2016 at 12:53 AM

    The throw : Read access to offense.

    this이(가) 0x445FF8였습니다.

    If you have a program for this exception handler can continue safely.


    template inline
    Point_<_Tp> Rect_<_Tp>::br() const
    {
    return Point_<_Tp>(x + width, y + height); <<error part
    }

  • Unknown
    Unknown March 8, 2017 at 11:57 PM

    I got exception like "Unhandled exception at 0x000007FEFD619E5D in OpenCVTest.exe: Microsoft C++ exception: cv::Exception at memory location 0x00000000001BF1F0."
    what should I do

  • Unknown
    Unknown March 16, 2017 at 6:41 AM

    mine gives error on vector. I fixed it vector found. but why it cant detect my face?

    • Unknown
      Unknown March 18, 2017 at 2:54 AM

      how did you fixed it ?

    • Unknown
      Unknown June 25, 2017 at 7:57 AM

      This comment has been removed by the author.

  • Unknown
    Unknown May 17, 2017 at 4:59 AM

    This comment has been removed by the author.

    • Unknown
      Unknown May 17, 2017 at 5:00 AM

      This comment has been removed by the author.

  • Unknown
    Unknown January 21, 2018 at 2:35 AM

    'std::vector': use of class template requires template argument list this is error
    can you provide me some idea

    • Unknown
      Unknown July 16, 2018 at 12:01 AM

      write vector found; instead of vector found;
      Worked for me. Probably not gonna be useful for you since you might have already solved it but I hope it can help someone one day

    • Unknown
      Unknown July 16, 2018 at 12:03 AM

      welp sorry, meant that you have to enter Rect into brackets next to vector. Idk why I can't right it in comment section
      <Rect

  • Unknown
    Unknown May 7, 2018 at 5:50 AM

    The information in this blog is handy for the people. Face Recognition App

  • Unknown
    Unknown August 4, 2018 at 11:25 PM

    This comment has been removed by the author.

  • Unknown
    Unknown August 4, 2018 at 11:35 PM

    fixed file,you can download it from:
    https://goo.gl/cQmgw2

Add Comment
comment url