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

📄 boxfieldrecognizer.cpp

📁 An open source handwriting recongnition package!!!
💻 CPP
📖 第 1 页 / 共 3 页
字号:
* ARGUMENTS		: rc - The recognition context for the current recognition
* RETURNS		: SUCCESS/FAILURE
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of
*************************************************************************************/
int BoxedFieldRecognizer::recognize (LTKRecognitionContext& rc)
{
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::recognize"
	    <<endl;

	string 	tempStr = REC_UNIT_INFO;  //temp string required to pass the arguments to set/get Flags

	int tempFlagValue = 0; //temp int to hold flag values

	int errorCode = 0;

	vector <LTKWordRecoResult>::iterator resultIter,resultEnd;  //iterates through decoded recognition results

	int numWordRecoResults ; //Number of results required by the application

	int resultIndex ; //index to iterate through the results

	vector<unsigned short> resultString; //result

	float normConf; //normalized confidence

	//Returning FAILURE if the recognition context
	//is not segmented into characters

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

		LTKReturnError(errorCode);
	}

	if( tempFlagValue != REC_UNIT_CHAR)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< EINVALID_SEGMENT <<":"<< getErrorMessage(EINVALID_SEGMENT)
            <<" BoxedFieldRecognizer::recognize" <<endl;

		LTKReturnError(EINVALID_SEGMENT);
	}

	tempStr =REC_MODE;

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

		LTKReturnError(errorCode);
	}

	if(tempFlagValue == REC_MODE_BATCH)
	{
		//clear all the recognizer state
		clearRecognizerState();
		recognizeTraces(rc);
	}
	else if (tempFlagValue == REC_MODE_STREAMING)
	{
		recognizeTraces(rc);
	}
	else
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
		    <<"Error : "<< EINVALID_REC_MODE <<":"<< getErrorMessage(EINVALID_REC_MODE)
            <<" BoxedFieldRecognizer::recognize" <<endl;

		LTKReturnError(EINVALID_REC_MODE);
	}

	/* Now all the recognized results are in
	 * m_decodedResults.
	 */

	resultEnd =	m_decodedResults.end();

	for(resultIter = m_decodedResults.begin(); resultIter != resultEnd ; ++resultIter)
	{
		normConf = (*resultIter).getResultConfidence();

		normConf /= ((*resultIter).getResultWord()).size();

		(*resultIter).setResultConfidence(normConf);

	}

	//number of requested results
	numWordRecoResults =  rc.getNumResults();

	//checking with existing recognition results' size
	if(numWordRecoResults > m_decodedResults.size())
	{
		LOG(LTKLogger::LTK_LOGLEVEL_INFO)
			<< "Don't have enough results to populate. Num of results available = "
			<< m_decodedResults.size() <<", however, results asked for ="
			<< numWordRecoResults <<endl;
	}

	resultEnd = m_decodedResults.end();
	for(resultIndex =0,resultIter = m_decodedResults.begin();(resultIndex <numWordRecoResults)&&(resultIter != resultEnd); ++resultIndex,++resultIter)
	{
		//map the shape ids to unicode IDs
		if((errorCode = LTKStrEncoding::shapeStrToUnicode(m_boxedShapeProject,(*resultIter).getResultWord(),resultString)) != SUCCESS)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            	<<"Error: BoxedFieldRecognizer::recognize"<<endl;

			return LTKReturnError(errorCode);
		}

		//adding the recognition result to recognition context
		 rc.addRecognitionResult(LTKWordRecoResult(resultString,
												   (*resultIter).getResultConfidence()));

		resultString.clear();

	}

	//clearing state of the recognizer
	clearRecognizerState();


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

	return SUCCESS;
}

/**********************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: recognize
* DESCRIPTION	: This method reset the recognizer
* ARGUMENTS		: resetParam - This parameter could specify what to reset
* RETURNS		: SUCCESS/FAILURE
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of 
*************************************************************************************/
 int BoxedFieldRecognizer::reset (int resetParam)
{
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::reset"<<endl;

	clearRecognizerState();

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

	return SUCCESS;
}

/**********************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: recognize
* DESCRIPTION	: This method reset the recognizer
* ARGUMENTS		: resetParam - This parameter could specify what to reset
* RETURNS		: SUCCESS/FAILURE
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of
*************************************************************************************/
 int BoxedFieldRecognizer::unloadModelData()
{

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

	 //clear the recognition state
	clearRecognizerState();

	int errorCode=FAILURE;

	//unload the model data and
	//delete the shape recognizer
	if( m_shapeRecognizer  && (m_module_deleteShapeRecognizer != NULL) )
	{

	   if((errorCode = m_shapeRecognizer->unloadModelData()) != SUCCESS)
	   {
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            	<<"Error: BoxedFieldRecognizer::unloadModelData"<<endl;


		   LTKReturnError(errorCode);
	   }

	   if((errorCode = m_module_deleteShapeRecognizer(&m_shapeRecognizer)) != SUCCESS)
	   {
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            	<<"Error: BoxedFieldRecognizer::unloadModelData"<<endl;


		   LTKReturnError(errorCode);
	   }

	   m_shapeRecognizer = NULL;
	}

	//Freeing the shape recognition library
	if(m_hAlgoDLLHandle)
	{
		m_OSUtilPtr->unloadSharedLib(m_hAlgoDLLHandle);
		m_hAlgoDLLHandle = NULL;
	}

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

	return SUCCESS;
}
/**********************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: ~BoxedFieldRecognizer
* DESCRIPTION	: destructor
* ARGUMENTS		:
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of
*************************************************************************************/

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

	//unload the model data

	int errorCode = FAILURE;
	if((errorCode = unloadModelData()) != SUCCESS)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)
        	<<"Error: BoxedFieldRecognizer::~BoxedFieldRecognizer"<<endl;

		throw LTKException(errorCode);
	}

    delete m_OSUtilPtr;

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


}


//PRIVATE FUNCTIONS

/**********************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: clearRecognizerState
* DESCRIPTION	: Erase the state information of the recognizer
* ARGUMENTS		:
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of
*************************************************************************************/

void BoxedFieldRecognizer::clearRecognizerState()
{
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::clearRecognizerState"
	    <<endl;

	m_numCharsProcessed = 0;       //initializing number of characters processed
	m_numTracesProcessed = 0;      //initializing number of traces processed
	m_decodedResults.clear();      //clearing all the partially decoded results
	m_boxedChar = LTKTraceGroup(); //assigning a new empty LTKTraceGroup

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

}

/**********************************************************************************
* AUTHOR		: Deepu V.
* DATE			: 22-AUG-2005
* NAME			: recognizeTraces
* DESCRIPTION	: performs the recognition of the new strokes added to rc
*               : pre condition - markers are present in this vector
*               :               - m_numTracesProcessed  and m_numCharsProcessed
*               :                 set to proper value
* ARGUMENTS		: rc - The recognitino context
* RETURNS		:
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of
*************************************************************************************/
int BoxedFieldRecognizer::recognizeTraces(LTKRecognitionContext& rc )
{
	LOG(LTKLogger::LTK_LOGLEVEL_DEBUG)
	    <<"Entering: BoxedFieldRecognizer::recognizeTraces"
	    <<endl;

	vector<LTKTrace>::const_iterator traceIter,traceEnd,traceBegin;
	                 //iterator for the traces

	int errorCode = FAILURE;

	int recUnit;     //unit for recognition (should be char)

	LTKTraceGroup emptyChar;
	                 //TraceGroup object that buffers
	                 //all ink corresponding to a character

	vector<int> subSet;
	                 //passing a null arguement for shape subset

	vector<LTKShapeRecoResult> shapeRecoResults;
	                 //The object to hold the output from shape recognizer

	LTKScreenContext screenContext = rc.getScreenContext();
	                 //retrieving the screen context

	LTKCaptureDevice captureDevice = rc.getDeviceContext();
	                 //retrieving the device context

	const LTKTraceVector & traces = rc.getAllInk();
	                //retrieving the traces from recognition context

	string tempStr;  //temporary string object


	if(m_shapeRecognizer == NULL)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Shape recognizer not initialized" <<endl;

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

		LTKReturnError(ENULL_POINTER);

	}
	else if( (errorCode = m_shapeRecognizer->setDeviceContext(captureDevice)) != SUCCESS)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Unable to set device context in shape rec : " << getErrorMessage(errorCode) <<endl;

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

		LTKReturnError(errorCode);
	}

	shapeRecoResults.reserve(m_numShapeRecoResults+1);//reserving memory


	if(m_numTracesProcessed > traces.size())
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Invalid number of traces processed. "
		    << "Traces processed = " << m_numTracesProcessed
            <<  " > total number of traces" << traces.size() <<endl;

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

		LTKReturnError(EINVALID_NUM_OF_TRACES);
	}
	//Start processing from the number of traces processed.
	traceBegin = traces.begin() + m_numTracesProcessed;
	traceEnd = traces.end();int r=0;

	for(traceIter = traceBegin; traceIter != traceEnd; ++traceIter)
	{
		/* Marker strokes are inserted to detect
		 * end of segment.  The marker strokes are
		 * identified by 9number of channels == 0)
         */
		if((*traceIter).getNumberOfPoints() == 0)
		{
			tempStr = REC_UNIT_INFO;
			if((errorCode = rc.getFlag(tempStr,recUnit)) != SUCCESS)
			{
				LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            		<<"Error: BoxedFieldRecognizer::recognizeTraces"<<endl;

				LTKReturnError(errorCode);
			}
			switch(recUnit)
			{

			/* The segment is character
			 * This algorithm recognizes
			 * only character segments
			 */
			case REC_UNIT_CHAR:
				shapeRecoResults.clear();
				//calling the shape recognizer's recognize method.

				if(m_boxedChar.getNumTraces() == 0)
				{
					LTKShapeRecoResult T;
					T.setShapeId(SHRT_MAX);
					T.setConfidence(1.0);
					shapeRecoResults.push_back(T);
				}
				else if( (errorCode =	m_shapeRecognizer->recognize(m_boxedChar,screenContext,subSet,
					m_shapeRecoMinConfidence, m_numShapeRecoResults, shapeRecoResults ))!= SUCCESS )
				{
					LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Shape recognition failed : " << getErrorMessage(errorCode) <<endl;

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

					LTKReturnError(errorCode);
				}


				//This updates the recognition results using
				//current shape recognition results

				if((errorCode = updateRecognitionResults(shapeRecoResults,rc)) != SUCCESS)
				{
					LOG(LTKLogger::LTK_LOGLEVEL_ERR)
            			<<"Error: BoxedFieldRecognizer::recognizeTraces"<<endl;

					LTKReturnError(errorCode);
				}

				for(r=0;r<shapeRecoResults.size();++r)
				{
					LTKShapeRecoResult& tempResult=shapeRecoResults[r];

				}

⌨️ 快捷键说明

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