ofxCvHaarFinder + videograbber

i’ve attempted looking through the addon examples/documentation to create a very basic face detection example with a video stream. I am not sure what I am doing incorrectly, but I see no bounding rectangle around the detected face from the webcam feed.

code below:

  
#include "testApp.h"  
  
//--------------------------------------------------------------  
void testApp::setup(){  
	  
	camWidth 		= 320;	// try to grab at this size.  
	camHeight 		= 240;  
    red.set(255,0,0);  
    //we can now get back a list of devices.  
	vector<ofVideoDevice> devices = vidGrabber.listDevices();  
	  
    for(int i = 0; i < devices.size(); i++){  
		cout << devices[i].id << ": " << devices[i].deviceName;  
        if( devices[i].bAvailable ){  
            cout << endl;  
        }else{  
            cout << " - unavailable " << endl;  
        }  
	}  
      
	vidGrabber.setDeviceID(0);  
	vidGrabber.setDesiredFrameRate(60);  
	vidGrabber.initGrabber(camWidth,camHeight);  
	  
	videoInverted 	= new unsigned char[camWidth*camHeight*3];  
	videoTexture.allocate(camWidth,camHeight, GL_RGB);  
	ofSetVerticalSync(true);  
      
      
	finder.setup("haarcascade_frontalface_default.xml");  
    finder.findHaarObjects(grayImage);  
}  
  
  
//--------------------------------------------------------------  
void testApp::update(){  
	  
	ofBackground(100,100,100);  
	  
    bool bNewFrame = false;  
      
    vidGrabber.update();  
    bNewFrame = vidGrabber.isFrameNew();  
      
      
      
	if (bNewFrame){  
          
        colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);  
        colorImg.setFromPixels(vidGrabber.getPixelsRef());  
        grayImage = colorImg; // convert our color image to a grayscale  
          
          
          
}  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
	ofSetHexColor(0xffffff);  
    grayImage.draw(360,20);  
      
    ofNoFill();  
	for(unsigned int i = 0; i < finder.blobs.size(); i++) {  
		ofRectangle cur = finder.blobs[i].boundingRect;  
		ofRect(cur.x, cur.y, cur.width, cur.height);  
	}  
      
      
}  

I’m considering your program looks like it’s building and running with no errors, right?

Did you make sure you have the “haarcascade_frontalface_default.xml” file in your bin/data folder?

ah, well this was an issue. yes it compiles and I had copied over the .xml file to the data folder, but when compiled, I am faced with this:

![](http://forum.openframeworks.cc/uploads/default/3109/Screen Shot 2013-11-24 at 10.24.39 PM.png)

The reason it’s not detecting is because you have this in the setup:

finder.findHaarObjects(grayImage);

Move that line to your update and it will work. Also, not sure why you are “setFromPixels” twice…

Here is a simplified version that will work:

//--------------------------------------------------------------
void ofApp::setup(){

vidGrabber.setVerbose(true);
vidGrabber.initGrabber(320,240);
colorImg.allocate(320,240);
grayImage.allocate(320, 240);

finder.setup("haarcascade_frontalface_default.xml");
}

//--------------------------------------------------------------
void ofApp::update(){

vidGrabber.update();

if (vidGrabber.isFrameNew())
{
    colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
    grayImage = colorImg;
    
    finder.findHaarObjects(grayImage);
}

}

//--------------------------------------------------------------
void ofApp::draw(){

ofSetHexColor(0xffffff);
colorImg.draw(0,0);

ofNoFill();
for(unsigned int i = 0; i < finder.blobs.size(); i++)
{
	ofRectangle cur = finder.blobs[i].boundingRect;
	ofRect(cur.x, cur.y, cur.width, cur.height);
}
}

Hi jasonrwalters I copy&paste your code, but it gets blobs.size() equals 0. I did exactly that, didn’t work though. It just displays the feeding from the webcam. Any ideas?

I added by myself background substraction and thresholding to a gray image previous to find faces, it didn’t work either.
oF 0.8.4
Ubuntu 12.04 64 bits

Just in case another thread Iooked into Find a face using Haar and copy it somewhere else

Greets!

Solved, affter digging down in tha addon’s code I realized I had to decrease the values passed to findHaarObjects
finder.findHaarObjects(img,10,10);
Maybe the streaming feed is more sensitive and to locate object these parameters are needed.