我试图将一个字符串加载到filestorage中。我不会让文件传递filename作为加载它的参数。相反,我将xml文档作为字符串。在文档http://docs.opencv.org/modules/core/doc/xml_yaml_persistence.html#filestorage中,提到fs.open的source属性可以是"文本字符串来读取"中的数据。我用OpenCv CascadeClassifier作为一个字符串运行一些简单的测试,但我得到一个空的FileStorage。我做错了什么?
CascadeClassifier face_cascade;
std::ifstream t("haarcascade_frontalface_alt.xml");
std::string ClasifierInString((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
cout << ClasifierInString << endl; //I CAN PRINT THE FILE AND SEE IT
cv::FileStorage fs;
if (!fs.open(ClasifierInString, cv::FileStorage::READ | cv::FileStorage::MEMORY | cv::FileStorage::FORMAT_XML))
{
cout << "Couldn't load file into memory" << endl;
return -2;
}
FileNodeIterator it = fs.getFirstTopLevelNode().begin(), it_end = fs.getFirstTopLevelNode().end();
for (; it != it_end; ++it)
{
cout << (string)*it << "\n"; //EMPTY LINE????
}
if (!face_cascade.read(fs.getFirstTopLevelNode()))
{
cout << "Couldn't read file from memory" << endl;
return -1;
}
编辑:
face_cascade.load("haarcascade_frontalface_alt.xml");
它有效。问题是我无法将其作为带有face_cascade.read的字符串读取(字符串)
答案 0 :(得分:1)
@berak评论是正确的答案。我使用了lbpcascade_frontalface.xml
,因为这是新的级联(并且更快!)Thx求助。
答案 1 :(得分:0)
我认为您的问题是文件名:"haarcascade_frontalface_alt.xml"
。 OpenCV flder中没有这样的文件...请尝试"haarcascade_frontalface_alt_tree.xml"
。
这是我的代码:
cv::CascadeClassifier face_cascade;
std::ifstream t("haarcascade_frontalface_alt_tree.xml");
std::string ClasifierInString((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
std::cout << ClasifierInString << std::endl; //I CAN PRINT THE FILE AND SEE IT
cv::FileStorage fs;
if (!fs.open(ClasifierInString, /*cv::FileStorage::READ | */cv::FileStorage::MEMORY | cv::FileStorage::FORMAT_XML))
{
std::cout << "Couldn't load file into memory" << std::endl;
return -2;
}
cv::FileNodeIterator it = fs.getFirstTopLevelNode().begin(), it_end = fs.getFirstTopLevelNode().end();
for (; it != it_end; ++it)
{
std::cout << (std::string)*it << "\n"; //EMPTY LINE????
}
if (!face_cascade.read(fs.getFirstTopLevelNode()))
{
std::cout << "Couldn't read file from memory" << std::endl;
return -1;
}