/src/extra-facedetect/main.cpp

https://bitbucket.org/realhidden/multimediaeval · C++ · 46 lines · 34 code · 7 blank · 5 comment · 4 complexity · 86ee483cc4e34eb748c6bebafd7c0b04 MD5 · raw file

  1. #include <opencv/highgui.h>
  2. #include <opencv/cv.h>
  3. #include <stdio.h>
  4. const char* cascade_name = "haarcascade_frontalface_alt.xml";
  5. int main(int argc, const char** argv)
  6. {
  7. IplImage *img = cvLoadImage(argv[1]);
  8. //Haar + memory
  9. CvMemStorage* storage = cvCreateMemStorage(0);
  10. CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
  11. if( !cascade )
  12. {
  13. fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
  14. return -1;
  15. }
  16. //detect
  17. CvSeq* faces = cvHaarDetectObjects( img, cascade, storage, 1.1, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(40, 40) );
  18. // size calculator
  19. double allsize=img->width*img->height;
  20. double thesize=allsize;
  21. // Loop the number of faces found.
  22. for( int i = 0; i < (faces ? faces->total : 0); i++ )
  23. {
  24. CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
  25. //printf("Face %d : %d %d\n",i,r->width,r->height);
  26. thesize-=r->width*r->height;
  27. }
  28. if (thesize==allsize)
  29. {
  30. printf("0");
  31. }else if (thesize<0.0f)
  32. {
  33. printf("1");
  34. }else{
  35. printf("%lf",1.0f-thesize/allsize);
  36. }
  37. cvReleaseImage(&img);
  38. return 0;
  39. }