示例#1
0
/**********************************************************************************
* AUTHOR		: Thanigai Murugan K
* DATE			: 30-AUG-2005
* NAME			: main
* DESCRIPTION	: Main function. Process the command line options and invoke the
*				  train/test methods after instantiating LipiEngine module.
* ARGUMENTS		: Command line arguments, refer to PrintUsage() function for syntax
* RETURNS		: -1 on error 0 on success
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/
int main(int argc, char** argv)
{
	//char *envstring = NULL;
	char lipienginepath[MAX_PATH]="";
	int iErrorCode;
    LTKOSUtil* utilPtr = LTKOSUtilFactory::getInstance();
    void *functionHandle = NULL;
	
	globalArg = argv;
	globalArgCount = argc;

	utilPtr->recordStartTime();
	
	// Assume the default log file, if user did not specify one...
	if(LTKSTRCMP(strLogFile, "") == 0)
	{
		strcpy(strLogFile, DEFAULT_LOG_FILE);
	}

	if(processCommandLineArgs() != 0)
	{
		printUsage();
		delete utilPtr;
		return -1;
	}

	if(bVersionRequest) /* Then display version and exit */
	{
		cout << "\n Version of runwordrec tool: " << SUPPORTED_MIN_VERSION << endl;
		delete utilPtr;
		return 0;
	}

	/* Get the LIPI_ROOT environment variable if the user has not provided in the command line */


	if(strlen(strLipiRootPath)==0)
	{
		char *tempStr=NULL;

		/* Get the LIPI_ROOT environment variable */
		tempStr=getenv(LIPIROOT_ENV_STRING);
		

		if(tempStr == NULL)
		{
			cout << "Error,LIPI_ROOT is neither provided in the command line nor set as an environment variable\n" << endl;
			delete utilPtr;
			return -1;
		}
		strcpy(strLipiRootPath,tempStr);
	}


	// Load the LipiEngine.DLL
	hLipiEngine = NULL;

    iErrorCode = utilPtr->loadSharedLib(strLipiRootPath, 
                                        LIPIENGINE_MODULE_STR, 
                                        &hLipiEngine);

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

	int iMajor_lipiEngine=0, iMinor_lipiEngine=0, iBugfix_lipiEngine=0;

    iErrorCode = utilPtr->getFunctionAddress(hLipiEngine, 
                                             "getToolkitVersion", 
                                             &functionHandle);

    if(iErrorCode != SUCCESS)
	{
		cout << "Error mapping the getToolkitVersion function" << endl;
		delete utilPtr;
		return -1;
	}

    LipiEngine_getCurrentVersion = (FN_PTR_GETCURRENTVERSION) functionHandle;

	LipiEngine_getCurrentVersion(&iMajor_lipiEngine, &iMinor_lipiEngine, &iBugfix_lipiEngine);

	// Version comparison START
	char toolkitVer[MAX_STRLEN];
	sprintf(toolkitVer, "%d.%d.%d",iMajor_lipiEngine,iMinor_lipiEngine,iBugfix_lipiEngine);

	LTKVersionCompatibilityCheck verTempObj;
	string supportedMinVersion(SUPPORTED_MIN_VERSION);
	string toolkitVersion(toolkitVer);

	bool compatibilityResults = verTempObj.isFirstVersionHigher(toolkitVersion, supportedMinVersion);

	if(compatibilityResults == false)
	{
		cout<< "\nIncompatible version of LipiEngine(ver: " << toolkitVersion << ") with runwordrec(ver: " << supportedMinVersion << ")" << endl;
		
		// Unload the DLL from memory
		utilPtr->unloadSharedLib(hLipiEngine);
		delete utilPtr;
		return FAILURE;
	}
	// Version comparison END

	// without reserving memory, it gives an error at the end...
	strLogFileName.reserve(MAX_PATH); 

	/* Get the function address of "createLTKLipiEngine" function from the DLL module */
    functionHandle = NULL;
    iErrorCode = utilPtr->getFunctionAddress(hLipiEngine, 
                                            "createLTKLipiEngine", 
                                            &functionHandle);

    if(iErrorCode != SUCCESS)
	{
		cout << "Error mapping the createLTKLipiEngine function" << endl;
		delete utilPtr;
		return -1;
	}

    createLTKLipiEngine = (FN_PTR_CREATELTKLIPIENGINE) functionHandle;

    functionHandle = NULL;
    
	// Create an instance of LipiEngine
	ptrObj = createLTKLipiEngine();

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

	// set the Log File Path
	if (strlen(strLogFile) != 0 )
	{
		string tempString(strLogFile);
		ptrObj->setLipiLogFileName(tempString);
	}

	if(strlen(strLogLevel) != 0)
	{
		string tempStringLogLevel(strLogLevel);
		ptrObj->setLipiLogLevel(tempStringLogLevel);
	}


	// Initialize the LipiEngine 
	iErrorCode = ptrObj->initializeLipiEngine();
	if(iErrorCode != 0)
	{
		cout << "Error initializing lipiengine: " << getErrorMessage(iErrorCode) << endl;
		cout << "For more details, please see the log file" << endl;
		// Unload the DLL from memory
		utilPtr->unloadSharedLib(hLipiEngine);
		delete utilPtr;
		return -1;
	}

	string strProjName(strProjectName), strProfName(strProfileName);

	// Now create the word recognizer instance using the project/profile name strings
	LTKWordRecognizer *pReco;
	iErrorCode = ptrObj->createWordRecognizer(strProjName, strProfName, &pReco);

	if(iErrorCode != SUCCESS)
	{
		cout << "Error creating word recognizer: " << getErrorMessage(iErrorCode) << endl;
		cout << "For more details, please see the log file" << endl;

		// Unload the DLL from memory
		utilPtr->unloadSharedLib(hLipiEngine);
		delete utilPtr;
		return -1;
	}

	if(bComputePerformance)
    {
        utilPtr->recordStartTime();
    }
	
	iErrorCode = evaluateWordRecognizer(pReco, strTestLstFile);
	if(iErrorCode != SUCCESS)
	{

		cout << "Error during testing the word recognizer: " << getErrorMessage(iErrorCode) << endl;
		cout << "For more details, please see the log file" << endl;

		ptrObj->deleteWordRecognizer(&pReco);

		// Unload the DLL from memory
		utilPtr->unloadSharedLib(hLipiEngine);
		delete utilPtr;
		return -1;
	}

	if(bComputePerformance)
	{
		utilPtr->recordEndTime();

        string timeTaken = "";
        utilPtr->diffTime(timeTaken);
        cout << "Time taken:" << timeTaken << endl;
	}

	// Delete the word recognizer which was created...
	ptrObj->deleteWordRecognizer(&pReco);

	// Unload the DLL from memory
	utilPtr->unloadSharedLib(hLipiEngine);

    delete utilPtr;


	return 0;
}
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;
}