Exemple #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;
}
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;
}