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

📄 boxfieldrecognizer.cpp

📁 An open source handwriting recongnition package!!!
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************************
* 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 US E OR OTHER DEALINGS IN THE SOFTWARE.
*****************************************************************************************/

/************************************************************************
 * SVN MACROS
 *
 * $LastChangedDate: 2008-08-06 14:43:59 +0530 (Wed, 06 Aug 2008) $
 * $Revision: 596 $
 * $Author: sharmnid $
 *
 ************************************************************************/
/************************************************************************
 * FILE DESCR: Implementation of BoxedFieldRecognizer
 * CONTENTS:
 *
 * AUTHOR:     Deepu V.
 *
 * DATE:       March 23, 2005
 * CHANGE HISTORY:
 * Author		Date			Description of 
 ************************************************************************/

#include "BoxFieldRecognizer.h"
#include "LTKLoggerUtil.h"
#include "LTKTrace.h"
#include "LTKInc.h"
#include "LTKTypes.h"
#include "LTKErrors.h"
#include "LTKErrorsList.h"
#include "LTKTraceGroup.h"
#include "LTKWordRecoConfig.h"

#include "LTKShapeRecognizer.h"
#include "LTKRecognitionContext.h"

#include "LTKScreenContext.h"
#include "LTKCaptureDevice.h"
#include "LTKConfigFileReader.h"
#include "LTKMacros.h"
#include "LTKStrEncoding.h"
#include "LTKException.h"
#include "LTKOSUtilFactory.h"
#include "LTKOSUtil.h"


extern void *m_hAlgoDLLHandle;



/*****************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: initializeWordRecognizer
* DESCRIPTION	: Initialization of Boxed word Recognizer. This function performs
*                      -Create & initialize shape recognizer
* ARGUMENTS		:
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of
*****************************************************************************/
BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo& controlInfo)
:LTKWordRecognizer(BOXFLD),
m_shapeRecognizer(NULL),
m_numShapeRecoResults(DEFAULT_SHAPE_RECO_CHOICES),
m_shapeRecoMinConfidence(DEFAULT_SHAPE_RECO_MIN_CONFID),
m_module_createShapeRecognizer(NULL),
m_module_deleteShapeRecognizer(NULL),
m_numCharsProcessed(0),
m_numTracesProcessed(0),
m_boxedShapeProject(""),
m_boxedShapeProfile(""),
m_lipiRoot(""),
m_boxedConfigFile(""),
m_logFile(""),
m_logLevel(LTKLogger::LTK_LOGLEVEL_ERR),
m_toolkitVersion(""),
m_OSUtilPtr(LTKOSUtilFactory::getInstance())
{

	string boxedShapeProfile;   //profile name

	int errorCode = 0;

	LTKControlInfo tempControlInfo = controlInfo;

    if ( tempControlInfo.lipiRoot.empty() )
    {
        throw LTKException(ELIPI_ROOT_PATH_NOT_SET);
    }

    if ( tempControlInfo.projectName.empty() )
    {
        throw LTKException(EINVALID_PROJECT_NAME);
    }

    if( (tempControlInfo.profileName).empty() )
	{
		tempControlInfo.profileName = DEFAULT_PROFILE;
	}

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

    // initialize the data members
    m_lipiRoot          = tempControlInfo.lipiRoot;
    m_toolkitVersion    = tempControlInfo.toolkitVersion;


	//constructing the boxed Config filename
	m_boxedConfigFile  = m_lipiRoot + PROJECTS_PATH_STRING +
		                 tempControlInfo.projectName + PROFILE_PATH_STRING +
		                 tempControlInfo.profileName + SEPARATOR + BOXFLD + CONFIGFILEEXT;

	readClassifierConfig();


	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo& )"
	    <<endl;

	//Creating the shape recognizer object
	if((errorCode = createShapeRecognizer(m_boxedShapeProject, m_boxedShapeProfile,&m_shapeRecognizer)) != SUCCESS)
	{
		 LOG(LTKLogger::LTK_LOGLEVEL_ERR)
        <<"Error: BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo&)"<<endl;

	   throw LTKException(errorCode);
	}

	if(m_shapeRecognizer == NULL)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< ECREATE_SHAPEREC <<":"<< getErrorMessage(ECREATE_SHAPEREC)
            <<" BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo&)" <<endl;

		throw LTKException(ECREATE_SHAPEREC);
	}

	//loading the model data file
	if( (errorCode = (m_shapeRecognizer->loadModelData())) != SUCCESS )
	{
		m_module_deleteShapeRecognizer(&m_shapeRecognizer);
		m_shapeRecognizer = NULL;

		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
        	<<"Error: BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo&)"<<endl;

		throw LTKException(errorCode);
	}

	m_numCharsProcessed = 0;  //initializing number of characters processed
	m_numTracesProcessed = 0; //initializing number of traces processed


	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Exiting: BoxedFieldRecognizer::BoxedFieldRecognizer(const LTKControlInfo&)"
	    <<endl;
}

/******************************************************************************
 * AUTHOR		: Nidhi Sharma
 * DATE			: 28-Sept-2007
 * NAME			: readClassifierConfig
 * DESCRIPTION	: Reads the boxfld.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 BoxedFieldRecognizer::readClassifierConfig()
{

	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::readClassifierConfig"
	    <<endl;

    LTKConfigFileReader* boxedFldConfigMap = NULL;
    string cfgFileValue = "";
	int errorCode = FAILURE;

    try
	{
		boxedFldConfigMap = new LTKConfigFileReader(m_boxedConfigFile);
	}
	catch(LTKException fofe)
	{
		delete boxedFldConfigMap;

		 LOG(LTKLogger::LTK_LOGLEVEL_ERR)
        <<"Error: BoxedFieldRecognizer::readClassifierConfig"<<endl;

		LTKReturnError(ECONFIG_FILE_OPEN);	// Error while reading project.cfg
	}

	//initializing the number of shape recognition choices required from the file

    errorCode = boxedFldConfigMap->getConfigValue(NUMSHAPECHOICES, cfgFileValue);

    if ( errorCode == SUCCESS )
    {
	    m_numShapeRecoResults = atoi(cfgFileValue.c_str());

		if(m_numShapeRecoResults <= 0)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
			    <<"Error : "<< ENON_POSITIVE_NUM <<":"<< getErrorMessage(ENON_POSITIVE_NUM)
                <<" BoxedFieldRecognizer::readClassifierConfig" <<endl;

			LTKReturnError(ENON_POSITIVE_NUM);
		}

    }
	else
	{
		LOG(LTKLogger::LTK_LOGLEVEL_INFO)
			<<"Assuming default value for number of shape recognizer choices:"
			<<m_numShapeRecoResults<<endl;
	}



	//initializing the minimum confidence threshold

    cfgFileValue = "";
    errorCode = boxedFldConfigMap->getConfigValue(MINSHAPECONFID, cfgFileValue);

    if ( errorCode == SUCCESS )
    {
    	m_shapeRecoMinConfidence = atof(cfgFileValue.c_str());

		if(m_shapeRecoMinConfidence < 0 || m_shapeRecoMinConfidence > 1)
		{

			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
			    <<"Error : "<< EINVALID_CONFIDENCE_VALUE <<":"<< getErrorMessage(EINVALID_CONFIDENCE_VALUE)
                <<" BoxedFieldRecognizer::readClassifierConfig" <<endl;


			LTKReturnError(EINVALID_CONFIDENCE_VALUE);
		}
    }
	else
	{
			LOG(LTKLogger::LTK_LOGLEVEL_INFO)
			<<"Assuming default value for minimum shape recognizer confidence:"
			<<m_shapeRecoMinConfidence<<endl;

	}

	//retrieving the boxed shape project and profile
	cfgFileValue = "";
    errorCode = boxedFldConfigMap->getConfigValue(BOXEDSHAPEPROJECT, cfgFileValue);

    if ( errorCode == SUCCESS )
    {
    	m_boxedShapeProject = cfgFileValue;

		if(m_boxedShapeProject.empty())
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
			    <<"Error : "<< EINVALID_PROJECT_NAME <<":"<< getErrorMessage(EINVALID_PROJECT_NAME)
			    <<" BoxedFieldRecognizer::readClassifierConfig" <<endl;

			LTKReturnError(EINVALID_PROJECT_NAME);
		}
    }
	else
	{
         LOG(LTKLogger::LTK_LOGLEVEL_ERR)
             <<"Error : "<< ENO_SHAPE_RECO_PROJECT <<":"<< getErrorMessage(ENO_SHAPE_RECO_PROJECT)
             <<" BoxedFieldRecognizer::readClassifierConfig" <<endl;

		LTKReturnError(ENO_SHAPE_RECO_PROJECT);
	}


    //retrieving the boxed shape project and profile
	cfgFileValue = "";
    errorCode = boxedFldConfigMap->getConfigValue(BOXEDSHAPEPROFILE, cfgFileValue);

    if( errorCode == SUCCESS )
    {
    	m_boxedShapeProfile = cfgFileValue;

		if(m_boxedShapeProfile.empty())
		{
			LOG(LTKLogger::LTK_LOGLEVEL_INFO)
				<<"No profile specified for shape recognizer project.Assuming 'default' profile"<<endl;

			m_boxedShapeProfile = DEFAULT_PROFILE;
		}


    }
	else
	{
		LOG(LTKLogger::LTK_LOGLEVEL_INFO)
			<<"No profile specified for shape recognizer project. Assuming 'default' profile"<<endl;

		m_boxedShapeProfile = DEFAULT_PROFILE;

	}

    delete boxedFldConfigMap;

    LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
		<<"Exiting: BoxedFieldRecognizer::readClassifierConfig"
	    <<endl;

	return SUCCESS;
}

/*****************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: processInk
* DESCRIPTION	: This method is called from recognition context whenever new traces
*               : are added to it.  The Recognizer need to process the new traces
*               : in this methods and updates the internal state.
* ARGUMENTS		: rc - The recognition context for the current recognition
* RETURNS		: SUCCESS/FAILURE
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of
******************************************************************************/
int BoxedFieldRecognizer::processInk (LTKRecognitionContext& rc)
{
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::processInk"
	    <<endl;

	string tempStr = REC_UNIT_INFO;

	int tempFlagValue=0;

	int errorCode=0;

	if((errorCode=rc.getFlag(tempStr,tempFlagValue))!=SUCCESS)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            <<"Error: BoxedFieldRecognizer::processInk"<<endl;

		LTKReturnError(errorCode);
	}

	//give an error if the Ink is not segmented into characters
	if(tempFlagValue != REC_UNIT_CHAR)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< EINVALID_SEGMENT <<":"<< getErrorMessage(EINVALID_SEGMENT)
            <<" BoxedFieldRecognizer::processInk" <<endl;

		LTKReturnError(EINVALID_SEGMENT);
	}

	tempStr = REC_MODE;


	if((errorCode=rc.getFlag(tempStr,tempFlagValue))!=SUCCESS)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            <<"Error: BoxedFieldRecognizer::processInk"<<endl;

		LTKReturnError(errorCode);
	}

	//if the recognizer mode is correct
	if (tempFlagValue == REC_MODE_STREAMING)
	{
		//recognize the newly added strokes
		recognizeTraces(rc);
	}
	else
	{
		//give an error otherwise
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
			<<"Error : "<< EINVALID_REC_MODE <<":"<< getErrorMessage(EINVALID_REC_MODE)
            <<" BoxedFieldRecognizer::processInk" <<endl;


		LTKReturnError(EINVALID_REC_MODE);
	}

	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
		<<"Exiting: BoxedFieldRecognizer::processInk"
	    <<endl;

	return SUCCESS;
}

/**********************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: endRecoUnit
* DESCRIPTION	: This function notifies the recognizer that end of current ink is
*               : the end of a logic segment.  This information could be used in
*               : constraining the recognizer choices
* ARGUMENTS		:
* RETURNS		: SUCCESS/FAILURE
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of 
*************************************************************************************/
void BoxedFieldRecognizer::endRecoUnit ()
{
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::endRecoUnit"<<endl;

	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
		<<"Exiting: BoxedFieldRecognizer::endRecoUnit"
	    <<endl;

}

/**********************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: recognize
* DESCRIPTION	: This is the recognize call
*               : The results of the recognition is set on the Recognition context
*               : object.  In case of BATCH_MODE recognition recognition of every
*               : character is performed.  otherwise the recognizer updates the outputs
*               : with the recognized results

⌨️ 快捷键说明

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