Ask Your Question
1

Simply count number of faces?

asked 2012-09-28 05:52:14 -0600

cts gravatar image

Hello,

for a halloween project I am looking for a way to simply count the number of faces in a picture given (the picture comes from my webcam, grabbed by the motion tool under linux).

The C example is, well, a little over my head: Could someone kindly point out to me where to change the facedetect.cpp code so as to only return the number of faces found, then exit or exit with returncode 0 if no faces have been found, 1 if >0 faces have been found (which would be even simpler for my usage under bash)?

Thank you very much, also in the same of my hopefully very scared daughter ;)

edit retag flag offensive close merge delete

Comments

1

I would recommend you to start with a simple C/C++ tutorial. Everything else will come along nicely.

sammy gravatar imagesammy ( 2012-09-28 05:56:11 -0600 )edit
1

A simple recipe for Dragon Stew: Step 1: Find a few kilos of well aged dragonmeat...

cts gravatar imagects ( 2012-09-28 06:09:27 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
5

answered 2012-09-28 06:25:40 -0600

updated 2012-09-28 06:31:39 -0600

You could try something as simple as this:

//This program outputs the number of faces detected on an image
//It receives the path to an image as a command line argument
//Will print on the standard output the number of faces detected
//Will return 0 if at least 1 face is detected
//Will return 1 if no faces are detected
//Will return 2 if some system call fails

#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>

int main( int argc, char **argv )
{

  if(argc != 2)
  {
    std::cerr << "USE: " << argv[0] << " path-to-image" << std::endl;
    return 2;
  }

  //Location of the classifiers of faces
  //REPLACE IT WITH THE APROPRIATE PATH
  std::string faceCascadeName = "/usr/share/OpenCV-2.3.1/OpenCV/lbpcascades/lbpcascade_frontalface.xml";

  //Create the classifiers
  cv::CascadeClassifier faceCascade;

  //Load the classifiers
  if (!faceCascade.load( faceCascadeName ))
  {
    std::cerr << "Could not load face classifier" << std::endl;
    return 2; 
  }


  //The captured frame
  cv::Mat frame;
  frame = cv::imread(argv[1]);
  if(frame.empty())
  {
    std::cerr << "Could not load image " << argv[1] << std::endl;
    return 2;
  }

  //This will contain the output of the face detector
  std::vector<cv::Rect> faces;

  //Preprocess the image
  cv::Mat frameGray;
  cv::cvtColor( frame, frameGray, CV_BGR2GRAY );
  cv::equalizeHist( frameGray, frameGray );

  //Detect the face
  faceCascade.detectMultiScale( frameGray, faces, 1.1, 2, 0, cv::Size(80,80));

  std::cout << "Number of detected faces: " << faces.size() << std::endl;

  //If no faces are detected return 1
  if (faces.size() == 0) return 1;

  //Number of detected faces was greater than 0.
  return 0;
}

But please, don't scare your daughter too much... BAD DADDY!! ;D

P.S.: I did this on the fly, I don't even know if it compiles. Also, keep in mind that false positives might occur.

edit flag offensive delete link more

Comments

wow. WOW. Yes, it does compile, and yes, it does work. wow. "on the fly"... amazing. Thank you! Btw, your code is more readable to a non-native C speaker like me, so it is a much more suitable "entry point" into learning. If I could give the OpenCV gang a recommendation: Add this to the examples, please.

cts gravatar imagects ( 2012-09-28 06:39:14 -0600 )edit
1

Thank you, you are very kind :)

Martin Peris gravatar imageMartin Peris ( 2012-09-28 23:17:45 -0600 )edit
0

answered 2013-04-25 05:32:53 -0600

mehdi gravatar image

For Linux i found the XML Classifier under another directory /home/myname/opencv_version/data/lbpcascades

thanks

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-09-28 05:52:14 -0600

Seen: 5,809 times

Last updated: Apr 25 '13