Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

i can't tell about the deprecation status, with current 4.1 it seems to be still the same: Algorithm::load/save handles the FileStorage, and e.g. RTrees::read/write handle the data inside it.

given you have your xml data inside a std::string xml you can initialize a FileStorage from that memory, and read the tree data:

std::string xml; // "<?xml version="1.0"?>\n<opencv_storage>\n<opencv_ml_rtrees>\n ......"
FileStorage fs(xml, FileStorage::MEMORY|FileStorage::READ);

Ptr<ml::RTrees> another_tree = ml::RTrees::create();
another_tree->read(fs.getFirstTopLevelNode());
//cout << another_tree->isTrained() << " " << another_tree->getVarCount() << endl;

the next problem is, you have to get somehow creative, to get the xml content into something you can save as code, maybe you need to hexencode it:

char bytes = {
     0x36, 0x44,0x45,0x33,.....
};
string xml = string(bytes);

so, workflow would be:

  • train your model, save() to xml on disc.
  • write a program, that reads xml and writes c code
  • embed the generated c code into your classification program, and use the code above to load the model.

i can't tell about the deprecation status, with current 4.1 it seems to be still the same: Algorithm::load/save handles the FileStorage, and e.g. RTrees::read/write handle the data inside it.

given you have your xml data inside a std::string xml you can initialize a FileStorage from that memory, and read the tree data:

std::string xml; // "<?xml version="1.0"?>\n<opencv_storage>\n<opencv_ml_rtrees>\n ......"
FileStorage fs(xml, FileStorage::MEMORY|FileStorage::READ);

Ptr<ml::RTrees> another_tree = ml::RTrees::create();
another_tree->read(fs.getFirstTopLevelNode());
//cout << another_tree->isTrained() << " " << another_tree->getVarCount() << endl;

the next problem is, you have to get somehow creative, to get the xml content into something you can save as code, maybe you need to hexencode it:

char bytes bytes[] = {
     0x36, 0x44,0x45,0x33,.....
};
string xml = string(bytes);

so, workflow would be:

  • train your model, save() to xml on disc.
  • write a program, that reads xml and writes c code, like the char[] above
  • embed the generated c code into your classification program, and use the code above to load the model.

i can't tell about the deprecation status, with current 4.1 it seems to be still the same: Algorithm::load/save handles the FileStorage, and e.g. RTrees::read/write handle the data inside it.

given you have your xml data inside a std::string xml you can initialize a FileStorage from that memory, and read the tree data:

std::string xml; // "<?xml version="1.0"?>\n<opencv_storage>\n<opencv_ml_rtrees>\n ......"
FileStorage fs(xml, FileStorage::MEMORY|FileStorage::READ);

Ptr<ml::RTrees> another_tree = ml::RTrees::create();
another_tree->read(fs.getFirstTopLevelNode());
//cout << another_tree->isTrained() << " " << another_tree->getVarCount() << endl;

the next problem is, you have to get somehow creative, to get the xml content into something you can save (and compile !) as code, maybe you need to hexencode it:

char bytes[] = {
     0x36, 0x44,0x45,0x33,.....
};
string xml = string(bytes);

so, workflow would be:

  • train your model, save() to xml on disc.
  • write a program, that reads xml and writes c code, like the char[] above
  • embed the generated c code into your classification program, and use the code above to load the model.