john February 2016

Speed up a people detection program, written using OpenCV 3.0

I use OpenCV 3.0.0 and Visual Studio 2012. I've found this program to detect people and count them and determine direction of their motion. But it's very slow. Do you have any idea how to speed it up?

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

using namespace std;
using namespace cv;

int main (int argc, const char * argv[])
{
    VideoCapture cap("C:/Users/John/Downloads/Video/Cam.avi");              
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 100);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 110);    
    if (!cap.isOpened())
    return -1;

    Mat img;
    HOGDescriptor hog;
    hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());

    namedWindow("video capture", CV_WINDOW_AUTOSIZE);
    while (true)
    {
        cap >> img;
        if (!img.data)
        continue;

        vector<Rect> found, found_filtered;
        hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);

        size_t i, j;
        for (i=0; i<found.size(); i++)
        {
            Rect r = found[i];
            for (j=0; j<found.size(); j++)
            if (j!=i && (r & found[j])==r)
            break;
            if (j==found.size())
            found_filtered.push_back(r);
        }
        for (i=0; i<found_filtered.size(); i++)
        {
            Rect r = found_filtered[i];
            r.x += cvRound(r.width*0.1);
            r.width = cvRound(r.width*0.8);
            r.y += cvRound(r.height*0.06);
            r.height = cvRound(r.height*0.9);
            rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 2);
        }
        imshow("video capture", img);
        if (waitKey(33) >= 0)
        break;
    }
    return 0;
}

Answers


bluekid February 2016

if there is an cuda gpu try gpu version of same code

according to this

http://opencv.org/platforms/cuda.html

http://opencv.org/wp-content/uploads/2012/06/perf.png

8x times faster for pedesterian detection

https://github.com/Itseez/opencv/blob/master/samples/gpu/hog.cpp


john February 2016

First, I replace

        Rect r = found_filtered[i];
        r.x += cvRound(r.width*0.1);
        r.width = cvRound(r.width*0.8);
        r.y += cvRound(r.height*0.06);
        r.height = cvRound(r.height*0.9);
        rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 2);

with this code:

rectangle(img, Point(found_filtered[i].x ,found_filtered[i].y),   Point(found_filtered[i].x  + found_filtered[i].width, found_filtered[i].y + found_filtered[i].height ),Scalar(255,0,0),2);

and it works finely as previous. For reducing image size and detection I add and edit these codes: add:

resize(img,img,Size(),0.25,0.25,1);

and edit rectangle as below:

rectangle(img, Point(found_filtered[i].x *4,found_filtered[i].y *4),   Point(found_filtered[i].x *4 + found_filtered[i].width* 4, found_filtered[i].y *4 + found_filtered[i].height *4 ),Scalar(255,0,0),2);

but not works.

final code:

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

using namespace std;
using namespace cv;

int main (int argc, const char * argv[])
{
VideoCapture cap("C:/Users/john/Downloads/Video/file.avi");              
cap.set(CV_CAP_PROP_FRAME_WIDTH, 100);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 110);    
if (!cap.isOpened())
return -1;

Mat img;
HOGDescriptor hog;
hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());

namedWindow("video capture", CV_WINDOW_AUTOSIZE);
while (true)
{
    cap >> img;
    //resize(img,img,Size(),0.25,0.25,1);
    if (!img.data)
    continue;

    vector<Rect> found, found_filtered;
    hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);

    size_t i, j;
    for (i=0; i<found.size(); i++)
    {
        Rect r = found[i];
        for (j 

Post Status

Asked in February 2016
Viewed 1,947 times
Voted 13
Answered 2 times

Search




Leave an answer