A way to count how many people have been detected in faceDetect.cpp

asked 2015-08-26 16:08:49 -0600

franken gravatar image

I am running the code faceDetect.cpp , i need to count how many people are beign detected , i can detect how many people are in in the same frame by incrementing a variable for each face detected but in the case for example if the person appear , disapear and re-appear i couldn't find a way to count it as just one person the complete code can be found here

https://github.com/Itseez/opencv/blob...

we are interrested in this part of the code (the function detectAndDraw) where the face and the eye are beign detected

void detectAndDraw( Mat& img, CascadeClassifier& cascade,
                CascadeClassifier& nestedCascade,
                double scale, bool tryflip )
{
    int i = 0;
    double t = 0;
    vector<Rect> faces, faces2;
    const static Scalar colors[] =  { CV_RGB(0,0,255),
        CV_RGB(0,128,255),
        CV_RGB(0,255,255),
        CV_RGB(0,255,0),
        CV_RGB(255,128,0),
        CV_RGB(255,255,0),
        CV_RGB(255,0,0),
        CV_RGB(255,0,255)} ;
    Mat gray, smallImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 );

cvtColor( img, gray, COLOR_BGR2GRAY );
resize( gray, smallImg, smallImg.size(), 0, 0, INTER_LINEAR );
equalizeHist( smallImg, smallImg );

t = (double)cvGetTickCount();
cascade.detectMultiScale( smallImg, faces,
    1.1, 2, 0
    //|CASCADE_FIND_BIGGEST_OBJECT
    //|CASCADE_DO_ROUGH_SEARCH
    |CASCADE_SCALE_IMAGE
    ,
    Size(30, 30) );
if( tryflip )
{
    flip(smallImg, smallImg, 1);
    cascade.detectMultiScale( smallImg, faces2,
                             1.1, 2, 0
                             //|CASCADE_FIND_BIGGEST_OBJECT
                             //|CASCADE_DO_ROUGH_SEARCH
                             |CASCADE_SCALE_IMAGE
                             ,
                             Size(30, 30) );
    for( vector<Rect>::const_iterator r = faces2.begin(); r != faces2.end(); r++ )
    {
        faces.push_back(Rect(smallImg.cols - r->x - r->width, r->y, r->width, r->height));
    }
}
t = (double)cvGetTickCount() - t;
printf( "detection time = %g ms\n", t/((double)cvGetTickFrequency()*1000.) );
for( vector<Rect>::const_iterator r = faces.begin(); r != faces.end(); r++, i++ )
{
    Mat smallImgROI;
    vector<Rect> nestedObjects;
    Point center;
    Scalar color = colors[i%8];
    int radius;

    double aspect_ratio = (double)r->width/r->height;
    if( 0.75 < aspect_ratio && aspect_ratio < 1.3 )
    {
        center.x = cvRound((r->x + r->width*0.5)*scale);
        center.y = cvRound((r->y + r->height*0.5)*scale);
        radius = cvRound((r->width + r->height)*0.25*scale);
        circle( img, center, radius, color, 3, 8, 0 );
    }
    else
        rectangle( img, cvPoint(cvRound(r->x*scale), cvRound(r->y*scale)),
                   cvPoint(cvRound((r->x + r->width-1)*scale), cvRound((r->y + r->height-1)*scale)),
                   color, 3, 8, 0);
    if( nestedCascade.empty() )
        continue;
    smallImgROI = smallImg(*r);
    nestedCascade.detectMultiScale( smallImgROI, nestedObjects,
        1.1, 2, 0
        //|CASCADE_FIND_BIGGEST_OBJECT
        //|CASCADE_DO_ROUGH_SEARCH
        //|CASCADE_DO_CANNY_PRUNING
        |CASCADE_SCALE_IMAGE
        ,
        Size(30, 30) );
    for( vector<Rect>::const_iterator nr = nestedObjects.begin(); nr != nestedObjects.end(); nr++ )
    {
        center.x = cvRound((r->x + nr->x + nr->width*0.5)*scale);
        center.y = cvRound((r->y + nr->y + nr->height*0.5)*scale);
        radius = cvRound((nr->width + nr->height)*0.25*scale);
        circle( img, center, radius, color, 3, 8, 0 );
    }
}
cv::imshow( "result", img );

}

note that the color is changed every time a new face is detected , that is the case when multiple image are detected in the same frame , but i am interrested in one person where the color normally doesn't change

any hints ?

edit retag flag offensive close merge delete

Comments

1

imho , you misunderstood the purpose (and limits) of the object detection.

above code just detects faces. it does not know, "who it is" (that would be recognition) or what happened in the last frame (that would be tracking)

berak gravatar imageberak ( 2015-08-27 03:02:27 -0600 )edit

@berak i think that recognition is impossible since my application turn in real time , i'm just looking for a trick to estimate the number of persons , or maybe there is nothing i can do , thanks

franken gravatar imagefranken ( 2015-08-27 03:09:33 -0600 )edit
1

don't give up too early, though !

  • if i understood you right, you're trying to "estimate the number of (unique) persons" over a certain time ? (the current number of persons is what you already got, right ?)

  • "i think that recognition is impossible since my application turn in real time" - to my exp. a simple recognition scheme (rescale found faces to a fixed size, keep last 20 in memory, compare with norm(), to see, who is 'new') will only take fractions of the (quite expensive) cascade detection.

berak gravatar imageberak ( 2015-08-27 03:39:20 -0600 )edit

@berak yes just in time the person is viewing the camera , itt's about beign able to measure the audiance for a smart billboard wich is equiped with a camera

  • i was thinking about getting the coordinate of the face if it's detected and if it is not going way from and interval 10 < x < 30 and for y too , i can see that the face detected is in the same place and so it's the same person .......
  • and for the recognition , i don't know much about it or how to integrate it with the actual code !!!
franken gravatar imagefranken ( 2015-08-29 14:09:08 -0600 )edit