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

📄 boxfieldrecognizer.cpp

📁 An open source handwriting recongnition package!!!
💻 CPP
📖 第 1 页 / 共 3 页
字号:

				m_boxedChar = emptyChar;//making the trace group empty again
				break;

			default:
				LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Unsupported reccognizer mode by Box Field" <<endl;

				LOG(LTKLogger::LTK_LOGLEVEL_ERR)
				    <<"Error : "<< EINVALID_RECOGNITION_MODE <<":"<< getErrorMessage(EINVALID_RECOGNITION_MODE)
             		<<" BoxedFieldRecognizer::recognizeTraces" <<endl;

				LTKReturnError(EINVALID_RECOGNITION_MODE);

			}
			++m_numCharsProcessed;      //incrementing number of characters processed
		}
		else
		{
			m_boxedChar.addTrace(*traceIter); //buffering the trace to the temp TraceGroup for recognition
		}
		++m_numTracesProcessed;         //incrementing the number of traces processed
	}


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

	return SUCCESS;
}

/**********************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: updateRecognitionResults
* DESCRIPTION	: This function tries to update the
*               : shape recognition choices with new shape recognition results
* ARGUMENTS		: results - new results for updating the results
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of
*************************************************************************************/
int BoxedFieldRecognizer::updateRecognitionResults(const vector<LTKShapeRecoResult>& results, LTKRecognitionContext& rc)
{
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::updateRecognitionResults"
	    <<endl;

	multimap< float, pair<int,int>, greater<float> >backTrace;
	                   //A multi map is used for finding best N paths
	multimap< float, pair<int,int>, greater<float> >::iterator iter, iterend;
	                   //Iterator for accessing elements of the map
	pair<int,int> combination;
	                   //Temporary variable that keeps a (int,int) pair
	int wordResultIndex, shapeResultIndex;
	                   //loop index variables
	float wordConfidence, shapeConfidence;
	                   //word level and shape level confidences
	unsigned short newSymbol;
	                   //temporary storage for shape recognizer id
	float newConf;     //temporary storage for shape recognizer confidence

	vector<LTKWordRecoResult> newResultVector;
	                   //new results after finding the best N paths

	int numWordRecoResults = rc.getNumResults();
	                   //number of word recognition results requested
	int numShapeRecoResults = results.size();
	                   //number of choices from the shape recognizer
        vector<unsigned short>initVec;
                           //for initializing the trellis



	//If there is no decoded results (First shape recognition in the word)
    if(m_decodedResults.empty())
	{
		//Initializing the results vector
		m_decodedResults.assign(numShapeRecoResults,LTKWordRecoResult());

		//iterating through different word recognition choices
 		for(wordResultIndex = 0; (wordResultIndex<numShapeRecoResults); ++wordResultIndex)
		{
				//Retrieving the shape recognition choices

				newSymbol = results.at(wordResultIndex).getShapeId();
				newConf   = results.at(wordResultIndex).getConfidence();

				//updating the results

				initVec.assign(1,newSymbol);
				m_decodedResults.at(wordResultIndex).setWordRecoResult(initVec,newConf);

		}
	}

    else
	{
		//initializing a temporary result vector
		//newResultVector.assign(smallerResultNumber,LTKWordRecoResult());

		//iterating through each word recognition result
		for(wordResultIndex=0; wordResultIndex<m_decodedResults.size(); ++wordResultIndex)
		{
			wordConfidence = (m_decodedResults.at(wordResultIndex)).getResultConfidence();

			//iterating through each shape recognition results
			for(shapeResultIndex =0; shapeResultIndex<numShapeRecoResults; ++shapeResultIndex )
			{
				//adding total confidence to the map. so that later they
				//can be retrieved in the sorted order
				shapeConfidence = (results.at(shapeResultIndex)).getConfidence();
				backTrace.insert( pair<float, pair<int,int> >( (shapeConfidence+wordConfidence),
					pair<int,int>(wordResultIndex,shapeResultIndex)));
			}
		}

		iterend = backTrace.end();

		//iterating through the map to retrieve the largest confidences.
		for(wordResultIndex = 0,iter = backTrace.begin(); (wordResultIndex<numWordRecoResults)&&(iter!= iterend); ++wordResultIndex,++iter)
		{

			//confidence
			wordConfidence = (*iter).first;

			//the combination that gave this
			//confidence
			combination = (*iter).second;

			//copying the word reco result corresponding to
			//the combination to new result vector
			//newResultVector.at(wordResultIndex) = m_decodedResults.at(combination.first);
			LTKWordRecoResult tempWordRecoResult = m_decodedResults.at(combination.first);

			//retrieving the shape recognition result id
			//and confidence corresponding to the combination
			newSymbol = results.at(combination.second).getShapeId();
			newConf   = results.at(combination.second).getConfidence();

			//updating the word reco result with new id and confidence
			//newResultVector.at(wordResultIndex).updateWordRecoResult(newSymbol, newConf);

				tempWordRecoResult.updateWordRecoResult(newSymbol,newConf);
				newResultVector.push_back(tempWordRecoResult);
		}

		//assigning the newly created result vector
		m_decodedResults = newResultVector;
	}


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


	return SUCCESS;
}


/**********************************************************************************
* AUTHOR        : Thanigai
* DATE          : 29-JUL-2005
* NAME          : createShapeRecognizer
* DESCRIPTION   : create an instance of shape recognizer object and call initialize
*				  function. Also loads the model data.
* ARGUMENTS     : strProjectName - project name; strProfileName - profile name
* RETURNS       : handle to the recognizer on success & NULL on error
* NOTES         :
* CHANGE HISTROY
* Author            Date                Description of
*************************************************************************************/
 int BoxedFieldRecognizer::createShapeRecognizer(const string& strProjectName, const string& strProfileName,LTKShapeRecognizer** outShapeRecPtr)
{
    LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
        <<"Entering: BoxedFieldRecognizer::createShapeRecognizer"
        <<endl;


    LTKConfigFileReader* projectCfgFileEntries = NULL;
    LTKConfigFileReader* profileCfgFileEntries = NULL;

	string cfgFilePath      = "";
	string shapeRecDllPath  = "";
	int iResult             = 0;
    string recognizerName   = "";
    string strLocalProfileName(strProfileName);


	/* invalid or no entry for project name */
	if(strProjectName == "")
	{

		*outShapeRecPtr = NULL;

		LOG( LTKLogger::LTK_LOGLEVEL_ERR)
            <<"Invalid or no entry for project name" <<endl;

        LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< EINVALID_PROJECT_NAME <<":"<< getErrorMessage(EINVALID_PROJECT_NAME)
            <<" BoxedFieldRecognizer::createShapeRecognizer" <<endl;

		LTKReturnError(EINVALID_PROJECT_NAME);
	}

	if(strProfileName == "")
	{
		strLocalProfileName = DEFAULT_PROFILE; /* assume the "default" profile */
	}

	cfgFilePath = m_lipiRoot + PROJECTS_PATH_STRING + strProjectName +
                  PROFILE_PATH_STRING + PROJECT_CFG_STRING;

	try
	{
		projectCfgFileEntries = new LTKConfigFileReader(cfgFilePath);
	}
	catch(LTKException e)
	{
		delete projectCfgFileEntries;

		*outShapeRecPtr = NULL; // Error exception thrown...

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

		LTKReturnError(e.getErrorCode());


	}

	//	Read the project.cfg and ensure this is a shaperecognizer; i.e. ProjectType = SHAPEREC;
	string projectType = "";
	projectCfgFileEntries->getConfigValue(PROJECT_TYPE_STRING, projectType);

	/* Invalid configuration entry for ProjectType */
	if(projectType != PROJECT_TYPE_SHAPEREC)
	{
		*outShapeRecPtr = NULL;

		errorCode = EINVALID_CONFIG_ENTRY;

		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< EINVALID_CONFIG_ENTRY <<":"<< getErrorMessage(EINVALID_CONFIG_ENTRY)
            <<" BoxedFieldRecognizer::createShapeRecognizer" <<endl;

		LTKReturnError(errorCode);

	}

	//	Read the profile.cfg and find out the recognition module to load;
	cfgFilePath = m_lipiRoot + PROJECTS_PATH_STRING + strProjectName +
	              PROFILE_PATH_STRING + strLocalProfileName +
	              SEPARATOR + PROFILE_CFG_STRING;
	try
	{
		profileCfgFileEntries = new LTKConfigFileReader(cfgFilePath);
	}
	catch(LTKException e)
	{
		*outShapeRecPtr = NULL;

		delete profileCfgFileEntries;

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

		LTKReturnError(e.getErrorCode());

	}

	int errorCode = profileCfgFileEntries->getConfigValue(SHAPE_RECOGNIZER_STRING, recognizerName);

	/* No recognizer specified. */
	if(errorCode != SUCCESS)
	{

		*outShapeRecPtr = NULL;

		errorCode = ENO_SHAPE_RECOGNIZER;

		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< ENO_SHAPE_RECOGNIZER <<":"<< getErrorMessage(ENO_SHAPE_RECOGNIZER)
            <<" BoxedFieldRecognizer::createShapeRecognizer" <<endl;

        delete projectCfgFileEntries;
    	delete profileCfgFileEntries;

		LTKReturnError(errorCode);
	}

    m_hAlgoDLLHandle = NULL;
    errorCode = m_OSUtilPtr->loadSharedLib(m_lipiRoot, recognizerName, &m_hAlgoDLLHandle);

	// Unable to load dll
	if(errorCode != SUCCESS)
	{
		*outShapeRecPtr = NULL;

		errorCode = ELOAD_SHAPEREC_DLL;

		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< ELOAD_SHAPEREC_DLL <<":"<< getErrorMessage(ELOAD_SHAPEREC_DLL)
            <<" BoxedFieldRecognizer::createShapeRecognizer" <<endl;

        delete projectCfgFileEntries;
    	delete profileCfgFileEntries;

		LTKReturnError(errorCode);

	}

	// Map Algo DLL functions...

	// Unable to map the functions
	if((errorCode = mapShapeAlgoModuleFunctions()) != SUCCESS)
	{
		*outShapeRecPtr = NULL;
	    delete projectCfgFileEntries;
    	delete profileCfgFileEntries;

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

		LTKReturnError(errorCode)
	}

    // Construct LTKControlInfo object
    LTKControlInfo controlInfo;
    controlInfo.projectName     = strProjectName;
    controlInfo.profileName     = strLocalProfileName;
    controlInfo.lipiRoot        = m_lipiRoot;
    controlInfo.toolkitVersion  = m_toolkitVersion;

	*outShapeRecPtr = NULL;

	/* Error, unable to create shape recognizer instance */
	if((errorCode = m_module_createShapeRecognizer(controlInfo,outShapeRecPtr)) != SUCCESS)
	{

		*outShapeRecPtr = NULL;

		delete projectCfgFileEntries;
    	delete profileCfgFileEntries;

		errorCode = ECREATE_SHAPEREC;

		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< ECREATE_SHAPEREC <<":"<< getErrorMessage(ECREATE_SHAPEREC)
            <<" BoxedFieldRecognizer::createShapeRecognizer" <<endl;



		LTKReturnError(errorCode);
	}


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

	delete projectCfgFileEntries;
	delete profileCfgFileEntries;

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

	return SUCCESS;
}

/**********************************************************************************
* AUTHOR        : Thanigai
* DATE          : 29-JUL-2005
* NAME          : mapShapeAlgoModuleFunctions
* DESCRIPTION   : To map function addresses of the methods exposed by
*				  the shape recognition modules
* ARGUMENTS     : None
* RETURNS       : 0 on success and other values on error
* NOTES         :
* CHANGE HISTROY
* Author            Date                Description of
*************************************************************************************/
int BoxedFieldRecognizer::mapShapeAlgoModuleFunctions()
{
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::mapShapeAlgoModuleFunctions"
	    <<endl;

    int returnVal = SUCCESS;
	m_module_createShapeRecognizer = NULL;

    void* functionHandle = NULL;
    returnVal = m_OSUtilPtr->getFunctionAddress(m_hAlgoDLLHandle, 
                                            CREATESHAPERECOGNIZER_FUNC_NAME, 
                                            &functionHandle);
    
	if(returnVal != SUCCESS)
	{
		LOG( LTKLogger::LTK_LOGLEVEL_ERR) <<
            "Exported function not found in module : createShapeRecognizer "<<endl;

        LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< EDLL_FUNC_ADDRESS <<":"<< getErrorMessage(EDLL_FUNC_ADDRESS)
            <<" BoxedFieldRecognizer::mapShapeAlgoModuleFunctions" <<endl;

		LTKReturnError(EDLL_FUNC_ADDRESS);
		// ERROR: Unable to link with createShapeRecognizer function in module */
	}

    m_module_createShapeRecognizer = (FN_PTR_CREATESHAPERECOGNIZER)functionHandle;

    functionHandle = NULL;
    

    // map delete shape recognizer function
    returnVal = m_OSUtilPtr->getFunctionAddress(m_hAlgoDLLHandle, 
                                            DELETESHAPERECOGNIZER_FUNC_NAME, 
                                            &functionHandle);
    
	if(returnVal != SUCCESS)
	{
		LOG( LTKLogger::LTK_LOGLEVEL_ERR) <<
            "Exported function not found in module : deleteShapeRecognizer " << endl;

        LOG(LTKLogger::LTK_LOGLEVEL_ERR)
				    <<"Error : "<< EDLL_FUNC_ADDRESS <<":"<< getErrorMessage(EDLL_FUNC_ADDRESS)
		            <<" BoxedFieldRecognizer::mapShapeAlgoModuleFunctions" <<endl;

		LTKReturnError(EDLL_FUNC_ADDRESS);
	}

    m_module_deleteShapeRecognizer = (FN_PTR_DELETESHAPERECOGNIZER)functionHandle;
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
		<<"Exiting: BoxedFieldRecognizer::mapShapeAlgoModuleFunctions"
		<<endl;

	return SUCCESS;
}



⌨️ 快捷键说明

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