Ask Your Question
0

convert xml to string to use in Filstorage.read func

asked 2017-03-05 01:17:03 -0600

sabra gravatar image

updated 2017-03-05 01:19:03 -0600

I want to use below code:

FileStorage fs( "xml_as_text_string", FileStorage::READ | FileStorage::MEMORY);
 face_cascade.read(fs.root());

but I must first convert xml to string how ? I put " " for xml content and assign it to string but it is completely False.

opencv: 2.4.9 language:c++ sample xml file: haarcascade_frontalface_alt.xml

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-03-05 02:42:11 -0600

berak gravatar image

unfortunately, it's not that easy, as c++ does not have multiple line strings, and you'd have to escape all "

a common way to handle this, is to make a "binary array" out of the xml string, and #include in your c++ prog.

think of it like this:

const char xml[] = {
60, 63, 120, 109, 108, 32, 118, 101, 114, 115, 105, 111, 110, 61, 34, 49,
....
};

so, you'll have to write a little program, to convert the xml file to a c++ header:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char **argv) {
    string fn = "eye.xml";
    ifstream in(fn.c_str());
    string all,line;
    while (getline(in,line))
        all += line + "\n";

    ofstream out("eye.h");
    out << "const char xml[] = {";
    for (size_t i=0; i<all.length(); i++) {
        if (i%16==0) out << "\n";
        out << int(all[i]) << ", ";
    }
    out << "10};\n";
    return 0;
}

then, you can just use that in your prog:

#include "eye.h"
int main(int argc, const char * argv[])
{
    FileStorage fs(xml, FileStorage::READ | FileStorage::MEMORY);
    CascadeClassifier c;
    bool ok = c.read(fs.getFirstTopLevelNode()); // NOT root() !
    cerr << c.empty() << " " << ok << endl;
    return 0;
}
edit flag offensive delete link more

Comments

hi,tnx

i used convert xml to header in a binary array form but in main when read xml its empty(bool ok =0 and c.empty=1) ,why? we must convert binary array to string form or other things?

sabra gravatar imagesabra ( 2017-03-05 04:06:25 -0600 )edit

sorry, cannot test with 2.4

berak gravatar imageberak ( 2017-03-05 04:31:49 -0600 )edit

xml [] need convert to string before use in

 FileStorage fs(xml, FileStorage::READ | FileStorage::MEMORY);

??

sabra gravatar imagesabra ( 2017-03-05 04:49:18 -0600 )edit

char* to std::string (or even cv::String) is automatic, this should not be the problem

berak gravatar imageberak ( 2017-03-05 05:45:50 -0600 )edit

yes,

your right,

xml have xml text in string format in it, maybe problem is related to fs,I cant see the content of it ,what should i do to run code correctly? another system? another opencv version? tnx a lot to help me

sabra gravatar imagesabra ( 2017-03-05 06:02:11 -0600 )edit

Note:

The file may contain a new cascade classifier (trained traincascade application) only.

@berak whats the mean of this note? i found it under cascade::read in open cv site "haarcascade_frontalface_alt.xml" is old and cant be used in read function?

sabra gravatar imagesabra ( 2017-03-05 07:43:28 -0600 )edit

it means, that you can only use cascades produced by the traincascade tool, not old ones from haartraining. (so, no cascades from the gpucascades folder)

berak gravatar imageberak ( 2017-03-06 00:43:03 -0600 )edit

that's right, for another trained yml/xml,it works! Thank you so much.

sabra gravatar imagesabra ( 2017-03-06 02:51:43 -0600 )edit
1

@berak

hi again berak, my header size is 27 Mb for only 40 pics,what can i do about it? how can i optimize this solution?

((should i make a new issue?))

sabra gravatar imagesabra ( 2017-03-07 00:34:13 -0600 )edit

yea, true. the text representation of the filestorage already prints each number with 12 digit mantissa, and the "binarization" makes up to 5 letters from each.

latest opencv3 filestorage should have base64 encoding (that's only x3 larger), but i never tried this.

berak gravatar imageberak ( 2017-03-07 00:48:35 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-05 01:17:03 -0600

Seen: 1,210 times

Last updated: Mar 05 '17