Tuesday, July 5, 2011

Detecting People: C and OpenCV

Continuation of previous post, this code was in the examples folder.


#include "cvaux.h"
#include "highgui.h"
#include "stdio.h"
#include "string.h"
#include "ctype.h"

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat img;
    FILE* f = 0;
    char _filename[1024];

    if( argc == 1 )
    {
        printf("Usage: peopledetect (<image_filename> | <image_list>.txt)\n");
        return 0;
    }
    img = imread(argv[1]);

    if( img.data )
    {
     strcpy(_filename, argv[1]);
    }
    else
    {
        f = fopen(argv[1], "rt");
        if(!f)
        {
      fprintf( stderr, "ERROR: the specified file could not be loaded\n");
      return -1;
     }
    }

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

    for(;;)
    {
     char* filename = _filename;
     if(f)
     {
      if(!fgets(filename, (int)sizeof(_filename)-2, f))
       break;
      //while(*filename && isspace(*filename))
      // ++filename;
      if(filename[0] == '#')
       continue;
      int l = strlen(filename);
      while(l > 0 && isspace(filename[l-1]))
       --l;
      filename[l] = '\0';
      img = imread(filename);
     }
     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).
        int can = img.channels();
     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++ )
     {
      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];
      // 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);
     }
     imshow("people detector", img);
     int c = waitKey(0) & 255;
     if( c == 'q' || c == 'Q' || !f)
            break;
    }
    if(f)
        fclose(f);
    return 0;
}

Reference:

Monday, July 4, 2011

Detecting People: Python & OpenCV

The latest SVN version of OpenCV contains an (undocumented) implementation of HOG-based pedestrian detection. It even comes with a pre-trained detector and a python wrapper. The basic usage is as follows:

from cv import *

storage = CreateMemStorage(0)
img = LoadImage(file)  # or read from camera

found = list(HOGDetectMultiScale(img, storage, win_stride=(8,8),
                padding=(32,32), scale=1.05, group_threshold=2))

An example from the examples folder:

import sys
from cv import *

def inside(r, q):
    (rx, ry), (rw, rh) = r
    (qx, qy), (qw, qh) = q
    return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh

try:
    img = LoadImage(sys.argv[1])
except:
    try:
        f = open(sys.argv[1], "rt")
    except:
        print "cannot read " + sys.argv[1]
        sys.exit(-1)
    imglist = list(f.readlines())
else:
    imglist = [sys.argv[1]]

NamedWindow("people detection demo", 1)
storage = CreateMemStorage(0)

for name in imglist:
    n = name.strip()
    print n
    try:
        img = LoadImage(n)
    except:
        continue
    
    #ClearMemStorage(storage)
    found = list(HOGDetectMultiScale(img, storage, win_stride=(8,8),
        padding=(32,32), scale=1.05, group_threshold=2))
    found_filtered = []
    for r in found:
        insidef = False
        for q in found:
            if inside(r, q):
                insidef = True
                break
        if not insidef:
            found_filtered.append(r)
    for r in found_filtered:
        (rx, ry), (rw, rh) = r
        tl = (rx + int(rw*0.1), ry + int(rh*0.07))
        br = (rx + int(rw*0.9), ry + int(rh*0.87))
        Rectangle(img, tl, br, (0, 255, 0), 3)
        
    ShowImage("people detection demo", img)
    c = WaitKey(0)
    if c == ord('q'):
        break

Reference:

Saturday, July 2, 2011