Ejemplo n.º 1
0
/**********************************************************************************
* AUTHOR		: Thanigai Murugan K
* DATE			: 30-AUG-2005
* NAME			: evaluateWordRecognizer
* DESCRIPTION	: Load the model data and call recognize function and display the
*				  results. 
* ARGUMENTS		: pReco - handle to LTKWordRecognizer object
* RETURNS		: -1 on error 0 on success
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/
int evaluateWordRecognizer(LTKWordRecognizer *pReco, const string& infilelist)
{
	int iErrorCode = 0;
	string tempStr(REC_UNIT_INFO), tempStr1(REC_MODE);
	string path;
	string strShapeId;

	vector<LTKTraceGroup> fieldInk;
	int charIndex;
	wstring eolstr(L"\r\n");
	int i;
    string strWordId;

	LTKRecognitionContext *recoContext = new LTKRecognitionContext();

	LTKCaptureDevice deviceContext;
	LTKScreenContext screenContext;

	recoContext->setWordRecoEngine(pReco);
	recoContext->setFlag(tempStr,REC_UNIT_CHAR);
	recoContext->setFlag(tempStr1,REC_MODE_STREAMING);

	recoContext->setNumResults(numChoices);

	ifstream in(infilelist.c_str());
	if(in == NULL)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)<< "Test list file open error " << infilelist <<endl;
		cout << "Test list file open error : " << infilelist.c_str() << endl;

		//delete recognition context object
		if(recoContext)
		{
			//ptrObj->deleteRecognitionContext(recoContext);

			delete recoContext;
			recoContext = NULL;
		}
		return FAILURE;
	}

	ofstream resultfile(strOutputFileName,ios::out|ios::binary);

	//a Header of 0xFEFF is required to identify this is a
	//16 bit unicode file
	const unsigned short fHeader = 0xfeff;
	resultfile.write((char*)&fHeader,sizeof(unsigned short));

	while(in)
	{
		//Get the file name
		if(!getline(in,path,' ')) 
		{
			break;
		}
		
		//Get the word ID
		getline(in,strWordId);
		//iWordID = atoi(strShapeId.c_str());

		if(path.length() > 0 && path[0] == COMMENTCHAR )
		{
			continue;
		}

		if(path.length() == 0)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR)<< "Empty File name" <<endl;
			continue;
		}

		try
		{
			fieldInk.clear();

			getAbsolutePath(path,strLipiRootPath);

			cout << path << endl;

			//read the word file
			if(readWordFile(path, fieldInk, deviceContext, screenContext) != SUCCESS)
			{
				LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error reading ink file:" << path << endl;
				cout<<"Error reading ink file:" << path << endl;
				cout<<"Aborted"<<endl;

				//delete recognition context object
				if(recoContext)
				{
					//ptrObj->deleteRecognitionContext(recoContext);

					delete recoContext;
					recoContext = NULL;
				}
				return FAILURE;

			}
			recoContext->setDeviceContext(deviceContext);
			recoContext->setScreenContext(screenContext);

			if(fieldInk.size()==0)
			{

				LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Empty trace group read from:" << path << endl;
				continue;
			}
			
			for(charIndex = 0; charIndex < fieldInk.size(); ++charIndex)
			{
				recoContext->beginRecoUnit();
				recoContext->addTraceGroups(LTKTraceGroupVector(1,fieldInk.at(charIndex)));
				recoContext->endRecoUnit();
				recoContext->beginRecoUnit();
				recoContext->addTraceGroups(LTKTraceGroupVector(1,LTKTraceGroup()));
				recoContext->endRecoUnit();

			}
		}
		catch(LTKException e)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR) << e.getExceptionMessage() <<endl;

			//delete recognition context object
			if(recoContext)
			{
				//ptrObj->deleteRecognitionContext(recoContext);

				delete recoContext;
				recoContext = NULL;
			}

			return FAILURE;
		}

		//Calling recognize and retrieving the top result
		{
			LTKWordRecoResult result;
			vector<LTKWordRecoResult> r2;
			recoContext->recognize();
			recoContext->getTopResult(result);
			recoContext->getNextBestResults(numChoices-1, r2);

			vector<unsigned short> resultVec = result.getResultWord();
			if(!resultVec.empty())
			{
				resultfile.write((char *)&(resultVec.at(0)), resultVec.size()*sizeof(unsigned short));
				resultfile.write((char*)eolstr.c_str(),eolstr.length()*sizeof(unsigned short));

				for(i =0; i<r2.size(); ++i)
				{
					resultVec = r2.at(i).getResultWord();
					resultfile.write((char *)&(resultVec.at(0)), resultVec.size()*sizeof(unsigned short));
					resultfile.write((char*)eolstr.c_str(),eolstr.length()*sizeof(unsigned short));
				}
			}

			recoContext->clearRecognitionResult();
		}

		resultfile.write((char*)eolstr.c_str(),eolstr.length()*sizeof(unsigned short));
	}

	resultfile.close();

	//delete recognition context object
	if(recoContext)
	{
		//ptrObj->deleteRecognitionContext(recoContext);

		delete recoContext;
		recoContext = NULL;
	}

	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;
}
int main(int argc, char** argv)
{
	char *envstring = NULL;
	int iResult;
	string tempStr(REC_UNIT_INFO), tempStr1(REC_MODE);
	string path;
	string strShapeId;
    string strWordId;
	char infilelist[MAX_PATH];
	string outfile("wordrectst.out");
	vector<LTKTraceGroup> fieldInk;
	int charIndex;
	wstring eolstr(L"\r\n");
	int i;
    

	// first argument is the logical project name 
	// second argument is the ink file to recognize
	// third argument is the output file
	if(argc < 4)
	{
		cout << "\nUsage:";
		cout << "\nwordrectst <logical projectname> <list file to recognize> <outputfile>";
		cout << "\nlist of valid <logicalname>s is available in $LIPI_ROOT/projects/lipiengine.cfg file";
		cout << endl;
        delete utilPtr;
		return -1;
	}

	// Get the LIPI_ROOT environment variable 
	envstring = getenv(LIPIROOT_ENV_STRING);
	if(envstring == NULL)
	{
		cout << "\nError, Environment variable is not set LIPI_ROOT\n";
        delete utilPtr;
		return -1;
	}

	// Load the LipiEngine.DLL
	hLipiEngine = NULL;
	iResult = utilPtr->loadSharedLib(envstring, LIPIENGINE_MODULE_STR, &hLipiEngine);

	if(iResult != SUCCESS)
	{
		cout << "Error loading LipiEngine module" << endl;
        delete utilPtr;
		return -1;
	}

	if(MapFunctions() != 0)
	{
		cout << "Error fetching exported functions of the module" << endl;
        delete utilPtr;
		return -1;
	}

	// create an instance of LipiEngine Module
	ptrObj = createLTKLipiEngine();

	// set the LIPI_ROOT path in Lipiengine module instance
	ptrObj->setLipiRootPath(envstring);

	// Initialize the LipiEngine module
	iResult = ptrObj->initializeLipiEngine();
	if(iResult != SUCCESS)
	{
		cout << iResult << ": Error initializing LipiEngine.\n";
		utilPtr->unloadSharedLib(hLipiEngine);
        delete utilPtr;

		return -1;
	}

//	Assign the logical name of the project to this string, i.e. TAMIL_WORD
	string strLogicalName = string(argv[1]);

	strcpy(infilelist,  argv[2]);
	outfile = argv[3];

	LTKWordRecognizer *pWordReco = NULL;
	ptrObj->createWordRecognizer(strLogicalName,&pWordReco);
	if(pWordReco == NULL)
	{
		cout << "\nError creating Word Recognizer\n";
	
		utilPtr->unloadSharedLib(hLipiEngine);
        delete utilPtr;
		return -1;
	}

//	You can also use project and profile name to create LipiEngine instance as follows...
//	string strProjectName = "tamil_boxed_field";
//	string strProfileName = "default";
//	LTKWordRecognizer *pWordReco = ptrObj->createWordRecognizer(&strProjectName, &strProfileName);

	int iErrorCode = 0;
	LTKRecognitionContext *recoContext = new LTKRecognitionContext();

	if(iErrorCode != 0)
	{
		cout << "\nError creating recognition context.\n";
		ptrObj->deleteWordRecognizer(pWordReco);

        utilPtr->unloadSharedLib(hLipiEngine);
        delete utilPtr;
        
		return -1;
	}

	LTKCaptureDevice deviceContext;
	LTKScreenContext screenContext;
	int numChoices = 2;

	// Setting the device attributes
	deviceContext.setSamplingRate(120);	
	deviceContext.setXDPI(2500);
	deviceContext.setYDPI(2500);
	deviceContext.setUniformSampling(true);

	// Set the engine to recognizer
	recoContext->setWordRecoEngine(pWordReco);
	
	// set the device context
	recoContext->setDeviceContext(deviceContext);
	// set the screen context
	recoContext->setScreenContext(screenContext);

	recoContext->setFlag(tempStr,REC_UNIT_CHAR);
	recoContext->setFlag(tempStr1,REC_MODE_STREAMING);

	// set the number of choices required
	recoContext->setNumResults(numChoices);

	ifstream in(infilelist);
	if(in == NULL)
	{
		cout << "Test list file open error : " << infilelist << endl;
        delete utilPtr;
		return -1;
	}

	ofstream resultfile(outfile.c_str(),ios::out|ios::binary);

	//a Header of 0xFEFF is required to identify this is a
	//16 bit unicode file
	const unsigned short fHeader = 0xfeff;
	resultfile.write((char*)&fHeader,sizeof(unsigned short));

	while(in)
	{
		//Get the file name
		if(!getline(in,path,' ')) 
		{
			break;
		}
		
		//Get the word ID
		getline(in,strWordId);
		//iWordID = atoi(strShapeId.c_str());

		cout << path << endl;

		try
		{
			fieldInk.clear();
		
			//read the word file
			readWordFile(path, fieldInk, deviceContext, screenContext);
			
			for(charIndex = 0; charIndex < fieldInk.size(); ++charIndex)
			{
				recoContext->beginRecoUnit();
				recoContext->addTraceGroups(LTKTraceGroupVector(1,fieldInk.at(charIndex)));
				recoContext->endRecoUnit();

			}
		}
		catch(LTKException e)
		{
			LOG(LTKLogger::LTK_LOGLEVEL_ERR) << e.getExceptionMessage();
			return FAILURE;
		}

		//Calling recognize and retrieving the top result
		{
			LTKWordRecoResult result;
			vector<LTKWordRecoResult> r2;
			recoContext->recognize();
			recoContext->getTopResult(result);
			recoContext->getNextBestResults(numChoices-1, r2);

			vector<unsigned short> resultVec = result.getResultWord();

			if(!resultVec.empty())
			{
				resultfile.write((char *)&(resultVec.at(0)), resultVec.size()*sizeof(unsigned short));
				resultfile.write((char*)eolstr.c_str(),eolstr.length()*sizeof(unsigned short));

				for(i =0; i<r2.size(); ++i)
				{
					resultVec = r2.at(i).getResultWord();
					resultfile.write((char *)&(resultVec.at(0)), resultVec.size()*sizeof(unsigned short));
					resultfile.write((char*)eolstr.c_str(),eolstr.length()*sizeof(unsigned short));
				}
			}

			recoContext->clearRecognitionResult();
		}

	}

	resultfile.close();

	//delete word recognition instance
	if(pWordReco)
	{
		ptrObj->deleteWordRecognizer(pWordReco);
	}

	//delete recognition context object
	if(recoContext)
	{
		//ptrObj->deleteRecognitionContext(recoContext);
		delete recoContext;
	}

	

	//unload the LipiEngine module from memory...
	utilPtr->unloadSharedLib(hLipiEngine);
    delete utilPtr;

	return 0;
}