2013-03-13 20:22:44 +08:00
# include "opencv2/core.hpp"
2012-06-08 01:21:29 +08:00
2010-05-12 01:44:00 +08:00
# include "cascadeclassifier.h"
# include <queue>
using namespace std ;
2013-11-16 23:56:08 +08:00
using namespace cv ;
2010-05-12 01:44:00 +08:00
static const char * stageTypes [ ] = { CC_BOOST } ;
2011-10-07 00:46:03 +08:00
static const char * featureTypes [ ] = { CC_HAAR , CC_LBP , CC_HOG } ;
2010-05-12 01:44:00 +08:00
2012-06-08 01:21:29 +08:00
CvCascadeParams : : CvCascadeParams ( ) : stageType ( defaultStageType ) ,
2010-05-12 01:44:00 +08:00
featureType ( defaultFeatureType ) , winSize ( cvSize ( 24 , 24 ) )
2012-06-08 01:21:29 +08:00
{
name = CC_CASCADE_PARAMS ;
2010-05-12 01:44:00 +08:00
}
CvCascadeParams : : CvCascadeParams ( int _stageType , int _featureType ) : stageType ( _stageType ) ,
featureType ( _featureType ) , winSize ( cvSize ( 24 , 24 ) )
2012-06-08 01:21:29 +08:00
{
2010-05-12 01:44:00 +08:00
name = CC_CASCADE_PARAMS ;
}
//---------------------------- CascadeParams --------------------------------------
void CvCascadeParams : : write ( FileStorage & fs ) const
{
2013-02-25 00:14:01 +08:00
string stageTypeStr = stageType = = BOOST ? CC_BOOST : string ( ) ;
2010-05-12 01:44:00 +08:00
CV_Assert ( ! stageTypeStr . empty ( ) ) ;
fs < < CC_STAGE_TYPE < < stageTypeStr ;
2013-02-25 00:14:01 +08:00
string featureTypeStr = featureType = = CvFeatureParams : : HAAR ? CC_HAAR :
2012-06-08 01:21:29 +08:00
featureType = = CvFeatureParams : : LBP ? CC_LBP :
2011-10-07 00:46:03 +08:00
featureType = = CvFeatureParams : : HOG ? CC_HOG :
0 ;
2010-05-12 01:44:00 +08:00
CV_Assert ( ! stageTypeStr . empty ( ) ) ;
fs < < CC_FEATURE_TYPE < < featureTypeStr ;
fs < < CC_HEIGHT < < winSize . height ;
fs < < CC_WIDTH < < winSize . width ;
}
bool CvCascadeParams : : read ( const FileNode & node )
{
if ( node . empty ( ) )
return false ;
2013-02-25 00:14:01 +08:00
string stageTypeStr , featureTypeStr ;
2010-05-12 01:44:00 +08:00
FileNode rnode = node [ CC_STAGE_TYPE ] ;
if ( ! rnode . isString ( ) )
return false ;
rnode > > stageTypeStr ;
stageType = ! stageTypeStr . compare ( CC_BOOST ) ? BOOST : - 1 ;
if ( stageType = = - 1 )
return false ;
rnode = node [ CC_FEATURE_TYPE ] ;
if ( ! rnode . isString ( ) )
return false ;
rnode > > featureTypeStr ;
featureType = ! featureTypeStr . compare ( CC_HAAR ) ? CvFeatureParams : : HAAR :
2012-06-08 01:21:29 +08:00
! featureTypeStr . compare ( CC_LBP ) ? CvFeatureParams : : LBP :
2011-10-07 00:46:03 +08:00
! featureTypeStr . compare ( CC_HOG ) ? CvFeatureParams : : HOG :
- 1 ;
2010-05-12 01:44:00 +08:00
if ( featureType = = - 1 )
return false ;
node [ CC_HEIGHT ] > > winSize . height ;
node [ CC_WIDTH ] > > winSize . width ;
return winSize . height > 0 & & winSize . width > 0 ;
}
void CvCascadeParams : : printDefaults ( ) const
{
CvParams : : printDefaults ( ) ;
cout < < " [-stageType < " ;
for ( int i = 0 ; i < ( int ) ( sizeof ( stageTypes ) / sizeof ( stageTypes [ 0 ] ) ) ; i + + )
{
cout < < ( i ? " | " : " " ) < < stageTypes [ i ] ;
if ( i = = defaultStageType )
cout < < " (default) " ;
}
cout < < " >] " < < endl ;
cout < < " [-featureType <{ " ;
for ( int i = 0 ; i < ( int ) ( sizeof ( featureTypes ) / sizeof ( featureTypes [ 0 ] ) ) ; i + + )
{
cout < < ( i ? " , " : " " ) < < featureTypes [ i ] ;
if ( i = = defaultStageType )
cout < < " (default) " ;
}
cout < < " }>] " < < endl ;
cout < < " [-w <sampleWidth = " < < winSize . width < < " >] " < < endl ;
cout < < " [-h <sampleHeight = " < < winSize . height < < " >] " < < endl ;
}
void CvCascadeParams : : printAttrs ( ) const
{
cout < < " stageType: " < < stageTypes [ stageType ] < < endl ;
cout < < " featureType: " < < featureTypes [ featureType ] < < endl ;
cout < < " sampleWidth: " < < winSize . width < < endl ;
cout < < " sampleHeight: " < < winSize . height < < endl ;
}
2013-02-25 00:14:01 +08:00
bool CvCascadeParams : : scanAttr ( const string prmName , const string val )
2010-05-12 01:44:00 +08:00
{
bool res = true ;
if ( ! prmName . compare ( " -stageType " ) )
{
for ( int i = 0 ; i < ( int ) ( sizeof ( stageTypes ) / sizeof ( stageTypes [ 0 ] ) ) ; i + + )
if ( ! val . compare ( stageTypes [ i ] ) )
stageType = i ;
}
else if ( ! prmName . compare ( " -featureType " ) )
{
for ( int i = 0 ; i < ( int ) ( sizeof ( featureTypes ) / sizeof ( featureTypes [ 0 ] ) ) ; i + + )
if ( ! val . compare ( featureTypes [ i ] ) )
featureType = i ;
}
else if ( ! prmName . compare ( " -w " ) )
{
winSize . width = atoi ( val . c_str ( ) ) ;
}
else if ( ! prmName . compare ( " -h " ) )
{
winSize . height = atoi ( val . c_str ( ) ) ;
}
else
res = false ;
return res ;
}
//---------------------------- CascadeClassifier --------------------------------------
2013-02-25 00:14:01 +08:00
bool CvCascadeClassifier : : train ( const string _cascadeDirName ,
const string _posFilename ,
const string _negFilename ,
2012-06-08 01:21:29 +08:00
int _numPos , int _numNeg ,
2010-05-12 01:44:00 +08:00
int _precalcValBufSize , int _precalcIdxBufSize ,
int _numStages ,
const CvCascadeParams & _cascadeParams ,
const CvFeatureParams & _featureParams ,
const CvCascadeBoostParams & _stageParams ,
2015-03-06 18:52:26 +08:00
bool baseFormatSave ,
double acceptanceRatioBreakValue )
2012-06-08 01:21:29 +08:00
{
2013-11-21 21:07:53 +08:00
// Start recording clock ticks for training time output
2017-03-03 17:06:35 +08:00
double time = ( double ) getTickCount ( ) ;
2013-11-21 21:07:53 +08:00
2010-05-12 01:44:00 +08:00
if ( _cascadeDirName . empty ( ) | | _posFilename . empty ( ) | | _negFilename . empty ( ) )
CV_Error ( CV_StsBadArg , " _cascadeDirName or _bgfileName or _vecFileName is NULL " ) ;
string dirName ;
2012-03-20 17:02:01 +08:00
if ( _cascadeDirName . find_last_of ( " / \\ " ) = = ( _cascadeDirName . length ( ) - 1 ) )
dirName = _cascadeDirName ;
2010-05-12 01:44:00 +08:00
else
2012-03-20 17:02:01 +08:00
dirName = _cascadeDirName + ' / ' ;
2010-05-12 01:44:00 +08:00
numPos = _numPos ;
numNeg = _numNeg ;
numStages = _numStages ;
if ( ! imgReader . create ( _posFilename , _negFilename , _cascadeParams . winSize ) )
{
cout < < " Image reader can not be created from -vec " < < _posFilename
< < " and -bg " < < _negFilename < < " . " < < endl ;
return false ;
}
if ( ! load ( dirName ) )
{
cascadeParams = _cascadeParams ;
featureParams = CvFeatureParams : : create ( cascadeParams . featureType ) ;
featureParams - > init ( _featureParams ) ;
2013-08-13 22:33:06 +08:00
stageParams = makePtr < CvCascadeBoostParams > ( ) ;
2010-05-12 01:44:00 +08:00
* stageParams = _stageParams ;
featureEvaluator = CvFeatureEvaluator : : create ( cascadeParams . featureType ) ;
2013-08-13 22:33:06 +08:00
featureEvaluator - > init ( featureParams , numPos + numNeg , cascadeParams . winSize ) ;
2010-05-12 01:44:00 +08:00
stageClassifiers . reserve ( numStages ) ;
2015-02-17 17:25:18 +08:00
} else {
// Make sure that if model parameters are preloaded, that people are aware of this,
// even when passing other parameters to the training command
cout < < " --------------------------------------------------------------------------------- " < < endl ;
cout < < " Training parameters are pre-loaded from the parameter file in data folder! " < < endl ;
cout < < " Please empty this folder if you want to use a NEW set of training parameters. " < < endl ;
cout < < " --------------------------------------------------------------------------------- " < < endl ;
2010-05-12 01:44:00 +08:00
}
cout < < " PARAMETERS: " < < endl ;
cout < < " cascadeDirName: " < < _cascadeDirName < < endl ;
cout < < " vecFileName: " < < _posFilename < < endl ;
cout < < " bgFileName: " < < _negFilename < < endl ;
cout < < " numPos: " < < _numPos < < endl ;
cout < < " numNeg: " < < _numNeg < < endl ;
cout < < " numStages: " < < numStages < < endl ;
cout < < " precalcValBufSize[Mb] : " < < _precalcValBufSize < < endl ;
cout < < " precalcIdxBufSize[Mb] : " < < _precalcIdxBufSize < < endl ;
2015-03-06 18:52:26 +08:00
cout < < " acceptanceRatioBreakValue : " < < acceptanceRatioBreakValue < < endl ;
2010-05-12 01:44:00 +08:00
cascadeParams . printAttrs ( ) ;
stageParams - > printAttrs ( ) ;
featureParams - > printAttrs ( ) ;
2016-03-17 20:34:01 +08:00
cout < < " Number of unique features given windowSize [ " < < _cascadeParams . winSize . width < < " , " < < _cascadeParams . winSize . height < < " ] : " < < featureEvaluator - > getNumFeatures ( ) < < " " < < endl ;
2010-05-12 01:44:00 +08:00
int startNumStages = ( int ) stageClassifiers . size ( ) ;
if ( startNumStages > 1 )
cout < < endl < < " Stages 0- " < < startNumStages - 1 < < " are loaded " < < endl ;
else if ( startNumStages = = 1 )
cout < < endl < < " Stage 0 is loaded " < < endl ;
2012-06-08 01:21:29 +08:00
2010-05-12 01:44:00 +08:00
double requiredLeafFARate = pow ( ( double ) stageParams - > maxFalseAlarm , ( double ) numStages ) /
( double ) stageParams - > max_depth ;
double tempLeafFARate ;
2012-06-08 01:21:29 +08:00
2010-05-12 01:44:00 +08:00
for ( int i = startNumStages ; i < numStages ; i + + )
{
cout < < endl < < " ===== TRAINING " < < i < < " -stage ===== " < < endl ;
cout < < " <BEGIN " < < endl ;
2014-06-27 02:28:47 +08:00
if ( ! updateTrainingSet ( requiredLeafFARate , tempLeafFARate ) )
2010-05-12 01:44:00 +08:00
{
cout < < " Train dataset for temp stage can not be filled. "
2015-03-06 18:52:26 +08:00
" Branch training terminated. " < < endl ;
2010-05-12 01:44:00 +08:00
break ;
}
if ( tempLeafFARate < = requiredLeafFARate )
{
cout < < " Required leaf false alarm rate achieved. "
2015-03-06 18:52:26 +08:00
" Branch training terminated. " < < endl ;
break ;
}
2015-04-03 18:54:05 +08:00
if ( ( tempLeafFARate < = acceptanceRatioBreakValue ) & & ( acceptanceRatioBreakValue > = 0 ) ) {
2015-03-06 18:52:26 +08:00
cout < < " The required acceptanceRatio for the model has been reached to avoid overfitting of trainingdata. "
" Branch training terminated. " < < endl ;
2010-05-12 01:44:00 +08:00
break ;
}
2013-08-13 22:33:06 +08:00
Ptr < CvCascadeBoost > tempStage = makePtr < CvCascadeBoost > ( ) ;
bool isStageTrained = tempStage - > train ( featureEvaluator ,
2012-07-12 20:32:54 +08:00
curNumSamples , _precalcValBufSize , _precalcIdxBufSize ,
2013-08-13 22:33:06 +08:00
* stageParams ) ;
2010-05-12 01:44:00 +08:00
cout < < " END> " < < endl ;
2012-06-08 01:21:29 +08:00
2012-07-12 20:32:54 +08:00
if ( ! isStageTrained )
break ;
stageClassifiers . push_back ( tempStage ) ;
2010-05-12 01:44:00 +08:00
// save params
2012-07-12 20:32:54 +08:00
if ( i = = 0 )
2010-05-12 01:44:00 +08:00
{
2012-07-12 20:32:54 +08:00
std : : string paramsFilename = dirName + CC_PARAMS_FILENAME ;
FileStorage fs ( paramsFilename , FileStorage : : WRITE ) ;
2010-05-12 01:44:00 +08:00
if ( ! fs . isOpened ( ) )
{
2012-07-12 20:32:54 +08:00
cout < < " Parameters can not be written, because file " < < paramsFilename
2010-05-12 01:44:00 +08:00
< < " can not be opened. " < < endl ;
return false ;
}
2012-07-12 20:32:54 +08:00
fs < < FileStorage : : getDefaultObjectName ( paramsFilename ) < < " { " ;
2010-05-12 01:44:00 +08:00
writeParams ( fs ) ;
fs < < " } " ;
}
// save current stage
2022-05-16 00:20:45 +08:00
char buf [ 32 ] ;
2022-06-25 11:48:22 +08:00
snprintf ( buf , sizeof ( buf ) , " %s%d " , " stage " , i ) ;
2012-07-12 20:32:54 +08:00
string stageFilename = dirName + buf + " .xml " ;
FileStorage fs ( stageFilename , FileStorage : : WRITE ) ;
2010-05-12 01:44:00 +08:00
if ( ! fs . isOpened ( ) )
{
2012-07-12 20:32:54 +08:00
cout < < " Current stage can not be written, because file " < < stageFilename
2010-05-12 01:44:00 +08:00
< < " can not be opened. " < < endl ;
return false ;
}
2012-07-12 20:32:54 +08:00
fs < < FileStorage : : getDefaultObjectName ( stageFilename ) < < " { " ;
2010-05-12 01:44:00 +08:00
tempStage - > write ( fs , Mat ( ) ) ;
fs < < " } " ;
2013-11-21 21:07:53 +08:00
// Output training time up till now
2017-03-03 17:06:35 +08:00
double seconds = ( ( double ) getTickCount ( ) - time ) / getTickFrequency ( ) ;
2013-11-21 21:07:53 +08:00
int days = int ( seconds ) / 60 / 60 / 24 ;
int hours = ( int ( seconds ) / 60 / 60 ) % 24 ;
int minutes = ( int ( seconds ) / 60 ) % 60 ;
int seconds_left = int ( seconds ) % 60 ;
cout < < " Training until now has taken " < < days < < " days " < < hours < < " hours " < < minutes < < " minutes " < < seconds_left < < " seconds. " < < endl ;
2010-05-12 01:44:00 +08:00
}
2012-07-12 20:32:54 +08:00
if ( stageClassifiers . size ( ) = = 0 )
{
cout < < " Cascade classifier can't be trained. Check the used training parameters. " < < endl ;
return false ;
}
2010-05-12 01:44:00 +08:00
save ( dirName + CC_CASCADE_FILENAME , baseFormatSave ) ;
2012-07-12 20:32:54 +08:00
2010-05-12 01:44:00 +08:00
return true ;
}
int CvCascadeClassifier : : predict ( int sampleIdx )
{
CV_DbgAssert ( sampleIdx < numPos + numNeg ) ;
for ( vector < Ptr < CvCascadeBoost > > : : iterator it = stageClassifiers . begin ( ) ;
2016-04-29 21:41:39 +08:00
it ! = stageClassifiers . end ( ) ; + + it )
2010-05-12 01:44:00 +08:00
{
if ( ( * it ) - > predict ( sampleIdx ) = = 0.f )
return 0 ;
}
return 1 ;
}
2014-06-27 02:28:47 +08:00
bool CvCascadeClassifier : : updateTrainingSet ( double minimumAcceptanceRatio , double & acceptanceRatio )
2010-05-12 01:44:00 +08:00
{
int64 posConsumed = 0 , negConsumed = 0 ;
imgReader . restart ( ) ;
2014-06-27 02:28:47 +08:00
int posCount = fillPassedSamples ( 0 , numPos , true , 0 , posConsumed ) ;
2010-05-12 01:44:00 +08:00
if ( ! posCount )
return false ;
cout < < " POS count : consumed " < < posCount < < " : " < < ( int ) posConsumed < < endl ;
2011-10-07 22:00:19 +08:00
int proNumNeg = cvRound ( ( ( ( double ) numNeg ) * ( ( double ) posCount ) ) / numPos ) ; // apply only a fraction of negative samples. double is required since overflow is possible
2014-06-27 02:28:47 +08:00
int negCount = fillPassedSamples ( posCount , proNumNeg , false , minimumAcceptanceRatio , negConsumed ) ;
2010-05-12 01:44:00 +08:00
if ( ! negCount )
2015-12-24 10:26:42 +08:00
if ( ! ( negConsumed > 0 & & ( ( double ) negCount + 1 ) / ( double ) negConsumed < = minimumAcceptanceRatio ) )
return false ;
2011-05-04 19:12:17 +08:00
2010-05-12 01:44:00 +08:00
curNumSamples = posCount + negCount ;
acceptanceRatio = negConsumed = = 0 ? 0 : ( ( double ) negCount / ( double ) ( int64 ) negConsumed ) ;
cout < < " NEG count : acceptanceRatio " < < negCount < < " : " < < acceptanceRatio < < endl ;
return true ;
}
2014-06-27 02:28:47 +08:00
int CvCascadeClassifier : : fillPassedSamples ( int first , int count , bool isPositive , double minimumAcceptanceRatio , int64 & consumed )
2010-05-12 01:44:00 +08:00
{
int getcount = 0 ;
Mat img ( cascadeParams . winSize , CV_8UC1 ) ;
for ( int i = first ; i < first + count ; i + + )
{
for ( ; ; )
{
2014-06-27 02:28:47 +08:00
if ( consumed ! = 0 & & ( ( double ) getcount + 1 ) / ( double ) ( int64 ) consumed < = minimumAcceptanceRatio )
return getcount ;
2010-05-12 01:44:00 +08:00
bool isGetImg = isPositive ? imgReader . getPos ( img ) :
imgReader . getNeg ( img ) ;
2012-06-08 01:21:29 +08:00
if ( ! isGetImg )
2010-05-12 01:44:00 +08:00
return getcount ;
consumed + + ;
featureEvaluator - > setImage ( img , isPositive ? 1 : 0 , i ) ;
2015-12-02 11:50:22 +08:00
if ( predict ( i ) = = 1 )
2010-05-12 01:44:00 +08:00
{
getcount + + ;
2013-11-21 21:07:53 +08:00
printf ( " %s current samples: %d \r " , isPositive ? " POS " : " NEG " , getcount ) ;
2019-04-12 23:51:35 +08:00
fflush ( stdout ) ;
2010-05-12 01:44:00 +08:00
break ;
}
}
}
return getcount ;
}
void CvCascadeClassifier : : writeParams ( FileStorage & fs ) const
{
cascadeParams . write ( fs ) ;
fs < < CC_STAGE_PARAMS < < " { " ; stageParams - > write ( fs ) ; fs < < " } " ;
fs < < CC_FEATURE_PARAMS < < " { " ; featureParams - > write ( fs ) ; fs < < " } " ;
}
void CvCascadeClassifier : : writeFeatures ( FileStorage & fs , const Mat & featureMap ) const
{
2013-08-13 22:33:06 +08:00
featureEvaluator - > writeFeatures ( fs , featureMap ) ;
2010-05-12 01:44:00 +08:00
}
void CvCascadeClassifier : : writeStages ( FileStorage & fs , const Mat & featureMap ) const
{
char cmnt [ 30 ] ;
int i = 0 ;
2012-06-08 01:21:29 +08:00
fs < < CC_STAGES < < " [ " ;
2010-05-12 01:44:00 +08:00
for ( vector < Ptr < CvCascadeBoost > > : : const_iterator it = stageClassifiers . begin ( ) ;
2016-04-29 21:41:39 +08:00
it ! = stageClassifiers . end ( ) ; + + it , + + i )
2010-05-12 01:44:00 +08:00
{
2022-06-25 11:48:22 +08:00
snprintf ( cmnt , sizeof ( cmnt ) , " stage %d " , i ) ;
2023-05-23 14:19:27 +08:00
fs . writeComment ( cmnt ) ;
2010-05-12 01:44:00 +08:00
fs < < " { " ;
2013-08-13 22:33:06 +08:00
( * it ) - > write ( fs , featureMap ) ;
2010-05-12 01:44:00 +08:00
fs < < " } " ;
}
fs < < " ] " ;
}
bool CvCascadeClassifier : : readParams ( const FileNode & node )
{
if ( ! node . isMap ( ) | | ! cascadeParams . read ( node ) )
return false ;
2012-06-08 01:21:29 +08:00
2013-08-13 22:33:06 +08:00
stageParams = makePtr < CvCascadeBoostParams > ( ) ;
2010-05-12 01:44:00 +08:00
FileNode rnode = node [ CC_STAGE_PARAMS ] ;
if ( ! stageParams - > read ( rnode ) )
return false ;
2012-06-08 01:21:29 +08:00
2010-05-12 01:44:00 +08:00
featureParams = CvFeatureParams : : create ( cascadeParams . featureType ) ;
rnode = node [ CC_FEATURE_PARAMS ] ;
if ( ! featureParams - > read ( rnode ) )
return false ;
2012-06-08 01:21:29 +08:00
return true ;
2010-05-12 01:44:00 +08:00
}
bool CvCascadeClassifier : : readStages ( const FileNode & node )
{
FileNode rnode = node [ CC_STAGES ] ;
if ( ! rnode . empty ( ) | | ! rnode . isSeq ( ) )
return false ;
stageClassifiers . reserve ( numStages ) ;
FileNodeIterator it = rnode . begin ( ) ;
for ( int i = 0 ; i < min ( ( int ) rnode . size ( ) , numStages ) ; i + + , it + + )
{
2013-08-13 22:33:06 +08:00
Ptr < CvCascadeBoost > tempStage = makePtr < CvCascadeBoost > ( ) ;
if ( ! tempStage - > read ( * it , featureEvaluator , * stageParams ) )
2010-05-12 01:44:00 +08:00
return false ;
stageClassifiers . push_back ( tempStage ) ;
}
return true ;
}
// For old Haar Classifier file saving
2016-12-18 02:54:55 +08:00
# define ICV_HAAR_TYPE_ID "opencv-haar-classifier"
2010-05-12 01:44:00 +08:00
# define ICV_HAAR_SIZE_NAME "size"
# define ICV_HAAR_STAGES_NAME "stages"
# define ICV_HAAR_TREES_NAME "trees"
# define ICV_HAAR_FEATURE_NAME "feature"
# define ICV_HAAR_RECTS_NAME "rects"
# define ICV_HAAR_TILTED_NAME "tilted"
# define ICV_HAAR_THRESHOLD_NAME "threshold"
# define ICV_HAAR_LEFT_NODE_NAME "left_node"
# define ICV_HAAR_LEFT_VAL_NAME "left_val"
# define ICV_HAAR_RIGHT_NODE_NAME "right_node"
# define ICV_HAAR_RIGHT_VAL_NAME "right_val"
# define ICV_HAAR_STAGE_THRESHOLD_NAME "stage_threshold"
# define ICV_HAAR_PARENT_NAME "parent"
# define ICV_HAAR_NEXT_NAME "next"
2013-02-25 00:14:01 +08:00
void CvCascadeClassifier : : save ( const string filename , bool baseFormat )
2010-05-12 01:44:00 +08:00
{
FileStorage fs ( filename , FileStorage : : WRITE ) ;
if ( ! fs . isOpened ( ) )
return ;
2016-12-18 02:54:55 +08:00
fs < < FileStorage : : getDefaultObjectName ( filename ) ;
2010-05-12 01:44:00 +08:00
if ( ! baseFormat )
{
2012-06-08 01:21:29 +08:00
Mat featureMap ;
2010-05-12 01:44:00 +08:00
getUsedFeaturesIdxMap ( featureMap ) ;
2016-12-18 02:54:55 +08:00
fs < < " { " ;
2010-05-12 01:44:00 +08:00
writeParams ( fs ) ;
fs < < CC_STAGE_NUM < < ( int ) stageClassifiers . size ( ) ;
writeStages ( fs , featureMap ) ;
writeFeatures ( fs , featureMap ) ;
}
else
{
//char buf[256];
CvSeq * weak ;
if ( cascadeParams . featureType ! = CvFeatureParams : : HAAR )
CV_Error ( CV_StsBadFunc , " old file format is used for Haar-like features only " ) ;
2016-12-18 02:54:55 +08:00
fs < < " {: " ICV_HAAR_TYPE_ID ;
2012-06-08 01:21:29 +08:00
fs < < ICV_HAAR_SIZE_NAME < < " [: " < < cascadeParams . winSize . width < <
2010-05-12 01:44:00 +08:00
cascadeParams . winSize . height < < " ] " ;
fs < < ICV_HAAR_STAGES_NAME < < " [ " ;
for ( size_t si = 0 ; si < stageClassifiers . size ( ) ; si + + )
{
fs < < " { " ; //stage
2022-06-25 11:48:22 +08:00
/*snprintf( buf, sizeof(buf), "stage %d", si );
2010-05-12 01:44:00 +08:00
CV_CALL ( cvWriteComment ( fs , buf , 1 ) ) ; */
weak = stageClassifiers [ si ] - > get_weak_predictors ( ) ;
fs < < ICV_HAAR_TREES_NAME < < " [ " ;
for ( int wi = 0 ; wi < weak - > total ; wi + + )
{
2023-05-23 14:19:27 +08:00
int total_inner_node_idx = - 1 ;
2010-05-12 01:44:00 +08:00
queue < const CvDTreeNode * > inner_nodes_queue ;
CvCascadeBoostTree * tree = * ( ( CvCascadeBoostTree * * ) cvGetSeqElem ( weak , wi ) ) ;
2012-06-08 01:21:29 +08:00
2010-05-12 01:44:00 +08:00
fs < < " [ " ;
2022-06-25 11:48:22 +08:00
/*snprintf( buf, sizeof(buf), "tree %d", wi );
2010-05-12 01:44:00 +08:00
CV_CALL ( cvWriteComment ( fs , buf , 1 ) ) ; */
const CvDTreeNode * tempNode ;
2012-06-08 01:21:29 +08:00
2010-05-12 01:44:00 +08:00
inner_nodes_queue . push ( tree - > get_root ( ) ) ;
total_inner_node_idx + + ;
2012-06-08 01:21:29 +08:00
2010-05-12 01:44:00 +08:00
while ( ! inner_nodes_queue . empty ( ) )
{
tempNode = inner_nodes_queue . front ( ) ;
fs < < " { " ;
fs < < ICV_HAAR_FEATURE_NAME < < " { " ;
2013-08-13 22:33:06 +08:00
( ( CvHaarEvaluator * ) featureEvaluator . get ( ) ) - > writeFeature ( fs , tempNode - > split - > var_idx ) ;
2010-05-12 01:44:00 +08:00
fs < < " } " ;
fs < < ICV_HAAR_THRESHOLD_NAME < < tempNode - > split - > ord . c ;
if ( tempNode - > left - > left | | tempNode - > left - > right )
{
inner_nodes_queue . push ( tempNode - > left ) ;
total_inner_node_idx + + ;
fs < < ICV_HAAR_LEFT_NODE_NAME < < total_inner_node_idx ;
}
else
fs < < ICV_HAAR_LEFT_VAL_NAME < < tempNode - > left - > value ;
if ( tempNode - > right - > left | | tempNode - > right - > right )
{
inner_nodes_queue . push ( tempNode - > right ) ;
total_inner_node_idx + + ;
fs < < ICV_HAAR_RIGHT_NODE_NAME < < total_inner_node_idx ;
}
else
fs < < ICV_HAAR_RIGHT_VAL_NAME < < tempNode - > right - > value ;
fs < < " } " ; // ICV_HAAR_FEATURE_NAME
inner_nodes_queue . pop ( ) ;
}
fs < < " ] " ;
}
fs < < " ] " ; //ICV_HAAR_TREES_NAME
fs < < ICV_HAAR_STAGE_THRESHOLD_NAME < < stageClassifiers [ si ] - > getThreshold ( ) ;
fs < < ICV_HAAR_PARENT_NAME < < ( int ) si - 1 < < ICV_HAAR_NEXT_NAME < < - 1 ;
fs < < " } " ; //stage
} /* for each stage */
fs < < " ] " ; //ICV_HAAR_STAGES_NAME
}
fs < < " } " ;
}
2013-02-25 00:14:01 +08:00
bool CvCascadeClassifier : : load ( const string cascadeDirName )
2010-05-12 01:44:00 +08:00
{
FileStorage fs ( cascadeDirName + CC_PARAMS_FILENAME , FileStorage : : READ ) ;
if ( ! fs . isOpened ( ) )
return false ;
FileNode node = fs . getFirstTopLevelNode ( ) ;
if ( ! readParams ( node ) )
return false ;
featureEvaluator = CvFeatureEvaluator : : create ( cascadeParams . featureType ) ;
2013-08-13 22:33:06 +08:00
featureEvaluator - > init ( featureParams , numPos + numNeg , cascadeParams . winSize ) ;
2010-05-12 01:44:00 +08:00
fs . release ( ) ;
2022-06-25 11:48:22 +08:00
char buf [ 5 + 10 + 1 ] = { 0 } ;
2010-05-12 01:44:00 +08:00
for ( int si = 0 ; si < numStages ; si + + )
{
2022-06-25 11:48:22 +08:00
snprintf ( buf , sizeof ( buf ) , " %s%d " , " stage " , si ) ;
2010-05-12 01:44:00 +08:00
fs . open ( cascadeDirName + buf + " .xml " , FileStorage : : READ ) ;
node = fs . getFirstTopLevelNode ( ) ;
if ( ! fs . isOpened ( ) )
break ;
2013-08-13 22:33:06 +08:00
Ptr < CvCascadeBoost > tempStage = makePtr < CvCascadeBoost > ( ) ;
2010-05-12 01:44:00 +08:00
2013-08-13 22:33:06 +08:00
if ( ! tempStage - > read ( node , featureEvaluator , * stageParams ) )
2010-05-12 01:44:00 +08:00
{
fs . release ( ) ;
break ;
}
stageClassifiers . push_back ( tempStage ) ;
}
return true ;
}
void CvCascadeClassifier : : getUsedFeaturesIdxMap ( Mat & featureMap )
{
2011-10-07 00:46:03 +08:00
int varCount = featureEvaluator - > getNumFeatures ( ) * featureEvaluator - > getFeatureSize ( ) ;
featureMap . create ( 1 , varCount , CV_32SC1 ) ;
2010-05-12 01:44:00 +08:00
featureMap . setTo ( Scalar ( - 1 ) ) ;
2012-06-08 01:21:29 +08:00
2010-05-12 01:44:00 +08:00
for ( vector < Ptr < CvCascadeBoost > > : : const_iterator it = stageClassifiers . begin ( ) ;
2016-04-29 21:41:39 +08:00
it ! = stageClassifiers . end ( ) ; + + it )
2013-08-13 22:33:06 +08:00
( * it ) - > markUsedFeaturesInMap ( featureMap ) ;
2012-06-08 01:21:29 +08:00
2011-10-07 00:46:03 +08:00
for ( int fi = 0 , idx = 0 ; fi < varCount ; fi + + )
2010-05-12 01:44:00 +08:00
if ( featureMap . at < int > ( 0 , fi ) > = 0 )
featureMap . ptr < int > ( 0 ) [ fi ] = idx + + ;
}