Write sequence of int with FileStorage in Python
Hello,
I want to write a defaultConfig.xml file using the cv2.FileStorage Python bindings.
For now I am using another lib to do the defaultConfig.xml generation because of a limit of the python bindings to write a right <camera_resolution>
sequence of integers (defaultConfig.xml example).
I have a simple fix (bellow) and I would like to know if this is the best way to go.
The problem is with the <camera_resolution> integers sequence. I can't manage to write it using the actual python binding, as it is written as an opencv-matrix
(Mat
). Maybe the problem is that a Python binding must be create specifically for the sequence of integers, like it has be done for the sequence of Strings.
import cv2
fs = cv2.FileStorage('test.xml', cv2.FILE_STORAGE_WRITE)
fs.write("seq_int", [640, 480])
fs.release()
Produce the following problematic test.xml file:
<?xml version="1.0"?>
<opencv_storage>
<seq_int type_id="opencv-matrix">
<rows>2</rows>
<cols>1</cols>
<dt>d</dt>
<data>
640. 480.</data></seq_int>
</opencv_storage>
In the last pre-4.0 released there are bindings for sequence of strings but not for sequence of integers. Line 202 in /modules/core/src/persistence_cpp.cpp
void FileStorage::write( const String& name, const std::vector<String>& val )
{
*this << name << val;
}
I think I will propose as a PR (here is my actual fork) to add sequence of integers support:
void FileStorage::write( const String& name, const std::vector<int>& val )
{
*this << name << val;
}
With this add, running test like in test_persistence.py
import cv2
fs = cv2.FileStorage('test.xml', cv2.FILE_STORAGE_WRITE)
fs.write("seq_int", [640, 480])
fs.release()
And I get the following good test.xml file:
<?xml version="1.0"?>
<opencv_storage>
<seq_int>
640 480</seq_int>
</opencv_storage>
Do you thing it is the right way to go ?
I have read the PR discussion about vect strings support but could not find a definitive answer.
Thanks a lot if you take the time to read, and even more if you answer!