Ask Your Question

how to use CascadeClassifier::read?

asked 2016-01-27 05:44:36 -0500

Mark Nareznoy gravatar image


I want to use CascadeClassifier for face detection. I've managed to use it by using the function CascadeClassifier::load("haarcascade_frontalface_alt2.xml"). I want to use the same functinality, but without loading XML file from the disk. I assumed that the way to do it is converting the xml file to some kind of string (static const char* pCascade = "...".) and then use this string to initiate the CascadeClassifier using the function read(const FileNode& node).

My goal is to create an application which uses the CascadeClassifier, WITHOUT forcing the user to place the file "haarcascade_frontalface_alt2.xml" on his disk.

I searched the web and did not find any sample code using the CascadeClassifier read function.

Thanks, Mark

edit retag flag offensive close merge delete



Looking at the docs you can see that there is a note The file may contain a new cascade classifier (trained traincascade application) only.. So just to be sure, you aren't using any old models right?

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-27 08:43:16 -0500 )edit

Hi Steven,

I don't really know the difference between the old and the new models. My goal is to be able to use opencv cascade classifier to detect faces, without forcing the user to have a file on his disk.

I mean that I want to be able to have a code which is equal to: CascadeClassifier cascade; cascade.load("haarcascade_frontalface_alt2.xml");

The file "haarcascade_frontalface_alt2.xml" is provided with opencv. If the file "haarcascade_frontalface_alt2.xml" is considered an old model, then how can I get the new model of this file?

Thanks, Mark

Mark Nareznoy gravatar imageMark Nareznoy ( 2016-01-28 07:26:53 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2016-01-28 02:11:20 -0500

berak gravatar image

updated 2016-01-28 03:09:37 -0500

if you're using a cascade made from train_cascade (it won't work with the older ones from haar_training), you can do it easily like this:

String xml = "<xml> ....";
FileStorage fs( xml, FileStorage::MEMORY );

CascadeClassifier cascade; fs.getFirstTopLevelNode() );
edit flag offensive delete link more


Just a question on my side FileStorage::MEMORY means that it is stored in memory but it is thus not portable right? I guess he is looking more to somehow hardcode the model in?

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-28 03:22:22 -0500 )edit

^^ not portable ? why so ?

(also, the xml string could come from a database, or via network)

berak gravatar imageberak ( 2016-01-28 03:41:48 -0500 )edit

yeah but if it comes from a database, I do not really see the benefit compared to just packing the model inside the executable itself which is unpacked during installation ... :D

StevenPuttemans gravatar imageStevenPuttemans ( 2016-01-28 03:53:09 -0500 )edit

think of some weird webserver, that does not allow hard disk access (i.e. google appengine), or android, where the resources are in a zip file, and thus not accessible via a fopen()

berak gravatar imageberak ( 2016-01-28 04:42:13 -0500 )edit

Question Tools

1 follower


Asked: 2016-01-27 05:44:36 -0500

Seen: 956 times

Last updated: Jan 28 '16