Merge pull request #17436 from vpisarev:fix_python_io
* fixed #17044 1. fixed Python part of the tutorial about using OpenCV XML-YAML-JSON I/O functionality from C++ and Python. 2. added startWriteStruct() and endWriteStruct() methods to FileStorage 3. modifed FileStorage::write() methods to make them work well inside sequences, not only mappings. * try to fix the doc builder * added Python regression test for FileStorage I/O API ([TODO] iterating through long sequences can be very slow) * fixed yaml testing
This commit is contained in:
@@ -429,7 +429,7 @@ public:
|
||||
/** @brief Writes the registered C structure (CvMat, CvMatND, CvSeq).
|
||||
@param name Name of the written object.
|
||||
@param obj Pointer to the object.
|
||||
@see ocvWrite for details.
|
||||
@see cvWrite for details.
|
||||
*/
|
||||
void writeObj( const String& name, const void* obj );
|
||||
|
||||
@@ -456,6 +456,17 @@ public:
|
||||
*/
|
||||
CV_WRAP void writeComment(const String& comment, bool append = false);
|
||||
|
||||
/** @brief Starts to write a nested structure (sequence or a mapping).
|
||||
@param name name of the structure (if it's a member of parent mapping, otherwise it should be empty
|
||||
@param flags type of the structure (FileNode::MAP or FileNode::SEQ (both with optional FileNode::FLOW)).
|
||||
@param typeName usually an empty string
|
||||
*/
|
||||
CV_WRAP void startWriteStruct(const String& name, int flags, const String& typeName=String());
|
||||
|
||||
/** @brief Finishes writing nested structure (should pair startWriteStruct())
|
||||
*/
|
||||
CV_WRAP void endWriteStruct();
|
||||
|
||||
/** @brief Returns the normalized object name for the specified name of a file.
|
||||
@param filename Name of a file
|
||||
@returns The normalized object name.
|
||||
|
||||
@@ -166,22 +166,24 @@ void FileStorage::writeObj( const String& name, const void* obj )
|
||||
|
||||
void FileStorage::write( const String& name, int val )
|
||||
{
|
||||
*this << name << val;
|
||||
cvWriteInt(fs, name.c_str(), val);
|
||||
}
|
||||
|
||||
void FileStorage::write( const String& name, double val )
|
||||
{
|
||||
*this << name << val;
|
||||
cvWriteReal(fs, name.c_str(), val);
|
||||
}
|
||||
|
||||
void FileStorage::write( const String& name, const String& val )
|
||||
{
|
||||
*this << name << val;
|
||||
cvWriteString(fs, name.c_str(), val.c_str());
|
||||
}
|
||||
|
||||
void FileStorage::write( const String& name, InputArray val )
|
||||
{
|
||||
*this << name << val.getMat();
|
||||
if(state & INSIDE_MAP)
|
||||
*this << name;
|
||||
*this << val.getMat();
|
||||
}
|
||||
|
||||
void FileStorage::writeComment( const String& comment, bool append )
|
||||
@@ -189,6 +191,26 @@ void FileStorage::writeComment( const String& comment, bool append )
|
||||
cvWriteComment(fs, comment.c_str(), append ? 1 : 0);
|
||||
}
|
||||
|
||||
void FileStorage::startWriteStruct(const String& name, int flags, const String& typeName)
|
||||
{
|
||||
int struct_type = flags & FileNode::TYPE_MASK;
|
||||
bool isflow = (flags & FileNode::FLOW) != 0;
|
||||
CV_Assert(struct_type == FileNode::SEQ || struct_type == FileNode::MAP);
|
||||
char strbegin_[] = { (struct_type == FileNode::SEQ ? '[' : '{'), (isflow ? ':' : '\0'), '\0' };
|
||||
String strbegin = strbegin_;
|
||||
if (!typeName.empty())
|
||||
strbegin += typeName;
|
||||
*this << name << strbegin;
|
||||
}
|
||||
|
||||
void FileStorage::endWriteStruct()
|
||||
{
|
||||
if( structs.empty() )
|
||||
CV_Error( CV_StsError, "Extra endWriteStruct()" );
|
||||
char openparen = structs.back();
|
||||
*this << (openparen == '[' ? "]" : "}");
|
||||
}
|
||||
|
||||
String FileStorage::getDefaultObjectName(const String& _filename)
|
||||
{
|
||||
static const char* stubname = "unnamed";
|
||||
|
||||
@@ -853,7 +853,7 @@ void icvXMLWriteScalar( CvFileStorage* fs, const char* key, const char* data, in
|
||||
char* ptr = fs->buffer;
|
||||
int new_offset = (int)(ptr - fs->buffer_start) + len;
|
||||
|
||||
if( key )
|
||||
if( key && key[0] != '\0' )
|
||||
CV_Error( CV_StsBadArg, "elements with keys can not be written to sequence" );
|
||||
|
||||
fs->struct_flags = CV_NODE_SEQ;
|
||||
|
||||
Reference in New Issue
Block a user