peopledetect.cpp 3.26 KB
Newer Older
wester committed
1 2 3 4 5 6 7
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <stdio.h>
#include <string.h>
#include <ctype.h>
wester committed
8 9 10 11

using namespace cv;
using namespace std;

wester committed
12 13 14 15 16 17 18 19
// static void help()
// {
//     printf(
//             "\nDemonstrate the use of the HoG descriptor using\n"
//             "  HOGDescriptor::hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());\n"
//             "Usage:\n"
//             "./peopledetect (<image_filename> | <image_list>.txt)\n\n");
// }
wester committed
20

wester committed
21
int main(int argc, char** argv)
wester committed
22
{
wester committed
23 24 25
    Mat img;
    FILE* f = 0;
    char _filename[1024];
wester committed
26

wester committed
27
    if( argc == 1 )
wester committed
28
    {
wester committed
29 30
        printf("Usage: peopledetect (<image_filename> | <image_list>.txt)\n");
        return 0;
wester committed
31
    }
wester committed
32
    img = imread(argv[1]);
wester committed
33

wester committed
34
    if( img.data )
wester committed
35
    {
wester committed
36
        strcpy(_filename, argv[1]);
wester committed
37
    }
wester committed
38
    else
wester committed
39
    {
wester committed
40 41 42 43 44 45
        f = fopen(argv[1], "rt");
        if(!f)
        {
            fprintf( stderr, "ERROR: the specified file could not be loaded\n");
            return -1;
        }
wester committed
46 47 48 49 50 51
    }

    HOGDescriptor hog;
    hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector());
    namedWindow("people detector", 1);

wester committed
52
    for(;;)
wester committed
53
    {
wester committed
54 55
        char* filename = _filename;
        if(f)
wester committed
56
        {
wester committed
57 58 59 60 61 62 63 64 65 66 67
            if(!fgets(filename, (int)sizeof(_filename)-2, f))
                break;
            //while(*filename && isspace(*filename))
            //  ++filename;
            if(filename[0] == '#')
                continue;
            int l = (int)strlen(filename);
            while(l > 0 && isspace(filename[l-1]))
                --l;
            filename[l] = '\0';
            img = imread(filename);
wester committed
68
        }
wester committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
        printf("%s:\n", filename);
        if(!img.data)
            continue;

        fflush(stdout);
        vector<Rect> found, found_filtered;
        double t = (double)getTickCount();
        // run the detector with default parameters. to get a higher hit-rate
        // (and more false alarms, respectively), decrease the hitThreshold and
        // groupThreshold (set groupThreshold to 0 to turn off the grouping completely).
        hog.detectMultiScale(img, found, 0, Size(8,8), Size(32,32), 1.05, 2);
        t = (double)getTickCount() - t;
        printf("tdetection time = %gms\n", t*1000./cv::getTickFrequency());
        size_t i, j;
        for( i = 0; i < found.size(); i++ )
wester committed
84
        {
wester committed
85 86 87 88 89 90
            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);
wester committed
91
        }
wester committed
92
        for( i = 0; i < found_filtered.size(); i++ )
wester committed
93
        {
wester committed
94 95 96 97 98 99 100 101
            Rect r = found_filtered[i];
            // the HOG detector returns slightly larger rectangles than the real objects.
            // so we slightly shrink the rectangles to get a nicer output.
            r.x += cvRound(r.width*0.1);
            r.width = cvRound(r.width*0.8);
            r.y += cvRound(r.height*0.07);
            r.height = cvRound(r.height*0.8);
            rectangle(img, r.tl(), r.br(), cv::Scalar(0,255,0), 3);
wester committed
102
        }
wester committed
103 104 105 106
        imshow("people detector", img);
        int c = waitKey(0) & 255;
        if( c == 'q' || c == 'Q' || !f)
            break;
wester committed
107
    }
wester committed
108 109
    if(f)
        fclose(f);
wester committed
110 111
    return 0;
}