I'm trying to solve a problem related with the OpenCV FileStorage. I can successfully write and read to/from yml/xml files. This only works when writing and reading functions are in the same program though.
When I create and write a yml file successfully in one program and tried to access that file from another program, I'm having the following problem.
Writing part:
d_img = imread(filename, 1);
FileStorage fs_source("tmp.yaml", FileStorage::WRITE);
if (fs_source.isOpened()){
fs_source << "source" << d_img;
fs_source.releaseAndGetString();
cout<<"Source : image written to temporary file"<<endl;
}
else
{
fs_source.open("tmp.yaml", FileStorage::WRITE);
fs_source << "source" << d_img;
fs_source.releaseAndGetString();
cout<<"Source : image written to temporary file 2"<<endl;
}
Reading part:
FileStorage fs_sink("tmp.yaml", FileStorage::READ);
if (fs_sink.isOpened()){
fs_sink["source"] >> d_img;
fs_sink.releaseAndGetString();
cout<<"Sink : image read from file 1"<<endl;
}
else
{
fs_sink.open("tmp.yaml", FileStorage::READ);
fs_sink["source"] >> d_img;
fs_sink.releaseAndGetString();
cout<<"Sink : image read from file 2"<<endl;
}
imwrite( "output.jpg", d_img );
First code creates the yml file without any problem.
But the second code throws the following error when it comes to the first line here which is FileStorage fs_sink("tmp.yaml", FileStorage::READ);
OpenCV Error: Parsing error (tmp.yaml(0): Valid XML should start with '<?xml ...?>') in icvXMLParse, file /var/tmp/portage/media-libs/opencv-2.4.7/work/opencv-2.4.7/modules/core/src/persistence.cpp, line 2257
thread[thread-per-block[1]: <block image_sink (2)>]: /var/tmp/portage/media-libs/opencv-2.4.7/work/opencv-2.4.7/modules/core/src/persistence.cpp:2257: error: (-212) tmp.yaml(0): Valid XML should start with '<?xml ...?>' in function icvXMLParse
This is odd because the file is yml, not an xml. It still gives the same error when I change the file type to xml.
What could cause this error?
PS: Both releaseAndGetString();
and release();
methods are tried to close the file. Same results.
xml
, yml
, and yaml
extensions are all tried too.
Edit: Can be replicated if file is empty.