📄 runshaperec.cpp
字号:
/*****************************************************************************************
* 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-07-31 12:19:33 +0530 (Thu, 31 Jul 2008) $
* $Revision: 580 $
* $Author: kuanish $
*
************************************************************************/
/************************************************************************
* FILE DESCR: Implementation of RunShaperec tool
*
* CONTENTS:
* main
* getIntValue
* getStringValue
* printUsage
* ValidateCommandLineArgs
*
* AUTHOR: Thanigai Murugan K
*
* DATE: August 30, 2005
* CHANGE HISTORY:
* Author Date Description of change
************************************************************************/
#include "RunShaperec.h"
#include "LTKInkFileReader.h"
#include "LTKTrace.h"
#include "LTKLipiEngineInterface.h"
#include "LTKErrors.h"
#include "LTKStringUtil.h"
#include "LTKVersionCompatibilityCheck.h"
#include "LTKException.h"
#include "LTKErrorsList.h"
#include "LTKOSUtilFactory.h"
#include "LTKOSUtil.h"
#include "LTKConfigFileReader.h"
enum TRAIN_TEST_ADAPT {NONE, TEST, TRAIN, ADAPT};
TRAIN_TEST_ADAPT trainTestAdapt=NONE;
/* function pointer declaration to get the function address of "createLTKLipiEngine" */
typedef LTKLipiEngineInterface* (*FN_PTR_CREATELTKLIPIENGINE) (void);
FN_PTR_CREATELTKLIPIENGINE createLTKLipiEngine;
typedef void (*FN_PTR_DELETELTKLIPIENGINE) (void);
FN_PTR_DELETELTKLIPIENGINE deleteLTKLipiEngine;
typedef int (*FN_PTR_GETCURRENTVERSION) (int *iMajor, int *iMinor, int *iBugfix);
FN_PTR_GETCURRENTVERSION LipiEngine_getCurrentVersion;
typedef int (*FN_PTR_CREATELTKLIPIPREPROCESSOR)(const LTKControlInfo& , LTKPreprocessorInterface** );
FN_PTR_CREATELTKLIPIPREPROCESSOR createLTKLipiPreProcessor = NULL;
typedef int (*FN_PTR_DELETELTKLIPIPREPROCESSOR)(LTKPreprocessorInterface* );
FN_PTR_DELETELTKLIPIPREPROCESSOR deleteLTKLipiPreProcessor = NULL;
char strTrainLstFile[MAX_PATH] = "";
char strFeatureFile[MAX_PATH] = "";
char strTestLstFile[MAX_PATH] = "";
char strProjectName[MAX_PROJECT_NAME] = "";
char strProfileName[MAX_PROFILE_NAME] = "";
char strHeaderInfoFileName[MAX_PATH] = "";
char strComment[MAX_STRLEN] = "";
char strDataset[MAX_STRLEN] = "";
char strLogFile[MAX_PATH] = "";
char strLogLevel[MAX_PATH] = "";
string strLogFileName;
char strAdaptListFile[MAX_PATH];
char strLipiRootPath[MAX_PATH];
char strOutputFileName[MAX_PATH];
float confThreshold;
int numChoices;
bool bVersionRequest = false;
bool bComputePerformance = false;
/* Pointer to the LipiEngine interface */
LTKLipiEngineInterface *ptrObj = NULL;
void *hLipiEngine;
char **globalArg;
int globalArgCount = 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;
int iErrorCode;
void * functionHandle = NULL;
LTKOSUtil* utilPtr = LTKOSUtilFactory::getInstance();
LTKShapeRecognizer *pReco = NULL;
numChoices = NUM_CHOICES_FILTER_OFF;
confThreshold = CONF_THRESHOLD_FILTER_OFF;
strcpy(strLipiRootPath,"");
strcpy(strOutputFileName,"runshaperec.out");
strcpy (strAdaptListFile,"");
globalArg = argv;
globalArgCount = argc;
utilPtr->recordStartTime();
if(processCommandLineArgs() != 0)
{
printUsage();
delete utilPtr;
return -1;
}
if(bVersionRequest) /* Then display version and exit */
{
cout << "\n Version of runshaperec Tool: " << SUPPORTED_MIN_VERSION << endl;
delete utilPtr;
return 0;
}
/* Get the LIPI_ROOT environment variable */
envstring = strLipiRootPath;
if(strlen(envstring) == 0)
{
envstring = getenv(LIPIROOT_ENV_STRING);
if(envstring == NULL )
{
cout << "Error, Environment variable is not set LIPI_ROOT" << endl;
delete utilPtr;
return -1;
}
strcpy(strLipiRootPath, envstring );
}
// Load the LipiEngine.DLL
hLipiEngine = NULL;
iErrorCode = utilPtr->loadSharedLib(envstring, 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;
functionHandle = NULL;
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 runshaperec(ver: " <<
supportedMinVersion << ")" << endl;
cleanUp(&pReco, utilPtr);
return FAILURE;
}
// Version comparison END
/* Get the function address of "createLTKLipiEngine" function from the DLL module */
functionHandle = NULL;
utilPtr->getFunctionAddress(hLipiEngine, "createLTKLipiEngine", &functionHandle);
createLTKLipiEngine = (FN_PTR_CREATELTKLIPIENGINE) functionHandle;
functionHandle = NULL;
/* Get the function address of "deleteLTKLipiEngine" function from the DLL module */
functionHandle = NULL;
utilPtr->getFunctionAddress(hLipiEngine, "deleteLTKLipiEngine", &functionHandle);
deleteLTKLipiEngine = (FN_PTR_DELETELTKLIPIENGINE) functionHandle;
functionHandle = NULL;
// Create an instance of LipiEngine
ptrObj = createLTKLipiEngine();
// set the LIPI_ROOT path in Lipiengine module instance
ptrObj->setLipiRootPath(envstring);
// 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 : " << getErrorMessage(iErrorCode) << endl;
cleanUp(&pReco, utilPtr);
return FAILURE;
}
string strProjName(strProjectName), strProfName(strProfileName);
iErrorCode = ptrObj->createShapeRecognizer(strProjName, strProfName, &pReco);
if(iErrorCode != SUCCESS)
{
cout << "Error : "
<< getErrorMessage(iErrorCode) << endl;
cleanUp(&pReco,utilPtr);
return FAILURE;
}
// Train or Test option?
if(trainTestAdapt == TRAIN)
{
if(bComputePerformance)
{
utilPtr->recordStartTime();
}
string strHeader = strHeaderInfoFileName;
string comment = strComment;
string dataset = strDataset;
iErrorCode = pReco->train(strTrainLstFile, strHeader, comment, dataset);
if(iErrorCode != 0)
{
cout << "Error : " << getErrorMessage(iErrorCode) << endl;
cleanUp(&pReco,utilPtr);
return FAILURE;
}
if(bComputePerformance)
{
//Time at the end of Train Clustering
utilPtr->recordEndTime();
string timeTaken = "";
utilPtr->diffTime(timeTaken);
cout << "Time taken (in seconds) for train function: "
<< timeTaken << endl;
}
}
else if(trainTestAdapt == TEST )
{
if(bComputePerformance)
{
utilPtr->recordStartTime();
}
iErrorCode = evaluateShapeRecognizer(&pReco);
if(iErrorCode != SUCCESS)
{
cleanUp(&pReco,utilPtr);
return FAILURE;
}
if(bComputePerformance)
{
utilPtr->recordEndTime();
string timeTaken = "";
utilPtr->diffTime(timeTaken);
cout << "Time taken (in seconds) for test function: "
<< timeTaken << endl;
}
}
#ifdef _INTERNAL
else if(trainTestAdapt == ADAPT )
{
iErrorCode = evaluateAdapt(&pReco, strProjName);
if(iErrorCode !=SUCCESS)
{
cout << "Error loading Model data." << endl;
LOG( LTKLogger::LTK_LOGLEVEL_ERR) << "Error : " <<
iErrorCode << getErrorMessage(iErrorCode) << "Error in evaluateAdapt()"
<<endl;
cleanUp(&pReco, utilPtr);
LTKReturnError(iErrorCode);
}
}
#endif
else
{
cout << "Invalid option, Exiting " << endl;
}
cleanUp(&pReco, utilPtr);
cout << "Exiting runShapeRec\n";
return SUCCESS;
}
/***********************************************************************************
* 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;
string tempString = globalArg[index+1];
if (LTKStringUtil::isInteger(tempString) != true )
{
return -1;
}
iRes = atoi(globalArg[index+1]);
if(iRes < 0)
return -1;
*iValue = iRes;
return 0;
}
/***********************************************************************************
* AUTHOR : Vandana Roy
* DATE : 13-AUG-2007
* NAME : getFloatValue
* DESCRIPTION : To get the float 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 getFloatValue(int index, float* fValue)
{
// float fRes = 0.0f;
float fRes = -1.0f;
if(index > globalArgCount)
return -1;
if(index + 1 >= globalArgCount) // set to default...
return -1;
string tempString = globalArg[index+1];
if (LTKStringUtil::isFloat(tempString) != true )
{
return -1;
}
fRes = atof(globalArg[index+1]);
if(fRes < 0)
return -1;
*fValue = fRes;
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 : runshaperec\n");
printf("\nrunshaperec\n");
printf("\n -project <projectname>\n");
#ifdef _INTERNAL
printf("\n -train <filename> | -test <list filename> | -adapt <filename>\n");
#else
printf("\n -train <filename> | -test <list filename> \n");
#endif
printf("\n [-h <header info filename>]\n");
printf("\n [-lipiroot <root name of the lipitk>]\n");
printf("\n [-profile <profilename>]\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 [-confthreshold <confthreshold>]\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))
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -