mirror of
https://github.com/opencv/opencv.git
synced 2025-01-18 22:44:02 +08:00
Merge pull request #6949 from wiryls:FileStorageBase64DocsTests
This commit is contained in:
commit
5eee757658
@ -1976,8 +1976,16 @@ CVAPI(void) cvSetIPLAllocators( Cv_iplCreateImageHeader create_header,
|
||||
|
||||
The function opens file storage for reading or writing data. In the latter case, a new file is
|
||||
created or an existing file is rewritten. The type of the read or written file is determined by the
|
||||
filename extension: .xml for XML and .yml or .yaml for YAML. The function returns a pointer to the
|
||||
CvFileStorage structure. If the file cannot be opened then the function returns NULL.
|
||||
filename extension: .xml for XML and .yml or .yaml for YAML.
|
||||
|
||||
At the same time, it also supports adding parameters like "example.xml?base64". The three ways
|
||||
are the same:
|
||||
@snippet samples/cpp/filestorage_base64.cpp suffix_in_file_name
|
||||
@snippet samples/cpp/filestorage_base64.cpp flag_write_base64
|
||||
@snippet samples/cpp/filestorage_base64.cpp flag_write_and_flag_base64
|
||||
|
||||
The function returns a pointer to the CvFileStorage structure.
|
||||
If the file cannot be opened then the function returns NULL.
|
||||
@param filename Name of the file associated with the storage
|
||||
@param memstorage Memory storage used for temporary data and for
|
||||
: storing dynamic structures, such as CvSeq or CvGraph . If it is NULL, a temporary memory
|
||||
@ -1985,6 +1993,7 @@ CvFileStorage structure. If the file cannot be opened then the function returns
|
||||
@param flags Can be one of the following:
|
||||
> - **CV_STORAGE_READ** the storage is open for reading
|
||||
> - **CV_STORAGE_WRITE** the storage is open for writing
|
||||
(use **CV_STORAGE_WRITE | CV_STORAGE_WRITE_BASE64** to write rawdata in Base64)
|
||||
@param encoding
|
||||
*/
|
||||
CVAPI(CvFileStorage*) cvOpenFileStorage( const char* filename, CvMemStorage* memstorage,
|
||||
@ -2162,7 +2171,7 @@ the file with multiple streams looks like this:
|
||||
@endcode
|
||||
The YAML file will look like this:
|
||||
@code{.yaml}
|
||||
%YAML:1.0
|
||||
%YAML 1.0
|
||||
# stream #1 data
|
||||
...
|
||||
---
|
||||
@ -2187,6 +2196,28 @@ to a sequence rather than a map.
|
||||
CVAPI(void) cvWriteRawData( CvFileStorage* fs, const void* src,
|
||||
int len, const char* dt );
|
||||
|
||||
/** @brief Writes multiple numbers in Base64.
|
||||
|
||||
If either CV_STORAGE_WRITE_BASE64 or cv::FileStorage::WRITE_BASE64 is used,
|
||||
this function will be the same as cvWriteRawData. If neither, the main
|
||||
difference is that it outputs a sequence in Base64 encoding rather than
|
||||
in plain text.
|
||||
|
||||
This function can only be used to write a sequence with a type "binary".
|
||||
|
||||
Consider the following two examples where their output is the same:
|
||||
@snippet samples/cpp/filestorage_base64.cpp without_base64_flag
|
||||
and
|
||||
@snippet samples/cpp/filestorage_base64.cpp with_write_base64_flag
|
||||
|
||||
@param fs File storage
|
||||
@param src Pointer to the written array
|
||||
@param len Number of the array elements to write
|
||||
@param dt Specification of each array element, see @ref format_spec "format specification"
|
||||
*/
|
||||
CVAPI(void) cvWriteRawDataBase64( CvFileStorage* fs, const void* src,
|
||||
int len, const char* dt );
|
||||
|
||||
/** @brief Returns a unique pointer for a given name.
|
||||
|
||||
The function returns a unique pointer for each particular file node name. This pointer can be then
|
||||
|
@ -311,7 +311,10 @@ public:
|
||||
FORMAT_MASK = (7<<3), //!< mask for format flags
|
||||
FORMAT_AUTO = 0, //!< flag, auto format
|
||||
FORMAT_XML = (1<<3), //!< flag, XML format
|
||||
FORMAT_YAML = (2<<3) //!< flag, YAML format
|
||||
FORMAT_YAML = (2<<3), //!< flag, YAML format
|
||||
|
||||
BASE64 = 64, //!< flag, write rawdata in Base64 by default. (consider using WRITE_BASE64)
|
||||
WRITE_BASE64 = BASE64 | WRITE, //!< flag, enable both WRITE and BASE64
|
||||
};
|
||||
enum
|
||||
{
|
||||
@ -354,7 +357,9 @@ public:
|
||||
Extension of the file (.xml or .yml/.yaml) determines its format (XML or YAML respectively).
|
||||
Also you can append .gz to work with compressed files, for example myHugeMatrix.xml.gz. If both
|
||||
FileStorage::WRITE and FileStorage::MEMORY flags are specified, source is used just to specify
|
||||
the output file format (e.g. mydata.xml, .yml etc.).
|
||||
the output file format (e.g. mydata.xml, .yml etc.). A file name can also contain parameters.
|
||||
You can use this format, "*?base64" (e.g. "file.xml?base64"), as an alternative to
|
||||
FileStorage::BASE64 flag. Note: it is case sensitive.
|
||||
@param flags Mode of operation. One of FileStorage::Mode
|
||||
@param encoding Encoding of the file. Note that UTF-16 XML encoding is not supported currently and
|
||||
you should use 8-bit encoding instead of it.
|
||||
|
@ -1669,6 +1669,8 @@ typedef struct CvFileStorage CvFileStorage;
|
||||
#define CV_STORAGE_FORMAT_AUTO 0
|
||||
#define CV_STORAGE_FORMAT_XML 8
|
||||
#define CV_STORAGE_FORMAT_YAML 16
|
||||
#define CV_STORAGE_BASE64 64
|
||||
#define CV_STORAGE_WRITE_BASE64 (CV_STORAGE_BASE64 | CV_STORAGE_WRITE)
|
||||
|
||||
/** @brief List of attributes. :
|
||||
|
||||
|
86
modules/core/perf/perf_io_base64.cpp
Normal file
86
modules/core/perf/perf_io_base64.cpp
Normal file
@ -0,0 +1,86 @@
|
||||
#include "perf_precomp.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
using namespace perf;
|
||||
using std::tr1::make_tuple;
|
||||
using std::tr1::get;
|
||||
|
||||
typedef std::tr1::tuple<cv::Size, MatType, String> Size_MatType_Str_t;
|
||||
typedef TestBaseWithParam<Size_MatType_Str_t> Size_Mat_StrType;
|
||||
|
||||
#define MAT_SIZES ::perf::sz1080p/*, ::perf::sz4320p*/
|
||||
#define MAT_TYPES CV_8UC1, CV_32FC1
|
||||
#define FILE_EXTENSION String(".xml"), String(".yml")
|
||||
|
||||
|
||||
PERF_TEST_P(Size_Mat_StrType, fs_text,
|
||||
testing::Combine(testing::Values(MAT_SIZES),
|
||||
testing::Values(MAT_TYPES),
|
||||
testing::Values(FILE_EXTENSION))
|
||||
)
|
||||
{
|
||||
Size size = get<0>(GetParam());
|
||||
int type = get<1>(GetParam());
|
||||
String ext = get<2>(GetParam());
|
||||
|
||||
Mat src(size.height, size.width, type);
|
||||
Mat dst = src.clone();
|
||||
|
||||
declare.in(src, WARMUP_RNG).out(dst);
|
||||
|
||||
cv::String file_name = cv::tempfile(ext.c_str());
|
||||
cv::String key = "test_mat";
|
||||
|
||||
TEST_CYCLE_MULTIRUN(2)
|
||||
{
|
||||
{
|
||||
FileStorage fs(file_name, cv::FileStorage::WRITE);
|
||||
fs << key << src;
|
||||
fs.release();
|
||||
}
|
||||
{
|
||||
FileStorage fs(file_name, cv::FileStorage::READ);
|
||||
fs[key] >> dst;
|
||||
fs.release();
|
||||
}
|
||||
}
|
||||
|
||||
remove(file_name.c_str());
|
||||
SANITY_CHECK_NOTHING();
|
||||
}
|
||||
|
||||
PERF_TEST_P(Size_Mat_StrType, fs_base64,
|
||||
testing::Combine(testing::Values(MAT_SIZES),
|
||||
testing::Values(MAT_TYPES),
|
||||
testing::Values(FILE_EXTENSION))
|
||||
)
|
||||
{
|
||||
Size size = get<0>(GetParam());
|
||||
int type = get<1>(GetParam());
|
||||
String ext = get<2>(GetParam());
|
||||
|
||||
Mat src(size.height, size.width, type);
|
||||
Mat dst = src.clone();
|
||||
|
||||
cv::String file_name = cv::tempfile(ext.c_str());
|
||||
cv::String key = "test_mat";
|
||||
|
||||
declare.in(src, WARMUP_RNG).out(dst);
|
||||
TEST_CYCLE_MULTIRUN(2)
|
||||
{
|
||||
{
|
||||
FileStorage fs(file_name, cv::FileStorage::WRITE_BASE64);
|
||||
fs << key << src;
|
||||
fs.release();
|
||||
}
|
||||
{
|
||||
FileStorage fs(file_name, cv::FileStorage::READ);
|
||||
fs[key] >> dst;
|
||||
fs.release();
|
||||
}
|
||||
}
|
||||
|
||||
remove(file_name.c_str());
|
||||
SANITY_CHECK_NOTHING();
|
||||
}
|
File diff suppressed because it is too large
Load Diff
@ -594,150 +594,263 @@ TEST(Core_InputOutput, FileStorageSpaces)
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Core_InputOutput, filestorage_yml_compatibility)
|
||||
struct data_t
|
||||
{
|
||||
// TODO:
|
||||
}
|
||||
typedef uchar u;
|
||||
typedef char b;
|
||||
typedef ushort w;
|
||||
typedef short s;
|
||||
typedef int i;
|
||||
typedef float f;
|
||||
typedef double d;
|
||||
|
||||
class CV_Base64IOTest : public cvtest::BaseTest
|
||||
{
|
||||
private:
|
||||
std::string file_name;
|
||||
u u1 ;u u2 ; i i1 ;
|
||||
i i2 ;i i3 ;
|
||||
d d1 ;
|
||||
d d2 ;
|
||||
i i4 ;
|
||||
|
||||
struct data_t
|
||||
{
|
||||
uchar u1, u2;
|
||||
int i1, i2, i3;
|
||||
double d1, d2;
|
||||
int i4;
|
||||
};
|
||||
|
||||
public:
|
||||
CV_Base64IOTest(std::string const & test_file_name)
|
||||
: file_name(test_file_name) {}
|
||||
~CV_Base64IOTest() {}
|
||||
protected:
|
||||
void run(int)
|
||||
{
|
||||
try
|
||||
{
|
||||
std::vector<data_t> rawdata;
|
||||
|
||||
cv::Mat _em_out, _em_in;
|
||||
cv::Mat _2d_out, _2d_in;
|
||||
cv::Mat _nd_out, _nd_in;
|
||||
|
||||
{ /* init */
|
||||
|
||||
/* normal mat */
|
||||
_2d_out = cv::Mat(100, 100, CV_8UC3, cvScalar(1U, 2U, 127U));
|
||||
for (int i = 0; i < _2d_out.rows; ++i)
|
||||
for (int j = 0; j < _2d_out.cols; ++j)
|
||||
_2d_out.at<cv::Vec3b>(i, j)[1] = (i + j) % 256;
|
||||
|
||||
/* 4d mat */
|
||||
const int Size[] = {4, 4, 4, 4};
|
||||
cv::Mat _4d(4, Size, CV_64FC4, cvScalar(0.888, 0.111, 0.666, 0.444));
|
||||
const cv::Range ranges[] = {
|
||||
cv::Range(0, 2),
|
||||
cv::Range(0, 2),
|
||||
cv::Range(1, 2),
|
||||
cv::Range(0, 2) };
|
||||
_nd_out = _4d(ranges);
|
||||
|
||||
/* raw data */
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
data_t tmp;
|
||||
tmp.u1 = 1;
|
||||
tmp.u2 = 2;
|
||||
tmp.i1 = 1;
|
||||
tmp.i2 = 2;
|
||||
tmp.i3 = 3;
|
||||
tmp.d1 = 0.1;
|
||||
tmp.d2 = 0.2;
|
||||
tmp.i4 = i;
|
||||
rawdata.push_back(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
{ /* write */
|
||||
cv::FileStorage fs(file_name, cv::FileStorage::WRITE);
|
||||
CvMat holder = _2d_out;
|
||||
cv::cvWriteMat_Base64(*fs, "normal_2d_mat", &holder);
|
||||
CvMatND holder_nd = _nd_out;
|
||||
cv::cvWriteMatND_Base64(*fs, "normal_nd_mat", &holder_nd);
|
||||
holder = _em_out;
|
||||
cv::cvWriteMat_Base64(*fs, "empty_2d_mat", &holder);
|
||||
|
||||
cv::cvStartWriteRawData_Base64(*fs, "rawdata", static_cast<int>(rawdata.size()), "2u3i2di");
|
||||
for (int i = 0; i < 10; i++)
|
||||
cv::cvWriteRawData_Base64(*fs, rawdata.data() + i * 100, 100);
|
||||
cv::cvEndWriteRawData_Base64(*fs);
|
||||
|
||||
fs.release();
|
||||
}
|
||||
|
||||
{ /* read */
|
||||
cv::FileStorage fs(file_name, cv::FileStorage::READ);
|
||||
|
||||
/* mat */
|
||||
fs["empty_2d_mat"] >> _em_in;
|
||||
fs["normal_2d_mat"] >> _2d_in;
|
||||
fs["normal_nd_mat"] >> _nd_in;
|
||||
|
||||
/* raw data */
|
||||
std::vector<data_t>(1000).swap(rawdata);
|
||||
cvReadRawData(*fs, fs["rawdata"].node, rawdata.data(), "2u3i2di");
|
||||
|
||||
fs.release();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
// TODO: Solve this bug in `cvReadRawData`
|
||||
//EXPECT_EQ(rawdata[i].u1, 1);
|
||||
//EXPECT_EQ(rawdata[i].u2, 2);
|
||||
//EXPECT_EQ(rawdata[i].i1, 1);
|
||||
//EXPECT_EQ(rawdata[i].i2, 2);
|
||||
//EXPECT_EQ(rawdata[i].i3, 3);
|
||||
//EXPECT_EQ(rawdata[i].d1, 0.1);
|
||||
//EXPECT_EQ(rawdata[i].d2, 0.2);
|
||||
//EXPECT_EQ(rawdata[i].i4, i);
|
||||
}
|
||||
|
||||
EXPECT_EQ(_em_in.rows , _em_out.rows);
|
||||
EXPECT_EQ(_em_in.cols , _em_out.cols);
|
||||
EXPECT_EQ(_em_in.depth(), _em_out.depth());
|
||||
EXPECT_TRUE(_em_in.empty());
|
||||
|
||||
EXPECT_EQ(_2d_in.rows , _2d_in.rows);
|
||||
EXPECT_EQ(_2d_in.cols , _2d_in.cols);
|
||||
EXPECT_EQ(_2d_in.dims , _2d_in.dims);
|
||||
EXPECT_EQ(_2d_in.depth(), _2d_in.depth());
|
||||
for(int i = 0; i < _2d_in.rows; ++i)
|
||||
for (int j = 0; j < _2d_in.cols; ++j)
|
||||
EXPECT_EQ(_2d_in.at<cv::Vec3b>(i, j), _2d_out.at<cv::Vec3b>(i, j));
|
||||
|
||||
EXPECT_EQ(_nd_in.rows , _nd_in.rows);
|
||||
EXPECT_EQ(_nd_in.cols , _nd_in.cols);
|
||||
EXPECT_EQ(_nd_in.dims , _nd_in.dims);
|
||||
EXPECT_EQ(_nd_in.depth(), _nd_in.depth());
|
||||
EXPECT_EQ(cv::countNonZero(cv::mean(_nd_in != _nd_out)), 0);
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
ts->set_failed_test_info(cvtest::TS::FAIL_MISMATCH);
|
||||
}
|
||||
}
|
||||
static inline const char * signature() { return "2u3i2di"; }
|
||||
};
|
||||
|
||||
TEST(Core_InputOutput, filestorage_yml_base64)
|
||||
TEST(Core_InputOutput, filestorage_base64_basic)
|
||||
{
|
||||
CV_Base64IOTest test("base64_test_tmp_file.yml"); test.safe_run();
|
||||
char const * filenames[] = {
|
||||
"core_io_base64_basic_test.yml",
|
||||
"core_io_base64_basic_test.xml",
|
||||
0
|
||||
};
|
||||
|
||||
for (char const ** ptr = filenames; *ptr; ptr++)
|
||||
{
|
||||
char const * name = *ptr;
|
||||
|
||||
std::vector<data_t> rawdata;
|
||||
|
||||
cv::Mat _em_out, _em_in;
|
||||
cv::Mat _2d_out, _2d_in;
|
||||
cv::Mat _nd_out, _nd_in;
|
||||
cv::Mat _rd_out(64, 64, CV_64FC1), _rd_in;
|
||||
|
||||
{ /* init */
|
||||
|
||||
/* a normal mat */
|
||||
_2d_out = cv::Mat(100, 100, CV_8UC3, cvScalar(1U, 2U, 127U));
|
||||
for (int i = 0; i < _2d_out.rows; ++i)
|
||||
for (int j = 0; j < _2d_out.cols; ++j)
|
||||
_2d_out.at<cv::Vec3b>(i, j)[1] = (i + j) % 256;
|
||||
|
||||
/* a 4d mat */
|
||||
const int Size[] = {4, 4, 4, 4};
|
||||
cv::Mat _4d(4, Size, CV_64FC4, cvScalar(0.888, 0.111, 0.666, 0.444));
|
||||
const cv::Range ranges[] = {
|
||||
cv::Range(0, 2),
|
||||
cv::Range(0, 2),
|
||||
cv::Range(1, 2),
|
||||
cv::Range(0, 2) };
|
||||
_nd_out = _4d(ranges);
|
||||
|
||||
/* a random mat */
|
||||
cv::randu(_rd_out, cv::Scalar(0.0), cv::Scalar(1.0));
|
||||
|
||||
/* raw data */
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
data_t tmp;
|
||||
tmp.u1 = 1;
|
||||
tmp.u2 = 2;
|
||||
tmp.i1 = 1;
|
||||
tmp.i2 = 2;
|
||||
tmp.i3 = 3;
|
||||
tmp.d1 = 0.1;
|
||||
tmp.d2 = 0.2;
|
||||
tmp.i4 = i;
|
||||
rawdata.push_back(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
{ /* write */
|
||||
cv::FileStorage fs(name, cv::FileStorage::WRITE_BASE64);
|
||||
fs << "normal_2d_mat" << _2d_out;
|
||||
fs << "normal_nd_mat" << _nd_out;
|
||||
fs << "empty_2d_mat" << _em_out;
|
||||
fs << "random_mat" << _rd_out;
|
||||
|
||||
cvStartWriteStruct( *fs, "rawdata", CV_NODE_SEQ | CV_NODE_FLOW, "binary" );
|
||||
for (int i = 0; i < 10; i++)
|
||||
cvWriteRawDataBase64(*fs, rawdata.data() + i * 100, 100, data_t::signature());
|
||||
cvEndWriteStruct( *fs );
|
||||
|
||||
fs.release();
|
||||
}
|
||||
|
||||
{ /* read */
|
||||
cv::FileStorage fs(name, cv::FileStorage::READ);
|
||||
|
||||
/* mat */
|
||||
fs["empty_2d_mat"] >> _em_in;
|
||||
fs["normal_2d_mat"] >> _2d_in;
|
||||
fs["normal_nd_mat"] >> _nd_in;
|
||||
fs["random_mat"] >> _rd_in;
|
||||
|
||||
/* raw data */
|
||||
std::vector<data_t>(1000).swap(rawdata);
|
||||
cvReadRawData(*fs, fs["rawdata"].node, rawdata.data(), data_t::signature());
|
||||
|
||||
fs.release();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
// TODO: Solve this bug in `cvReadRawData`
|
||||
//EXPECT_EQ(rawdata[i].u1, 1);
|
||||
//EXPECT_EQ(rawdata[i].u2, 2);
|
||||
//EXPECT_EQ(rawdata[i].i1, 1);
|
||||
//EXPECT_EQ(rawdata[i].i2, 2);
|
||||
//EXPECT_EQ(rawdata[i].i3, 3);
|
||||
//EXPECT_EQ(rawdata[i].d1, 0.1);
|
||||
//EXPECT_EQ(rawdata[i].d2, 0.2);
|
||||
//EXPECT_EQ(rawdata[i].i4, i);
|
||||
}
|
||||
|
||||
EXPECT_EQ(_em_in.rows , _em_out.rows);
|
||||
EXPECT_EQ(_em_in.cols , _em_out.cols);
|
||||
EXPECT_EQ(_em_in.depth(), _em_out.depth());
|
||||
EXPECT_TRUE(_em_in.empty());
|
||||
|
||||
EXPECT_EQ(_2d_in.rows , _2d_out.rows);
|
||||
EXPECT_EQ(_2d_in.cols , _2d_out.cols);
|
||||
EXPECT_EQ(_2d_in.dims , _2d_out.dims);
|
||||
EXPECT_EQ(_2d_in.depth(), _2d_out.depth());
|
||||
for(int i = 0; i < _2d_out.rows; ++i)
|
||||
for (int j = 0; j < _2d_out.cols; ++j)
|
||||
EXPECT_EQ(_2d_in.at<cv::Vec3b>(i, j), _2d_out.at<cv::Vec3b>(i, j));
|
||||
|
||||
EXPECT_EQ(_nd_in.rows , _nd_out.rows);
|
||||
EXPECT_EQ(_nd_in.cols , _nd_out.cols);
|
||||
EXPECT_EQ(_nd_in.dims , _nd_out.dims);
|
||||
EXPECT_EQ(_nd_in.depth(), _nd_out.depth());
|
||||
EXPECT_EQ(cv::countNonZero(cv::mean(_nd_in != _nd_out)), 0);
|
||||
|
||||
EXPECT_EQ(_rd_in.rows , _rd_out.rows);
|
||||
EXPECT_EQ(_rd_in.cols , _rd_out.cols);
|
||||
EXPECT_EQ(_rd_in.dims , _rd_out.dims);
|
||||
EXPECT_EQ(_rd_in.depth(), _rd_out.depth());
|
||||
EXPECT_EQ(cv::countNonZero(cv::mean(_rd_in != _rd_out)), 0);
|
||||
|
||||
remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Core_InputOutput, filestorage_xml_base64)
|
||||
TEST(Core_InputOutput, filestorage_base64_valid_call)
|
||||
{
|
||||
CV_Base64IOTest test("base64_test_tmp_file.xml"); test.safe_run();
|
||||
char const * filenames[] = {
|
||||
"core_io_base64_other_test.yml",
|
||||
"core_io_base64_other_test.xml",
|
||||
"core_io_base64_other_test.yml?base64",
|
||||
"core_io_base64_other_test.xml?base64",
|
||||
0
|
||||
};
|
||||
char const * real_name[] = {
|
||||
"core_io_base64_other_test.yml",
|
||||
"core_io_base64_other_test.xml",
|
||||
"core_io_base64_other_test.yml",
|
||||
"core_io_base64_other_test.xml",
|
||||
0
|
||||
};
|
||||
|
||||
std::vector<int> rawdata(10, static_cast<int>(0x00010203));
|
||||
cv::String str_out = "test_string";
|
||||
|
||||
for (char const ** ptr = filenames; *ptr; ptr++)
|
||||
{
|
||||
char const * name = *ptr;
|
||||
|
||||
EXPECT_NO_THROW(
|
||||
{
|
||||
cv::FileStorage fs(name, cv::FileStorage::WRITE_BASE64);
|
||||
|
||||
cvStartWriteStruct(*fs, "manydata", CV_NODE_SEQ);
|
||||
cvStartWriteStruct(*fs, 0, CV_NODE_SEQ | CV_NODE_FLOW);
|
||||
for (int i = 0; i < 10; i++)
|
||||
cvWriteRawData(*fs, rawdata.data(), static_cast<int>(rawdata.size()), "i");
|
||||
cvEndWriteStruct(*fs);
|
||||
cvWriteString(*fs, 0, str_out.c_str(), 1);
|
||||
cvEndWriteStruct(*fs);
|
||||
|
||||
fs.release();
|
||||
});
|
||||
|
||||
{
|
||||
cv::FileStorage fs(name, cv::FileStorage::READ);
|
||||
std::vector<int> data_in(rawdata.size());
|
||||
fs["manydata"][0].readRaw("i", (uchar *)data_in.data(), data_in.size());
|
||||
EXPECT_TRUE(fs["manydata"][0].isSeq());
|
||||
EXPECT_TRUE(std::equal(rawdata.begin(), rawdata.end(), data_in.begin()));
|
||||
cv::String str_in;
|
||||
fs["manydata"][1] >> str_in;
|
||||
EXPECT_TRUE(fs["manydata"][1].isString());
|
||||
EXPECT_EQ(str_in, str_out);
|
||||
fs.release();
|
||||
}
|
||||
|
||||
EXPECT_NO_THROW(
|
||||
{
|
||||
cv::FileStorage fs(name, cv::FileStorage::WRITE);
|
||||
|
||||
cvStartWriteStruct(*fs, "manydata", CV_NODE_SEQ);
|
||||
cvWriteString(*fs, 0, str_out.c_str(), 1);
|
||||
cvStartWriteStruct(*fs, 0, CV_NODE_SEQ | CV_NODE_FLOW, "binary");
|
||||
for (int i = 0; i < 10; i++)
|
||||
cvWriteRawData(*fs, rawdata.data(), static_cast<int>(rawdata.size()), "i");
|
||||
cvEndWriteStruct(*fs);
|
||||
cvEndWriteStruct(*fs);
|
||||
|
||||
fs.release();
|
||||
});
|
||||
|
||||
{
|
||||
cv::FileStorage fs(name, cv::FileStorage::READ);
|
||||
cv::String str_in;
|
||||
fs["manydata"][0] >> str_in;
|
||||
EXPECT_TRUE(fs["manydata"][0].isString());
|
||||
EXPECT_EQ(str_in, str_out);
|
||||
std::vector<int> data_in(rawdata.size());
|
||||
fs["manydata"][1].readRaw("i", (uchar *)data_in.data(), data_in.size());
|
||||
EXPECT_TRUE(fs["manydata"][1].isSeq());
|
||||
EXPECT_TRUE(std::equal(rawdata.begin(), rawdata.end(), data_in.begin()));
|
||||
fs.release();
|
||||
}
|
||||
|
||||
remove(real_name[ptr - filenames]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Core_InputOutput, filestorage_base64_invalid_call)
|
||||
{
|
||||
char const * filenames[] = {
|
||||
"core_io_base64_other_test.yml",
|
||||
"core_io_base64_other_test.xml",
|
||||
0
|
||||
};
|
||||
|
||||
for (char const ** ptr = filenames; *ptr; ptr++)
|
||||
{
|
||||
char const * name = *ptr;
|
||||
|
||||
EXPECT_ANY_THROW({
|
||||
cv::FileStorage fs(name, cv::FileStorage::WRITE);
|
||||
cvStartWriteStruct(*fs, "rawdata", CV_NODE_SEQ, "binary");
|
||||
cvStartWriteStruct(*fs, 0, CV_NODE_SEQ | CV_NODE_FLOW);
|
||||
});
|
||||
|
||||
EXPECT_ANY_THROW({
|
||||
cv::FileStorage fs(name, cv::FileStorage::WRITE);
|
||||
cvStartWriteStruct(*fs, "rawdata", CV_NODE_SEQ);
|
||||
cvStartWriteStruct(*fs, 0, CV_NODE_SEQ | CV_NODE_FLOW);
|
||||
cvWriteRawDataBase64(*fs, name, 1, "u");
|
||||
});
|
||||
|
||||
remove(name);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Core_InputOutput, filestorage_yml_vec2i)
|
||||
|
@ -65,11 +65,11 @@ int CV_SLMLTest::run_test_case( int testCaseIdx )
|
||||
{
|
||||
get_test_error( testCaseIdx, &test_resps1 );
|
||||
fname1 = tempfile(".yml.gz");
|
||||
save( fname1.c_str() );
|
||||
save( (fname1 + "?base64").c_str() );
|
||||
load( fname1.c_str() );
|
||||
get_test_error( testCaseIdx, &test_resps2 );
|
||||
fname2 = tempfile(".yml.gz");
|
||||
save( fname2.c_str() );
|
||||
save( (fname2 + "?base64").c_str() );
|
||||
}
|
||||
else
|
||||
ts->printf( cvtest::TS::LOG, "model can not be trained" );
|
||||
@ -280,7 +280,7 @@ TEST(DISABLED_ML_SVM, linear_save_load)
|
||||
svm1 = Algorithm::load<SVM>("SVM45_X_38-1.xml");
|
||||
svm2 = Algorithm::load<SVM>("SVM45_X_38-2.xml");
|
||||
string tname = tempfile("a.xml");
|
||||
svm2->save(tname);
|
||||
svm2->save(tname + "?base64");
|
||||
svm3 = Algorithm::load<SVM>(tname);
|
||||
|
||||
ASSERT_EQ(svm1->getVarCount(), svm2->getVarCount());
|
||||
|
71
samples/cpp/filestorage_base64.cpp
Normal file
71
samples/cpp/filestorage_base64.cpp
Normal file
@ -0,0 +1,71 @@
|
||||
#include "opencv2/core.hpp"
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
static CvFileStorage * three_same_ways_of_write_base64()
|
||||
{
|
||||
CvFileStorage * fs = 0;
|
||||
cv::RNG rng;
|
||||
switch ( rng.uniform( 0, 2 ) )
|
||||
{
|
||||
case 0:
|
||||
//! [suffix_in_file_name]
|
||||
fs = cvOpenFileStorage( "example.yml?base64", 0, CV_STORAGE_WRITE );
|
||||
//! [suffix_in_file_name]
|
||||
break;
|
||||
case 1:
|
||||
//! [flag_write_base64]
|
||||
fs = cvOpenFileStorage( "example.yml" , 0, CV_STORAGE_WRITE_BASE64 );
|
||||
//! [flag_write_base64]
|
||||
break;
|
||||
case 2:
|
||||
//! [flag_write_and_flag_base64]
|
||||
fs = cvOpenFileStorage( "example.yml" , 0, CV_STORAGE_WRITE | CV_STORAGE_BASE64 );
|
||||
//! [flag_write_and_flag_base64]
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return fs;
|
||||
}
|
||||
|
||||
static void two_ways_to_write_rawdata_in_base64()
|
||||
{
|
||||
std::vector<int> rawdata(10, 0x00010203);
|
||||
|
||||
{ // [1]
|
||||
//! [without_base64_flag]
|
||||
CvFileStorage* fs = cvOpenFileStorage( "example.xml", 0, CV_STORAGE_WRITE );
|
||||
// both CV_NODE_SEQ and "binary" are necessary.
|
||||
cvStartWriteStruct(fs, "rawdata", CV_NODE_SEQ | CV_NODE_FLOW, "binary");
|
||||
cvWriteRawDataBase64(fs, rawdata.data(), static_cast<int>(rawdata.size()), "i");
|
||||
cvEndWriteStruct(fs);
|
||||
cvReleaseFileStorage( &fs );
|
||||
//! [without_base64_flag]
|
||||
}
|
||||
|
||||
{ // [2]
|
||||
//! [with_write_base64_flag]
|
||||
CvFileStorage* fs = cvOpenFileStorage( "example.xml", 0, CV_STORAGE_WRITE_BASE64);
|
||||
// parameter, typename "binary" could be omitted.
|
||||
cvStartWriteStruct(fs, "rawdata", CV_NODE_SEQ | CV_NODE_FLOW);
|
||||
cvWriteRawData(fs, rawdata.data(), static_cast<int>(rawdata.size()), "i");
|
||||
cvEndWriteStruct(fs);
|
||||
cvReleaseFileStorage( &fs );
|
||||
//! [with_write_base64_flag]
|
||||
}
|
||||
}
|
||||
|
||||
int main(int /* argc */, char** /* argv */)
|
||||
{
|
||||
{ // base64 mode
|
||||
CvFileStorage * fs = three_same_ways_of_write_base64();
|
||||
cvReleaseFileStorage( &fs );
|
||||
}
|
||||
|
||||
{ // output rawdata by `cvWriteRawdata*`
|
||||
two_ways_to_write_rawdata_in_base64();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user