Strange bug with haar finder and openCV 1.1pre

Just wanted to let people know incase they run into this.

Using the ofxCvHaarFinder in 006 (on a mac) I get this error when I try and load a haar file.

  
OpenCV ERROR: Unspecified error (The node does not represent a user object (unknown type?))  
	in function cvRead, /Users/theo/Documents/CODE/__OPENFRAMEWORKS/SANDBOX/COMPILE_LIBRARIES/__openCV/openCV1.1PreCompile/cxcore/src/cxpersistence.cpp(5061)  
Terminating the application...  

The place this is happening is at:

  
	cascade		= (CvHaarClassifierCascade*)cvLoad( haarFile.c_str(), 0, 0, 0 );  
  

In ofxCvHaarFinder::setup(string haarFile);

After digging around I found at the end of this page:
http://opencv.willowgarage.com/wiki/FaceDetection

A lot of people have been receiving an error while executing the following line:

cascade = (CvHaarClassifierCascade*)cvLoad(file_name,NULL, NULL, NULL);
The error looks like this: Unspecified error (The node does not represent a user object (unknown type?)) in function cvRead, C:\Program Files\OpenCV\cxcore\src\cxpersistence.cpp (5040)

The solution is to use cxcored.lib, cvd.lib and highguid.lib instead of cxcored_i7.lib, cv.lib and highgui.lib. You can use highgui.lib, but you get an error after the face detected image has been displayed and you’re unloading it.

Another simple workaround is to call any function from cv.lib before the call to cvLoad. For example: create a dummy empty image, apply cvErode to it and release the image.

But being on mac I didn’t have this option -
but then I found a bizarre solution:
http://thread.gmane.org/gmane.comp.lib.-…-ocus=17400

So I recreated the logic of it and it worked - no idea why. Here is the modified setup function with the hack to get openCV not to crash.

  
void ofxCvHaarFinder::setup(string haarFile) {  
   
	haarFile	= ofToDataPath(haarFile);  
    storage = cvCreateMemStorage(0);  
    cvClearMemStorage( storage );  
	myMoments	= (CvMoments*)malloc( sizeof(CvMoments) );  
  
	//total hack - loading a blank image to the haar finder to stop opencv crashing  
	ofxCvGrayscaleImage inImage;  
	ofImage tmp;   
	tmp.allocate(320, 240, OF_IMAGE_GRAYSCALE);  
	inImage.allocate(320, 240);  
	inImage = tmp.getPixels();  
  
	cascade		= (CvHaarClassifierCascade*)cvLoad( haarFile.c_str(), 0, 0, 0 );  
	//hack for opencv bellow  
	cvHaarDetectObjects( inImage.getCvImage(), cascade, storage, scaleHaar, 2, CV_HAAR_DO_CANNY_PRUNING);  
  
	if( !cascade ){  
	  printf("ERROR: Could not load classifier cascade\n" );  
	}   
blobs.clear();  
}  
  

Fixed cpp and h files are also attached.
Theo

ofxCvHaarFinder.zip

Here’s a newer version of ofxCvHaarFinder that’s pretty much completely rewritten.

I went through and removed the “cv voodoo” that wasn’t doing anything.

I got rid of draw(), because it didn’t quite fit in.

I removed and then added back in minWidth/maxWidth. They weren’t doing anything before, but now they are.

I put a simplified version of Theo’s hack inside an ifdef so we can get rid of it once someone recompiles 1.1.

image_w and image_h are now getWidth() and getHeight()

  
  
class ofxCvHaarFinder {  
public:  
	vector<ofxCvBlob> blobs;  
  
	ofxCvHaarFinder();  
	~ofxCvHaarFinder();  
	int findHaarObjects(ofxCvGrayscaleImage& input,  
		int minWidth = HAAR_DEFAULT_MIN_SIZE, int minHeight = HAAR_DEFAULT_MIN_SIZE);  
	int findHaarObjects(ofxCvGrayscaleImage& input, ofRectangle& roi,  
		int minWidth = HAAR_DEFAULT_MIN_SIZE, int minHeight = HAAR_DEFAULT_MIN_SIZE);  
	int findHaarObjects(ofxCvGrayscaleImage&, int x, int y, int w, int h,  
		int minWidth = HAAR_DEFAULT_MIN_SIZE, int minHeight = HAAR_DEFAULT_MIN_SIZE);  
  
	void setup(string haarFile);  
	bool ready();  
	void setScaleHaar(float scaleHaar);  
	void setNeighbors(unsigned neighbors);  
  
	float getWidth();  
	float getHeight();  
  
protected:  
	CvHaarClassifierCascade* cascade;  
	ofxCvGrayscaleImage img;  
	CvSeq*  haarResults[HAAR_MAX_RESULTS];  
	float scaleHaar;  
	unsigned neighbors;  
};  
  

Edit: The latest version and info is here: http://forum.openframeworks.cc/t/ofxcvhaarfinder:-opencv-faceeyemouth-tracking/2006/0

Hey Kyle, that sounds wicked, looking forward to giving it a shot. Do you think you can add it to http://addons.openframeworks.cc/ ? makes finding it later on (and the latest version) much easier.

I’d be glad too add it. I don’t want to step on anyone’s toes though – the comments say stefanix had something to do with it, but I’m not totally sure about the history. If no one says anything, I’ll just go ahead and add it.

I have run into another error that is past my current ability to solve. Everything seems to compile fine, then I get this before window is generated

“Xcode could not locate source file: cxerror.cpp (line: 359)”

Any ideas?

cxerror.cpp is what OpenCV uses for errors, I think. I’m not sure why it wouldn’t be found, or why it would be needed…

Try the more recent version posted here: http://forum.openframeworks.cc/t/ofxcvhaarfinder:-opencv-faceeyemouth-tracking/2006/0 and see if it’s still needed.

got the example to compile in Xcode on that particular thread… going to reexamine my program and see if I am missing something… thanks for the help, and especially for the example code, That helps more than anything for me right now.

Hi all,
I’m moving my cv application in my new macOSX.
I’m using kylemcdonald’s version of ofxCvHaarFinder at http://kyle.googlecode.com/.
I’ve got the error:

  
OpenCV ERROR: Null pointer (Invalid classifier cascade)  
	in function cvHaarDetectObjects, /Users/theo/Documents/CODE/__OPENFRAMEWORKS/SANDBOX/COMPILE_LIBRARIES/__openCV/openCV1.1PreCompile/cv/src/cvhaar.cpp(884)  
Terminating the application...  

In order to apply…

Another simple workaround is to call any function from cv.lib before the call to cvLoad. For example: create a dummy empty image, apply cvErode to it and release the image.

…found by Theo on http://opencv.willowgarage.com/wiki/FaceDetection I’ve removed previous hack code and changed in:

*1

  
  
....  
void ofxCvHaarFinder::setup(string haarFile) {  
	if(cascade != NULL)  
		cvReleaseHaarClassifierCascade(&cascade);  
  
	this->haarFile = haarFile;  
	  
	#ifdef HAAR_HACK  
	IplImage* hack = cvCreateImage(cvSize(320,240),IPL_DEPTH_8U,1);  
	#endif  
	  
	haarFile = ofToDataPath(haarFile);  
	cascade = (CvHaarClassifierCascade*) cvLoad(haarFile.c_str(), 0, 0, 0);  
  
	if (!cascade)  
		printf("Could not load Haar cascade: %s  
", haarFile.c_str());  
}  
...  
  

Function cvCreateImage before cvLoad is sufficent to execute without errors.

(1) for OF 0.061

After that I’ve found another error:

  
OF_WARNING: No camera settings to load  
OpenCV ERROR: Null pointer (Invalid classifier cascade)  
	in function cvHaarDetectObjects, /Users/theo/Documents/CODE/__OPENFRAMEWORKS/SANDBOX/COMPILE_LIBRARIES/__openCV/openCV1.1PreCompile/cv/src/cvhaar.cpp(884)  
Terminating the application...  

This is a well knowed problem in mac relative ofToDataPath function:
http://forum.openframeworks.cc/t/ofsetdatapathroot-mac-behaviour/4121/0
http://forum.openframeworks.cc/t/oftodatapath-in-061—no-abs-paths/3091/0
http://forum.openframeworks.cc/t/oftodatapath/786/3
ecc…

For now I’ve resolved adding “…/…/…/”:

*2

  
  
....  
void ofxCvHaarFinder::setup(string haarFile) {  
	if(cascade != NULL)  
		cvReleaseHaarClassifierCascade(&cascade);  
  
	this->haarFile = haarFile;  
	  
	#ifdef HAAR_HACK  
	IplImage* hack = cvCreateImage(cvSize(320,240),IPL_DEPTH_8U,1);  
	#endif  
	  
	haarFile = ofToDataPath(haarFile);  
  
	#ifdef TARGET_OSX  
	haarFile = "../../../" + haarFile;  
	#endif  
  
	cascade = (CvHaarClassifierCascade*) cvLoad(haarFile.c_str(), 0, 0, 0);  
  
	if (!cascade)  
		printf("Could not load Haar cascade: %s  
", haarFile.c_str());  
}  
...  
  

(1) end

(2) for OF 0.07

I’ve switched to new OF 0.007.
No need to make “…/…/…/” modify, so I returned to my previous code *1.

I’ve put in main only:

  
  
int main() {  
	ofSetDataPathRoot("../../../data/");  
	...  
}  
  

so everything works, also ofxXmlSettings founds my config file.

(2) end

Hope this can be util to someone…
bye!