People Detection in OpenCV again

There are a number of enquiries about the people detection video I did a while ago. It is a step by step explanation of what I have done. I use the XCode 4 in OSX Lion with OpenCV 2.3 to try out the following.

The first step is to download and build the latest OpenCV 2.3 into the folder at /Developer/OpenCV-2.3.0. The headers are in the include folder. Please note that you may have to copy the individual include folders from the modules folder. I build the shared libraries in the lib/Release folder.

The code is a modification of the sample peopledetect.cpp.

The second step is to display the video capture image. I use the example from the C++ reference manual in the highgui section.

#include <iostream>
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main (int argc, const char * argv[])
{
    VideoCapture cap(CV_CAP_ANY);
    if (!cap.isOpened())
        return -1;
 
    Mat img;
    namedWindow("video capture", CV_WINDOW_AUTOSIZE);
    while (true)
    {
        cap >> img;
        imshow("video capture", img);
        if (waitKey(10) >= 0)
            break;
    }
    return 0;
}

The last step is to combine the two examples into one, with a little adjustment of the detection parameters and the display rectangle size.

#include <iostream>
#include <opencv2/opencv.hpp>
 
using namespace std;
using namespace cv;
 
int main (int argc, const char * argv[])
{
    VideoCapture cap(CV_CAP_ANY);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);    
    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(20) >= 0)
            break;
    }
    return 0;
}

Please note that the performance is pretty slow even though the capture size is 320 x 240.

This entry was posted in Testing and tagged , . Bookmark the permalink. Both comments and trackbacks are currently closed.

21 Comments

  1. NIKHIL
    Posted 2014/10/13 at 9:37 am | Permalink

    SO IF I REPLACE THE VIDEO WITH AN IMAGE THIS CODE DETECTS NUMBER OF PEOPLE IN IT
    ?

  2. badri
    Posted 2014/02/18 at 11:25 pm | Permalink

    frame rate a bit slow when i executed it
    pls help

  3. Bruna
    Posted 2013/09/09 at 10:21 pm | Permalink

    hi,

    I changed the program entry for images (. Jpg) to envez webcam,
    Now the image is processed several times, and what happens is that every time the detection is better, ie nearest the person, you know why? as it is the same picture going through the same code would not match all probes?
    thank you

  4. Daniel
    Posted 2013/08/06 at 11:15 am | Permalink

    Hi Bryan, I’m trying to compile your program but I have some mistakes, could you please help me? the error is as follows …..Undefined symbols for architecture x86_64:
    “cv::namedWindow(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, int)”, referenced from:
    _main in main.o

  5. Hasan
    Posted 2013/06/23 at 7:34 am | Permalink

    May I ask about the 3party libraries needed to run this code
    my compiler requested TBB, do we need other libraries?
    Thanks a lot

  6. Posted 2013/05/22 at 11:35 am | Permalink

    Hi Bryan I have a problem when I run the code I boot the following error referred to hodgdescriptor:
    rror LNK2019: external symbol “public: virtual void __ cdecl cv :: HOGDescriptor :: setSVMDetector (class cv :: _InputArray const &)” (? setSVMDetector @ HOGDescriptor @ cv @ @ UEAAXAEBV_InputArray @ 2 @ @ Z) unresolved at that becomes wmain referenced in function
    1> ConsoleApplication122.obj: error LNK2001: unresolved external symbol “public: virtual bool __ cdecl cv :: HOGDescriptor :: read (class cv :: FileNode &)” (read @ HOGDescriptor @ cv @ @ UEAA_NAEAVFileNode @ 2 @ @ Z) without resolve
    1> ConsoleApplication122.obj: error LNK2001: unresolved external symbol “public: virtual void __ cdecl cv :: HOGDescriptor :: write (class cv :: FileStorage &, class std :: basic_string <char, struct std :: char_traits , class std :: allocator> const &) const ” unresolved
    1> ConsoleApplication122.obj: error LNK2001: unresolved external symbol “public: virtual bool __ cdecl cv :: HOGDescriptor :: load (class std :: basic_string <char, struct std :: char_traits , class std :: allocator > const &, class std :: basic_string <char, struct std :: char_traits , class std :: allocator> const &) “(? load @ HOGDescriptor @ cv @ @ UEAA_NAEBV? $ basic_string @ DU? $ char_traits @ D @ std @ @ V? $ allocator @ D @ 2 @ @ std @ @ 0 @ Z) unresolved
    1> ConsoleApplication122.obj: error LNK2001: unresolved external symbol “public: virtual void __ cdecl cv :: HOGDescriptor :: save (class std :: basic_string <char, struct std :: char_traits , class std :: allocator > const &, class std :: basic_string <char, struct std :: char_traits , class std :: allocator> const &) const “(? save @ HOGDescriptor @ cv @ @ UEBAXAEBV? $ basic_string @ DU ? $ char_traits @ D @ std @ @ V? $ allocator @ D @ 2 @ @ std @ @ 0 @ Z) unresolved
    1> ConsoleApplication122.obj: error LNK2001: unresolved external symbol “public: virtual void __ cdecl cv :: HOGDescriptor :: copyTo (struct cv :: HOGDescriptor &) const” (? CopyTo @ HOGDescriptor @ cv @ @ UEBAXAEAU12 @ @ Z) unresolved
    1> ConsoleApplication122.obj: error LNK2001: unresolved external symbol “public: virtual void __ cdecl cv :: HOGDescriptor :: compute (class cv :: Mat const &, class std :: vector <float, class std :: allocator > &, class cv :: size_ , class cv :: size_ , class std :: vector <cv :: Point_ class, class std :: allocator <class cv :: Point_ > > const &) const ” unresolved
    1> ConsoleApplication122.obj: error LNK2001: unresolved external symbol “public: virtual void __ cdecl cv :: HOGDescriptor :: detect (class cv :: Mat const &, class std :: vector <cv :: Point_ class, class std :: allocator <class cv :: Point_ >> &, class std :: vector <double, class std :: allocator > &, double, cv :: size_ class, class cv :: size_ , class std :: vector <cv :: Point_ class, class std :: allocator <class cv :: Point_ >> const &) const ” unresolved
    1> ConsoleApplication122.obj: error LNK2001: unresolved external symbol “public: virtual void __ cdecl cv :: HOGDescriptor :: detect (class cv :: Mat const &, class std :: vector <cv :: Point_ class, class std :: allocator <class cv :: Point_ >> &, double, cv :: size_ class, class cv :: size_ , class std :: vector <class cv :: Point_ , class std :: allocator <class cv :: Point_ >> const &) const ” unresolved
    1> ConsoleApplication122.obj: error LNK2001: unresolved external symbol “public: virtual void __ cdecl cv :: HOGDescriptor :: detectMultiScale (class cv :: Mat const &, class std :: vector <cv :: Rect_ class, class std :: allocator <class cv :: Rect_ >> &, class std :: vector <double, class std :: allocator > &, double, cv :: size_ class, class cv :: size_ , double, double, bool) const ” unresolved
    1> ConsoleApplication122.obj: error LNK2019: external symbol “public: virtual void __ cdecl cv :: HOGDescriptor :: detectMultiScale (class cv :: Mat const &, class std :: vector <cv :: Rect_ class, class std :: allocator <class cv :: Rect_ >> &, double, cv :: size_ class, class cv :: size_ , double, double, bool) const ” unsolved referred to in function wmain
    how could I solve this problem

    • Ginson
      Posted 2013/10/15 at 12:49 am | Permalink

      Are you using x64 lib or x86 lib??? If you are using x64 machine and have x86 opencv library path in the linker or vice versa you’ll get this error. Please check if you are using the correct libraries(x64 or x86).

      If still the problem persists then go to solution configurations(next to step out control)
      on the drop down select configaration manager and select the appropriate configurations.

  7. Sahar
    Posted 2013/05/18 at 10:57 pm | Permalink

    Hi, i’m trying to implement the “HOG for human detection” so i used a part of your code but instead i loaded an image, but the results weren’t good at all, i was wondering if you have any information on what i should do. thanks

  8. Aditya
    Posted 2012/12/06 at 3:09 pm | Permalink

    this code is recognising my friends t-shirt hanging on wall rather than me.. plzz guide me with somechange i can do to make this work better ..

  9. mukund
    Posted 2012/10/31 at 8:08 pm | Permalink

    I am very glad that you help people learn by sharing your work.Can you please tell me what modifications has to be made to run the code on windows 7

    • mukund
      Posted 2012/11/20 at 2:37 am | Permalink

      sir i want to run this code on windows platform. will it work or do i have to do some modifications.pleas do reply

  10. sub
    Posted 2012/07/06 at 12:17 am | Permalink

    Hi,

    Is there anything extra I need to include in order to use Daimler’s people detector for the SVM instead ?

    • bryan
      Posted 2012/07/26 at 10:40 pm | Permalink

      Hi,
      I have not tested. I guess the line hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); should be replaced by something like hog.setSVMDetector(HOGDescriptor::getDaimlerPeopleDetector());

      Bryan

  11. Sarath
    Posted 2012/06/27 at 7:35 pm | Permalink

    Hi,

    Sir, i really appreciate your code.

    i have a simple query…. how are u able to detect ppl. do u supply the program with positive and negative images… ? i mean… say like 100 photos with people in and 100 photos without people in it? and then compare?

  12. Thusira
    Posted 2012/04/10 at 9:55 am | Permalink

    Hi Bryan,
    what are the alteration of the code if we are using a recorded (mp4) video file?

    thusira.

    • bryan
      Posted 2012/04/29 at 2:36 pm | Permalink

      Hi Thusira,
      In VideoCapture, you can use cap(“xxxx.mp4”) to capture from a digital video file. Thanks.

      Bryan

      • joshan
        Posted 2012/08/24 at 12:54 am | Permalink

        i copied the same code… cmd window s opening… suddenly it closed… no error s showing.. can yu tell me watz problem… wit tat….

        • bryan
          Posted 2012/08/25 at 11:00 am | Permalink

          Dear Joshan,
          Maybe you can try use a bare minimum code to verify if you can obtain the webcam capture image and use imshow to display it in the window first, in order to isolate which part is causing your error. The code in the post runs pretty well.

          Bryan

        • joshan
          Posted 2012/08/25 at 1:21 pm | Permalink

          tank u Mr.Bryan…i ve executed the code for loading webcam.. itz running perfectly… but but in case of deduction of human.. im getting “access violation writing location”…can yu help me wit dis error…

  13. Mike
    Posted 2011/08/26 at 11:29 pm | Permalink

    Did you install OpenCV from source tarball or did you use a pre-compiled package in DMG version for your Mac?
    The first improvement is the compiled code from tarballs, faster than the DMG one.
    Towards this fact, the usage of Windows or Linux is better than MacOS X, which is slower: on Windows, you could have a 90% faster code execution than XCode-built program and a 40-50% on Linux (Ubuntu is the best choice)

    • bryan
      Posted 2011/08/29 at 10:50 am | Permalink

      Hi Mike,
      For OSX, I compiled from source. I did try out in Windows 7 on the same Mac (bootcamp) and it has slight performance gain. BTW, I didn’t try out the GPU enhanced version yet. Thanks for the info.

      Bryan

2 Trackbacks