JD2022-TU1/main/extern/KinectGesture/Src/KinectGestureDetectorTrainer.cpp

116 lines
4.5 KiB
C++

#pragma once
#include "KinectGestureDetectorTrainer.h"
#ifndef GESTURE_EVALUATOR
#include "KinectGestureDetector.h"
#endif
#include "GestureDetector.h"
#if defined( GESTURE_TRAINER )
namespace KinectGesture
{
HRESULT GenerateLabeledExamples( const CHAR* szGestureName,
CHAR* const* szExampleFiles,
const UINT uiExampleFileCount,
const CHAR* szDestPath,
const INT iSkeletonIndex,
INT8 iLabels[],
CHAR* const* szOutputFiles )
{
// We want to time how long this takes
DWORD dwStart = GetTickCount();
GestureDetectorTrainer gestureDetectorTrainer;
HRESULT hResult = gestureDetectorTrainer.GenerateLabeledExamples( szGestureName, szExampleFiles, uiExampleFileCount, szDestPath, TRUE, iSkeletonIndex, iLabels, szOutputFiles );
DWORD dwStop = GetTickCount();
DWORD dwSeconds = ( (dwStop - dwStart ) / 1000 ) % 60;
DWORD dwMinutes = ( (dwStop - dwStart ) / 1000) / 60;
printf( "\n\nDuration: %d minutes, %d seconds", dwMinutes, dwSeconds );
printf( "\n\n%s", szDestPath );
const UINT nNumTrainingExamples = gestureDetectorTrainer.GetNumExamples();
if( nNumTrainingExamples > 0 )
{
const UINT nNumGestures = gestureDetectorTrainer.GetNumGestures();
const UINT nNumTrainingGestures = gestureDetectorTrainer.GetNumTrainingGestures();
const FLOAT fRatioPosNegExamples = (FLOAT)gestureDetectorTrainer.GetNumNegativeExamples() / (FLOAT)gestureDetectorTrainer.GetNumPositiveExamples();
const FLOAT fRatioPosNegGestures = (FLOAT)(nNumGestures - nNumTrainingGestures) / (FLOAT)nNumTrainingGestures;
printf( "\n\tNum Labeled Examples: %d", nNumTrainingExamples );
printf( "\n\tRatio positive to negative labeled examples: 1.0 : %f", fRatioPosNegExamples );
printf( "\n\tTotal Num Gestures: %d", nNumGestures );
printf( "\n\tNum Gestures Labeled as %s: %d", szGestureName, nNumTrainingGestures );
printf( "\n\tRatio %s to all other gestures: 1.0 : %f", szGestureName, fRatioPosNegGestures );
}
return hResult;
}
HRESULT TrainGestures( CHAR* const* szFiles,
const UINT uiFileCount,
const CHAR* szOutputFileName,
const DOUBLE fAccuracyLevel,
const UINT nNumWeakClassifiersAtRuntime,
const FLOAT fWeightOfFalsePositivesWhenFiltering,
const UINT uCPUAvailableForTraining,
BOOL useSkeleton,
BOOL useOpticalFlow,
UINT framesToSkip )
{
DWORD dwStart = GetTickCount();
GestureDetectorTrainer gestureDetectorTrainer;
if ( FAILED( gestureDetectorTrainer.Train( szFiles, uiFileCount, fAccuracyLevel, nNumWeakClassifiersAtRuntime
, fWeightOfFalsePositivesWhenFiltering, FALSE
, uCPUAvailableForTraining, useSkeleton, useOpticalFlow, framesToSkip ) ) )
{
// Delete any old file so that we don't accidentally use stale data
std::remove( szOutputFileName );
printf( "\nERROR: Failed to train gesture detector\n" );
return E_FAIL;
}
if ( FAILED( gestureDetectorTrainer.Save( szOutputFileName ) ) )
{
printf( "\nERROR: Failed to save training data from gesture detector\n" );
return E_FAIL;
}
DWORD dwStop = GetTickCount();
DWORD dwSeconds = ( (dwStop - dwStart ) / 1000 ) % 60;
DWORD dwMinutes = ( (dwStop - dwStart ) / 1000) / 60;
printf( "\nTotal Duration: %d minutes, %d seconds", dwMinutes, dwSeconds );
printf( "\n\nTop 10 contributing weak classifiers:");
DebugOutput output;
for ( UINT i = 0; i < 10; i++ )
{
printf( "\n\t%s", output.Print( (GestureDetector*)&gestureDetectorTrainer, i ) );
}
printf( "\n\nStats:");
printf( "\n\tDetection threshold: %f", gestureDetectorTrainer.GetDetectionThreshold());
printf( "\n\tPositive detection mean: %f, std.dev: %f"
, gestureDetectorTrainer.GetMean( KinectGesture::StrongClassifier::eLabel_Correct )
, gestureDetectorTrainer.GetStdDev( KinectGesture::StrongClassifier::eLabel_Correct ));
printf( "\n\tNegative detection mean: %f, std.dev: %f"
, gestureDetectorTrainer.GetMean( KinectGesture::StrongClassifier::eLabel_Incorrect )
, gestureDetectorTrainer.GetStdDev( KinectGesture::StrongClassifier::eLabel_Incorrect ));
BOOL bTest = true;
if (bTest)
{
gestureDetectorTrainer.Test("", true);
}
return S_OK;
}
}
#endif