⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nnshaperecognizer.cpp

📁 An open source handwriting recongnition package!!!
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*****************************************************************************************
 * Copyright (c) 2006 Hewlett-Packard Development Company, L.P.
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *****************************************************************************************/

/************************************************************************
 * SVN MACROS
 *
 * $LastChangedDate: 2008-08-12 14:19:20 +0530 (Tue, 12 Aug 2008) $
 * $Revision: 613 $
 * $Author: sharmnid $
 *
 ************************************************************************/
/************************************************************************
 * FILE DESCR: Implementation for NN Shape Recognition module
 *
 * CONTENTS:
 *
 * AUTHOR:     Saravanan R.
 *
 w
 * DATE:       January 23, 2004
 * CHANGE HISTORY:
 * Author       Date            Description of change
 ************************************************************************/

#include "LTKConfigFileReader.h"

#include "NNShapeRecognizer.h"

#include "LTKPreprocDefaults.h"

#include "LTKHierarchicalClustering.h"

#include "LTKPreprocessorInterface.h"

#include "LTKShapeFeatureExtractorFactory.h"

#include "LTKShapeFeatureExtractor.h"

#include "LTKShapeFeature.h"

#include "LTKVersionCompatibilityCheck.h"

#include "LTKInkFileWriter.h"
#include "LTKOSUtil.h"
#include "LTKOSUtilFactory.h"

/**********************************************************************************
 * AUTHOR		: Saravanan R.
 * DATE			: 23-Jan-2007
 * NAME			: NNShapeRecognizer
 * DESCRIPTION	: Default Constructor that initializes all data members
 * ARGUMENTS		: none
 * RETURNS		: none
 * NOTES			:
 * CHANGE HISTROY
 * Author			Date				Description
 *************************************************************************************/

void NNShapeRecognizer::assignDefaultValues()
{
    LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entering " <<
        "NNShapeRecognizer::assignDefaultValues()" << endl;

    m_numShapes = 0;
    m_nnCfgFilePath = "";
    m_nnMDTFilePath = "";
    m_ptrPreproc = NULL;
    m_projectTypeDynamic=false;
    m_prototypeSelection=NN_DEF_PROTOTYPESELECTION;
    m_prototypeReductionFactor=NN_DEF_PROTOTYPEREDUCTIONFACTOR;
    m_prototypeDistance=NN_DEF_PROTOTYPEDISTANCE;
    m_nearestNeighbors=NN_DEF_NEARESTNEIGHBORS;
    m_dtwBanding=NN_DEF_BANDING;
    m_dtwEuclideanFilter=NN_DEF_DTWEUCLIDEANFILTER;
    m_preProcSeqn=NN_DEF_PREPROC_SEQ;
    m_ptrFeatureExtractor=NULL;
    m_featureExtractorName=NN_DEF_FEATURE_EXTRACTOR;
    m_numClusters=NN_NUM_CLUST_INITIAL; // just to check that this is not what is mentioned by the user
    m_MDTUpdateFreq=NN_DEF_MDT_UPDATE_FREQ;
    m_prototypeSetModifyCount=0;
    m_rejectThreshold=NN_DEF_REJECT_THRESHOLD;
    m_adaptivekNN=false;
    m_deleteLTKLipiPreProcessor=NULL;
    m_MDTFileOpenMode = NN_MDT_OPEN_MODE_ASCII;
	m_LVQIterationScale=NN_DEF_LVQITERATIONSCALE;
	m_LVQInitialAlpha=NN_DEF_LVQINITIALALPHA;
	m_LVQDistanceMeasure=NN_DEF_LVQDISTANCEMEASURE;

    LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting " <<
        "NNShapeRecognizer::assignDefaultValues()" << endl;
}

/**********************************************************************************
 * AUTHOR		: Saravanan R.
 * DATE			: 23-Jan-2007
 * NAME			: initialize
 * DESCRIPTION	: This method initializes the NN shape recognizer
 * ARGUMENTS		: string  Holds the Project Name
 *				  string  Holds the Profile Name
 * RETURNS		: integer Holds error value if occurs
 *						  Holds SUCCESS if no erros
 * NOTES			:
 * CHANGE HISTROY
 * Author			Date				Description
 *************************************************************************************/
NNShapeRecognizer::NNShapeRecognizer(const LTKControlInfo& controlInfo):
m_OSUtilPtr(LTKOSUtilFactory::getInstance()),
m_libHandler(NULL),
m_libHandlerFE(NULL)
{
    LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entering " <<
        "NNShapeRecognizer::NNShapeRecognizer()" << endl;

	try
	{
	    LTKControlInfo tmpControlInfo=controlInfo;

	    string strProjectName = "";
	    string strProfileName = "";


	    if( (tmpControlInfo.projectName).empty() )
	    {
	        throw LTKException(EINVALID_PROJECT_NAME);
	    }
	    if( (tmpControlInfo.lipiRoot).empty() )
	    {
	        throw LTKException(ELIPI_ROOT_PATH_NOT_SET);
	    }

	    if( (tmpControlInfo.profileName).empty() )
	    {
	        strProfileName = DEFAULT_PROFILE;
	        tmpControlInfo.profileName = strProfileName;
	    }

	    if ( tmpControlInfo.toolkitVersion.empty() )
	    {
	        throw LTKException(ENO_TOOLKIT_VERSION);
	    }

	    assignDefaultValues();

	    m_lipiRootPath = tmpControlInfo.lipiRoot;
	    m_currentVersion = tmpControlInfo.toolkitVersion;
	    strProjectName = tmpControlInfo.projectName;
	    strProfileName = tmpControlInfo.profileName;


	    //Model Data file Header Information
	    m_headerInfo[PROJNAME] = strProjectName;

	    //Holds the value of Number of Shapes in string format
	    string strNumShapes = "";

	    string strProfileDirectory = m_lipiRootPath + PROJECTS_PATH_STRING +
	        strProjectName + PROFILE_PATH_STRING;

	    //Holds the path of the Project.cfg
	    string projectCFGPath = strProfileDirectory + PROJECT_CFG_STRING;

	    // Config file

	    m_nnCfgFilePath = m_lipiRootPath + PROJECTS_PATH_STRING +
	        (tmpControlInfo.projectName) + PROFILE_PATH_STRING +
	        (tmpControlInfo.profileName) + SEPARATOR +
	        NN + CONFIGFILEEXT;


	    //Set the path for nn.mdt
	    m_nnMDTFilePath = strProfileDirectory + strProfileName + SEPARATOR + NN + DATFILEEXT;


	    //To find whether the project was dynamic or not andto read read number of shapes from project.cfg
	    int errorCode = m_shapeRecUtil.isProjectDynamic(projectCFGPath,
	            m_numShapes, strNumShapes, m_projectTypeDynamic);

	    if( errorCode != SUCCESS)
	    {
	        LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error: " << errorCode << " " <<
	            "NNShapeRecognizer::NNShapeRecognizer()" <<endl;
	        throw LTKException(errorCode);
	    }


	    //Set the NumShapes to the m_headerInfo
	    m_headerInfo[NUMSHAPES] = strNumShapes;

	    //Currently preproc cfg also present in NN
	    tmpControlInfo.cfgFileName = NN;
	    errorCode = initializePreprocessor(tmpControlInfo,
	            &m_ptrPreproc);

	    if( errorCode != SUCCESS)
	    {
	        LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error: " << errorCode << " " <<
	            "NNShapeRecognizer::NNShapeRecognizer()" <<endl;
	        throw LTKException(errorCode);
	    }

	    //Reading NN configuration file
	    errorCode = readClassifierConfig();

	    if( errorCode != SUCCESS)
	    {
			cout<<endl<<"Encountered error in readClassifierConfig"<<endl;
	        LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error: " << errorCode << " " <<
	            "NNShapeRecognizer::NNShapeRecognizer()" <<endl;
	        throw LTKException(errorCode);
	    }

	    //Writing Feature extractor name and version into the header
	    m_headerInfo[FE_NAME] = m_featureExtractorName;
	    m_headerInfo[FE_VER] = SUPPORTED_MIN_VERSION; //FE version

	    //Writting mdt file open mode to the mdt header
	    m_headerInfo[MDT_FOPEN_MODE] = m_MDTFileOpenMode;

	    errorCode = initializeFeatureExtractorInstance(tmpControlInfo);

	    if( errorCode != SUCCESS)
	    {
	        LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error: " << errorCode << " " <<
	            "NNShapeRecognizer::NNShapeRecognizer()" <<endl;
	        throw LTKException(errorCode);
	    }

	}
	catch(LTKException e)
	{
		
		deletePreprocessor();
		m_prototypeSet.clear();

		m_cachedShapeSampleFeatures.clearShapeSampleFeatures();
	    
	    //Unloading the feature Extractor instance
	    deleteFeatureExtractorInstance();

		delete m_OSUtilPtr;
		throw e;
	}
    LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Exiting " <<
        "NNShapeRecognizer::NNShapeRecognizer()" << endl;

}

/**********************************************************************************
 * AUTHOR		: Saravanan R.
 * DATE			: 23-Jan-2007
 * NAME			: readClassifierConfig
 * DESCRIPTION	: Reads the NN.cfg and initializes the data members of the class
 * ARGUMENTS		: none
 * RETURNS		: SUCCESS   - If config file read successfully
 *				  errorCode - If failure
 * NOTES			:
 * CHANGE HISTROY
 * Author			Date				Description
 *************************************************************************************/
int NNShapeRecognizer::readClassifierConfig()
{
    LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entering " <<
        "NNShapeRecognizer::readClassifierConfig()" << endl;
    string tempStringVar = "";
    int tempIntegerVar = 0;
    float tempFloatVar = 0.0;
    LTKConfigFileReader *shapeRecognizerProperties = NULL;
    int errorCode = FAILURE;

    try
    {
        shapeRecognizerProperties = new LTKConfigFileReader(m_nnCfgFilePath);
    }
    catch(LTKException e)
    {
        LOG(LTKLogger::LTK_LOGLEVEL_INFO)<< "Info: " <<
            "Config file not found, using default values of the parameters" <<
            "NNShapeRecognizer::readClassifierConfig()"<<endl;

        delete shapeRecognizerProperties;

		return FAILURE;
    }

    //Pre-processing sequence
    errorCode = shapeRecognizerProperties->getConfigValue(PREPROCSEQUENCE, m_preProcSeqn);

    if(errorCode != SUCCESS)
    {
        LOG(LTKLogger::LTK_LOGLEVEL_INFO) << "Info: " <<
            "Using default value of prerocessing sequence: "<< m_preProcSeqn <<
            " NNShapeRecognizer::readClassifierConfig()"<<endl;

        m_preProcSeqn = NN_DEF_PREPROC_SEQ;
    }
	else
	{
		m_headerInfo[PREPROC_SEQ] = m_preProcSeqn;
	}


#ifdef _INTERNAL

	readInternalClassifierConfig();

#endif

    if((errorCode = mapPreprocFunctions()) != SUCCESS)
    {
        LOG(LTKLogger::LTK_LOGLEVEL_ERR)<<" Error: " << errorCode << 
            " NNShapeRecognizer::readClassifierConfig()"<<endl;

        delete shapeRecognizerProperties;

        LTKReturnError(errorCode);
    }

    tempStringVar = "";
    errorCode = shapeRecognizerProperties->getConfigValue(PROTOTYPESELECTION,
            tempStringVar);

    if (errorCode == SUCCESS)
    {
        if( (LTKSTRCMP(tempStringVar.c_str(), PROTOTYPE_SELECTION_CLUSTERING) == 0) || (LTKSTRCMP(tempStringVar.c_str(), PROTOTYPE_SELECTION_LVQ) == 0))
        {
            m_prototypeSelection = tempStringVar;
            LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) <<
                PROTOTYPESELECTION << " = " << tempStringVar <<
                "NNShapeRecognizer::readClassifierConfig()"<<endl;
        }

        else
        {
            LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<
                "Error: " << ECONFIG_FILE_RANGE << " " <<
                PROTOTYPESELECTION << " : " << tempStringVar
                <<  " method is not supported"  <<
                " NNShapeRecognizer::readClassifierConfig()"<<endl;

            delete shapeRecognizerProperties;

            LTKReturnError(ECONFIG_FILE_RANGE);
        }
    }
    else
    {
	    LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)<<
            "Using default value for " << PROTOTYPESELECTION << " : " << 
            m_prototypeSelection <<
            " NNShapeRecognizer::readClassifierConfig()"<<endl;
    }


    tempStringVar = "";
    errorCode = shapeRecognizerProperties->getConfigValue(PROTOREDFACTOR,
                                                          tempStringVar);

    string tempStringVar1 = "";
    int errorCode1 = shapeRecognizerProperties->getConfigValue(NUMCLUSTERS,
                                                               tempStringVar1);

    if(errorCode1 == SUCCESS && errorCode == SUCCESS)
    {
        LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<
            "Error: " << ECONFIG_FILE_RANGE
            << " Cannot use both config parameters " <<
            PROTOREDFACTOR << " and " << NUMCLUSTERS << " at the same time "<<
            " NNShapeRecognizer::readClassifierConfig()"<<endl;

        delete shapeRecognizerProperties;

        LTKReturnError(ECONFIG_FILE_RANGE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -