1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

Merge pull request #23594 from fdivitto:fdivitto-traincascade-patch

fix: traincascade, use C++ persistence API #23594

This pull allows to compile traincascade application with OpenCV 4.6. Changes uses new persistence C++ API in place of legacy one.
This commit is contained in:
Fabrizio Di Vittorio
2023-05-23 08:19:27 +02:00
committed by GitHub
parent e3c5c0906b
commit 044a322519
7 changed files with 278 additions and 282 deletions
+54 -56
View File
@@ -876,21 +876,21 @@ CvBoostTree::calc_node_value( CvDTreeNode* node )
}
void CvBoostTree::read( CvFileStorage* fs, CvFileNode* fnode, CvBoost* _ensemble, CvDTreeTrainData* _data )
void CvBoostTree::read( const cv::FileNode& fnode, CvBoost* _ensemble, CvDTreeTrainData* _data )
{
CvDTree::read( fs, fnode, _data );
CvDTree::read( fnode, _data );
ensemble = _ensemble;
}
void CvBoostTree::read( CvFileStorage*, CvFileNode* )
void CvBoostTree::read( cv::FileNode& )
{
assert(0);
}
void CvBoostTree::read( CvFileStorage* _fs, CvFileNode* _node,
void CvBoostTree::read( cv::FileNode& _node,
CvDTreeTrainData* _data )
{
CvDTree::read( _fs, _node, _data );
CvDTree::read( _node, _data );
}
@@ -1884,7 +1884,7 @@ float CvBoost::calc_error( CvMLData* _data, int type, std::vector<float> *resp )
return err;
}
void CvBoost::write_params( CvFileStorage* fs ) const
void CvBoost::write_params( cv::FileStorage& fs ) const
{
const char* boost_type_str =
params.boost_type == DISCRETE ? "DiscreteAdaboost" :
@@ -1899,35 +1899,33 @@ void CvBoost::write_params( CvFileStorage* fs ) const
params.boost_type == SQERR ? "SquaredErr" : 0;
if( boost_type_str )
cvWriteString( fs, "boosting_type", boost_type_str );
fs.write( "boosting_type", boost_type_str );
else
cvWriteInt( fs, "boosting_type", params.boost_type );
fs.write( "boosting_type", params.boost_type );
if( split_crit_str )
cvWriteString( fs, "splitting_criteria", split_crit_str );
fs.write( "splitting_criteria", split_crit_str );
else
cvWriteInt( fs, "splitting_criteria", params.split_criteria );
fs.write( "splitting_criteria", params.split_criteria );
cvWriteInt( fs, "ntrees", weak->total );
cvWriteReal( fs, "weight_trimming_rate", params.weight_trim_rate );
fs.write( "ntrees", weak->total );
fs.write( "weight_trimming_rate", params.weight_trim_rate );
data->write_params( fs );
}
void CvBoost::read_params( CvFileStorage* fs, CvFileNode* fnode )
void CvBoost::read_params( cv::FileNode& fnode )
{
CV_FUNCNAME( "CvBoost::read_params" );
__BEGIN__;
CvFileNode* temp;
if( !fnode || !CV_NODE_IS_MAP(fnode->tag) )
if( fnode.empty() || !fnode.isMap() )
return;
data = new CvDTreeTrainData();
CV_CALL( data->read_params(fs, fnode));
data->read_params( fnode );
data->shared = true;
params.max_depth = data->params.max_depth;
@@ -1937,41 +1935,41 @@ void CvBoost::read_params( CvFileStorage* fs, CvFileNode* fnode )
params.regression_accuracy = data->params.regression_accuracy;
params.use_surrogates = data->params.use_surrogates;
temp = cvGetFileNodeByName( fs, fnode, "boosting_type" );
if( !temp )
cv::FileNode temp = fnode[ "boosting_type" ];
if( temp.empty() )
return;
if( temp && CV_NODE_IS_STRING(temp->tag) )
if ( temp.isString() )
{
const char* boost_type_str = cvReadString( temp, "" );
params.boost_type = strcmp( boost_type_str, "DiscreteAdaboost" ) == 0 ? DISCRETE :
strcmp( boost_type_str, "RealAdaboost" ) == 0 ? REAL :
strcmp( boost_type_str, "LogitBoost" ) == 0 ? LOGIT :
strcmp( boost_type_str, "GentleAdaboost" ) == 0 ? GENTLE : -1;
std::string boost_type_str = temp;
params.boost_type = (boost_type_str == "DiscreteAdaboost") ? DISCRETE :
(boost_type_str == "RealAdaboost") ? REAL :
(boost_type_str == "LogitBoost") ? LOGIT :
(boost_type_str == "GentleAdaboost") ? GENTLE : -1;
}
else
params.boost_type = cvReadInt( temp, -1 );
params.boost_type = temp.empty() ? -1 : (int)temp;
if( params.boost_type < DISCRETE || params.boost_type > GENTLE )
CV_ERROR( CV_StsBadArg, "Unknown boosting type" );
temp = cvGetFileNodeByName( fs, fnode, "splitting_criteria" );
if( temp && CV_NODE_IS_STRING(temp->tag) )
temp = fnode[ "splitting_criteria" ];
if( !temp.empty() && temp.isString() )
{
const char* split_crit_str = cvReadString( temp, "" );
params.split_criteria = strcmp( split_crit_str, "Default" ) == 0 ? DEFAULT :
strcmp( split_crit_str, "Gini" ) == 0 ? GINI :
strcmp( split_crit_str, "Misclassification" ) == 0 ? MISCLASS :
strcmp( split_crit_str, "SquaredErr" ) == 0 ? SQERR : -1;
std::string split_crit_str = temp;
params.split_criteria = ( split_crit_str == "Default" ) ? DEFAULT :
( split_crit_str == "Gini" ) ? GINI :
( split_crit_str == "Misclassification" ) ? MISCLASS :
( split_crit_str == "SquaredErr" ) ? SQERR : -1;
}
else
params.split_criteria = cvReadInt( temp, -1 );
params.split_criteria = temp.empty() ? -1 : (int) temp;
if( params.split_criteria < DEFAULT || params.boost_type > SQERR )
CV_ERROR( CV_StsBadArg, "Unknown boosting type" );
params.weak_count = cvReadIntByName( fs, fnode, "ntrees" );
params.weight_trim_rate = cvReadRealByName( fs, fnode, "weight_trimming_rate", 0. );
params.weak_count = (int) fnode[ "ntrees" ];
params.weight_trim_rate = (double)fnode["weight_trimming_rate"];
__END__;
}
@@ -1979,29 +1977,29 @@ void CvBoost::read_params( CvFileStorage* fs, CvFileNode* fnode )
void
CvBoost::read( CvFileStorage* fs, CvFileNode* node )
CvBoost::read( cv::FileNode& node )
{
CV_FUNCNAME( "CvBoost::read" );
__BEGIN__;
CvSeqReader reader;
CvFileNode* trees_fnode;
cv::FileNodeIterator reader;
cv::FileNode trees_fnode;
CvMemStorage* storage;
int i, ntrees;
int ntrees;
clear();
read_params( fs, node );
read_params( node );
if( !data )
EXIT;
trees_fnode = cvGetFileNodeByName( fs, node, "trees" );
if( !trees_fnode || !CV_NODE_IS_SEQ(trees_fnode->tag) )
trees_fnode = node[ "trees" ];
if( trees_fnode.empty() || !trees_fnode.isSeq() )
CV_ERROR( CV_StsParseError, "<trees> tag is missing" );
cvStartReadSeq( trees_fnode->data.seq, &reader );
ntrees = trees_fnode->data.seq->total;
reader = trees_fnode.begin();
ntrees = (int) trees_fnode.size();
if( ntrees != params.weak_count )
CV_ERROR( CV_StsUnmatchedSizes,
@@ -2010,11 +2008,11 @@ CvBoost::read( CvFileStorage* fs, CvFileNode* node )
CV_CALL( storage = cvCreateMemStorage() );
weak = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvBoostTree*), storage );
for( i = 0; i < ntrees; i++ )
for( int i = 0; i < ntrees; i++ )
{
CvBoostTree* tree = new CvBoostTree();
CV_CALL(tree->read( fs, (CvFileNode*)reader.ptr, this, data ));
CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
tree->read( *reader, this, data );
reader++;
cvSeqPush( weak, &tree );
}
get_active_vars();
@@ -2024,7 +2022,7 @@ CvBoost::read( CvFileStorage* fs, CvFileNode* node )
void
CvBoost::write( CvFileStorage* fs, const char* name ) const
CvBoost::write( cv::FileStorage& fs, const char* name ) const
{
CV_FUNCNAME( "CvBoost::write" );
@@ -2033,27 +2031,27 @@ CvBoost::write( CvFileStorage* fs, const char* name ) const
CvSeqReader reader;
int i;
cvStartWriteStruct( fs, name, CV_NODE_MAP, CV_TYPE_NAME_ML_BOOSTING );
fs.startWriteStruct( name, cv::FileNode::MAP, CV_TYPE_NAME_ML_BOOSTING );
if( !weak )
CV_ERROR( CV_StsBadArg, "The classifier has not been trained yet" );
write_params( fs );
cvStartWriteStruct( fs, "trees", CV_NODE_SEQ );
fs.startWriteStruct( "trees", cv::FileNode::SEQ );
cvStartReadSeq( weak, &reader );
cvStartReadSeq(weak, &reader);
for( i = 0; i < weak->total; i++ )
{
CvBoostTree* tree;
CV_READ_SEQ_ELEM( tree, reader );
cvStartWriteStruct( fs, 0, CV_NODE_MAP );
fs.startWriteStruct( 0, cv::FileNode::MAP );
tree->write( fs );
cvEndWriteStruct( fs );
fs.endWriteStruct();
}
cvEndWriteStruct( fs );
cvEndWriteStruct( fs );
fs.endWriteStruct();
fs.endWriteStruct();
__END__;
}