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

📄 runwordrec.cpp

📁 An open source handwriting recongnition package!!!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************************
* Copyright (c) 2007 Hewlett-Packard Development Company, L.P.
* Permission is hereby granted, free of charge, to any person obtaining a copy of 
* this software and associated documentation files (the "Software"), to deal in 
* the Software without restriction, including without limitation the rights to use, 
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
* Software, and to permit persons to whom the Software is furnished to do so, 
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*****************************************************************************************/

/************************************************************************
 * SVN MACROS
 *
 * $LastChangedDate: 2008-08-29 09:57:07 +0530 (Fri, 29 Aug 2008) $
 * $Revision: 679 $
 * $Author: sharmnid $
 *
 ************************************************************************/
/************************************************************************
 * FILE DESCR: Implementation of RunWordrec tool
 *
 * CONTENTS:
 *			main
 *			getIntValue
 *			getStringValue
 *			printUsage
 *			ValidateCommandLineArgs
 *
 * AUTHOR:     Thanigai Murugan K
 *
 * DATE:       August 30, 2005
 * CHANGE HISTORY:
 * Author       Date            Description of change
 ************************************************************************/

#include "RunWordrec.h"
#include "LTKErrors.h"
#include "LTKWordRecoResult.h"
#include "LTKStringUtil.h"
#include "LTKVersionCompatibilityCheck.h"
#include "LTKException.h"
#include "LTKInc.h"
#include "LTKOSUtilFactory.h"
#include "LTKOSUtil.h"

int numChoices = 2;
char strTestLstFile[MAX_PATH] = "";
bool bComputePerformance = false;
char strProjectName[MAX_PROJECT_NAME] = "";
char strProfileName[MAX_PROFILE_NAME] = "";
char strLipiRootPath[MAX_PATH] = "";
char strOutputFileName[MAX_PATH] = "runwordrec.out";
bool bVersionRequest = false;

char **globalArg;
int globalArgCount = 0;

int getAbsolutePath (string &pathName, const string lipiRootPath);

/**********************************************************************************
* 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;
}

/**********************************************************************************
* AUTHOR		: Thanigai Murugan K
* DATE			: 30-AUG-2005
* NAME			: getIntValue
* DESCRIPTION	: To get the integer value which is passed as command line argument
*				  index specifies that the value to be fetched from the next command
*				  line argument position
* ARGUMENTS		: index - current index in the list of command line arguments
*				  iValue - Output value
* RETURNS		: -1 on error 0 on success
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/
int getIntValue(int index, int* iValue)
{
	int iRes = 0;

	if(index > globalArgCount)
		return -1;

	if(index + 1 >= globalArgCount) // set to default...
		return -1;

	iRes = atoi(globalArg[index+1]);
	if(iRes <= 0)
		return -1;

	*iValue = iRes;
	return 0;
}

/**********************************************************************************
* AUTHOR		: Thanigai Murugan K
* DATE			: 30-AUG-2005
* NAME			: getStringValue
* DESCRIPTION	: To get the string value which is passed as command line argument
*				  index specifies that the value to be fetched from the next command
*				  line argument position
* ARGUMENTS		: index - current index in the list of command line arguments
*				  strOption - Output value
* RETURNS		: -1 on error 0 on success
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/
int getStringValue(int index, char* strOption)
{
	if(index > globalArgCount)
		return -1;

	if(index + 1 >= globalArgCount) // set to default...
		return -1;

	if(strlen(globalArg[index+1]) >= MAX_PATH)
		strncpy(strOption, globalArg[index], MAX_PATH);
	else
	{
		if(strlen(globalArg[index+1]) <= 1)
			return -1;

		// Checking the value 
		if(CheckForOption(globalArg[index+1]) != 0) 
			return -1;

		strcpy(strOption, globalArg[index+1]);
	}

	return 0;
}

/**********************************************************************************
* AUTHOR		: Thanigai Murugan K
* DATE			: 30-AUG-2005
* NAME			: PrintUsage
* DESCRIPTION	: Prints the usage on an error
* ARGUMENTS		: None
* RETURNS		: void (none)
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/
void printUsage()
{
	printf("\n\nUsage : runwordrec\n");
	printf("\nrunwordrec\n");
	printf("\n	-project <projectname>\n");
	printf("\n	-test <list filename>\n");
	printf("\n	[-profile <profilename>]\n");
	printf("\n	[-lipiroot <root name of the lipitk>]\n");
	printf("\n	[-loglevel <DEBUG|ERR|ALL|OFF|INFO>]\n");
	printf("\n	[-logfile <logfilename>]\n");
	printf("\n	[-output <output filename>]\n");
	printf("\n	[-numchoices <numchoices>]\n");
	printf("\n	[-perf]\n");
	printf("\n	[-ver]\n\n");
}

/**********************************************************************************
* AUTHOR		: Thanigai Murugan K
* DATE			: 30-AUG-2005
* NAME			: ValidateCommandLineArgs
* DESCRIPTION	: Validate the command line arguments. Report an error if value is
*				  missing for any command line option
* ARGUMENTS		: None
* RETURNS		: 0 on Success and -1 on Error
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/
int ValidateCommandLineArgs()
{
	if((LTKSTRCMP(strProjectName, "") == 0))
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)<< "Project Name not Specified ... " << endl;
		return -1; // No projectname specified
	}

	if(LTKSTRCMP(strTestLstFile, "") == 0)
	{
		LOG(LTKLogger::LTK_LOGLEVEL_ERR)<< "Option (train/test) not Specified ... " << endl;
		return -1; // No option specified -test
	}

	return 0;
}

/**********************************************************************************
* AUTHOR		: Thanigai Murugan K
* DATE			: 30-AUG-2005
* NAME			: processCommandLineArgs
* DESCRIPTION	: Processes all the command line arguments. Report an error if value is
*				  missing for any command line option
* ARGUMENTS		: None
* RETURNS		: 0 on Success and -1 on Error
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/
int processCommandLineArgs()
{
	bool bProfileAssumed = true;

	// Minimum MIN_ARGS arguments must...
	if(globalArgCount >= MIN_ARGS)
	{
		if(LTKSTRCMP(globalArg[1], OPTION_VERSION) == 0)
		{
			bVersionRequest = true;
			return 0;
		}
	}

	for(int i = 1; i < globalArgCount; i++)
	{
		if((LTKSTRCMP(globalArg[i], OPTION_HELP1) == 0) || (LTKSTRCMP(globalArg[i], OPTION_HELP2) == 0))
		{
			return -1;
		}

		if(LTKSTRCMP(globalArg[i], OPTION_VERSION) == 0)
		{
			bVersionRequest = true;
			return 0;
		}

⌨️ 快捷键说明

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