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

📄 runshaperec.cpp

📁 An open source handwriting recongnition package!!!
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		cout << "Project name is not specified." << endl;
		return -1; // No projectname specified
	}

	if((LTKSTRCMP(strTrainLstFile, "") == 0) && 
       (LTKSTRCMP(strTestLstFile, "") == 0) && 
       (LTKSTRCMP(strFeatureFile, "") == 0) &&
	   (LTKSTRCMP(strAdaptListFile, "") == 0)
	){
		cout << "Either -train/-test/-adapt or -writefeatureFile option is required to run this." << endl;
		return -1; // No option specified -train or -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;
		}

		if(LTKSTRCMP(globalArg[i], OPTION_NUMCHOICES) == 0)
		{
			if ( i+1 >= globalArgCount )
			{
				cout << "Invalid or no value specified for -numchoices option." << endl;
				return -1;
			}

			if ( LTKSTRCMP(globalArg[i+1], "all" ) == 0 )
			{
				numChoices = NUM_CHOICES_FILTER_OFF;
                i++ ;
                continue;
			}
			else if(getIntValue(i, &numChoices) != 0)
			{
				cout << "Invalid or no value specified for -numchoices option." << endl;
				return -1;
			}
			else 
            { 
                i++; 
                continue;
            }
		}
		if(LTKSTRCMP(globalArg[i], OPTION_CONFTHRESHOLD) == 0)
		{
			if(getFloatValue(i, &confThreshold) != 0)
			{
				cout << "Invalid or no value specified for -confthreshold option." << endl;
				return -1;
			}
			else { i++; continue;}
		}


		if((LTKSTRCMP(globalArg[i], OPTION_TRAIN) == 0) || 
			(LTKSTRCMP(globalArg[i], OPTION_TEST) == 0) ||
			(LTKSTRCMP(globalArg[i], OPTION_ADAPT) == 0))
		{
			char tempVal[MAX_PATH], tempMode[124];

			strcpy(tempMode, globalArg[i]);

			// Now get the list file name
			if(getStringValue(i, tempVal) != 0)
			{
				cout << "Missing list file name for" << globalArg[i] << " option."  << endl;
				return -1;
			}
			i++;

			if(LTKSTRCMP(tempMode, OPTION_TRAIN)==0)
			{
				trainTestAdapt = TRAIN;
				strcpy(strTrainLstFile, tempVal);
			}
			if(LTKSTRCMP(tempMode, OPTION_TEST)==0)
			{
				trainTestAdapt = TEST;
				strcpy(strTestLstFile, tempVal);
			}
			if(LTKSTRCMP(tempMode, OPTION_ADAPT)==0)
			{
				trainTestAdapt = ADAPT;
				strcpy(strAdaptListFile, tempVal);
			}			
			continue;
		}

		if(LTKSTRCMP(globalArg[i], OPTION_PERF) == 0)
		{
			if(globalArgCount <= MIN_ARGS)
			{
				cout << "Option " << globalArg[i] << 
					    " can only be combined with other options." << endl;
				return -1;
			}
			else { bComputePerformance = true; continue;}
		}

		if(LTKSTRCMP(globalArg[i], OPTION_PROJECT) == 0)
		{
			if(getStringValue(i, strProjectName) != 0)
			{
				cout << "Invalid or no value specified for -project option." << endl;
				return -1;
			}
			else { i++; continue;}
		}

		if(LTKSTRCMP(globalArg[i], OPTION_COMMENT) == 0)
		{
			if(getStringValue(i, strComment) != 0)
			{
				cout << "Invalid or no value specified for -comment option." << endl;
				return -1;
			}
			else { i++; continue;}
		}

		if(LTKSTRCMP(globalArg[i], OPTION_DATASET) == 0)
		{
			if(getStringValue(i, strDataset) != 0)
			{
				cout << "Invalid or no value specified for -dataset option." << endl;
				return -1;
			}
			else { i++; continue;}
		}

		if(LTKSTRCMP(globalArg[i], OPTION_PROFILE) == 0)
		{
			if(getStringValue(i, strProfileName) != 0)
			{
				cout << "Invalid or no value specified for -profile option." << endl;
				return -1;
			}
			else { i++; bProfileAssumed = false; continue;}
		}

		if(LTKSTRCMP(globalArg[i], OPTION_OUTPUT) == 0)
		{
			if(getStringValue(i, strOutputFileName) != 0)
			{
				cout << "Invalid or no value specified for -output option." << endl;
				return -1;
			}
			else { i++; continue;}
		}

		if(LTKSTRCMP(globalArg[i], OPTION_HEADERINFO) == 0)
		{
			if(getStringValue(i, strHeaderInfoFileName) != 0)
			{
				cout << "Invalid or no value specified for -h option." << endl;
				return -1;
			}
			else { i++; continue;}
		}

		if(LTKSTRCMP(globalArg[i], OPTION_LOGFILE) == 0)
		{
			if(getStringValue(i, strLogFile) != 0)
			{
				cout << "Invalid or no value specified for -logfile option." << endl;
				return -1;
			}
			else { i++; continue;}
		}
		if(LTKSTRCMP(globalArg[i], OPTION_LOGLEVEL) == 0)
		{
			if(getStringValue(i, strLogLevel) != 0)
			{
				cout << "Invalid or no value specified for -loglevel option." << endl;
				return -1;
			}
			else 
			{
				i++; 
				continue;
			}
		}

		if(LTKSTRCMP(globalArg[i], LIPI_ROOT) == 0)
		{
			if(getStringValue(i, strLipiRootPath) != 0)
			{
				cout << "Invalid or no value specified for -lipiroot option." << endl;
				return -1;
			}
			else { i++;  continue;}
		}
		
		cout << globalArg[i] << " : Invalid or Unknown option." << endl;
		return -1;
	}

	if(trainTestAdapt == NONE)
		return -1; // specify either training or testing

	if(LTKSTRCMP(strProjectName, "") == 0)
	{
		cout << "No project name specified using -project." << endl;
		return -1;
	}

	if(ValidateCommandLineArgs() != 0)
		return -1;
	
	if(LTKSTRCMP(strProfileName, "") == 0)
		strcpy(strProfileName, DEFAULT_PROFILE);

	if(trainTestAdapt== TEST && (LTKSTRCMP(strHeaderInfoFileName, "") != 0))
	{
		cout << "Ignoring -h for option -test." << endl;
	}

	if(bProfileAssumed)
		cout << "Profile not specified, assuming " 
		     <<  DEFAULT_PROFILE << " profile." << endl;

	return 0;
}

/**********************************************************************************
* AUTHOR		: Thanigai Murugan K
* DATE			: 30-AUG-2005
* NAME			: evaluateShapeRecognizer
* DESCRIPTION	: Load the model data and call recognize function and display the
*				  results. 
* ARGUMENTS		: pReco - handle to LTKShapeRecognizer object
* RETURNS		: -1 on error 0 on success
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
* Saravanan .R		17-01-07			Call a getAbsolutePath() function to convert 
*										the Relative path to absolute path for testing
*************************************************************************************/
int evaluateShapeRecognizer(LTKShapeRecognizer **pReco)
{
	
	
	//LTKCaptureDevice captureDevice(0, true, 0, 0, 0);
    LTKCaptureDevice captureDevice;
	LTKScreenContext screenContext;
	int iErrorCode;

	vector<int> shapeSubSet; //subset of classes to be compared for recognition. If empty, all classes are compared

	

//	float confThreshold = 0.0f;

	vector<LTKShapeRecoResult> results;

	string path;

	string numSamples;

	string strShapeId;

	int intShapeId;

	int errorCount = 0;

	int total = 0;

	results.reserve(numChoices+1);

	try
	{
		int iResult = (*pReco)->loadModelData();
		if(iResult != 0)
		{
			cout << "Error loading Model data." << endl;
			LOG( LTKLogger::LTK_LOGLEVEL_ERR) << "Error loading Model data." << endl;

			cout << "Error : " <<  getErrorMessage(iResult) << endl;
			return -1;
		}
	}

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

		return FAILURE;
	}
	
	ifstream in(strTestLstFile);
	if(in == NULL)
	{
		LOG( LTKLogger::LTK_LOGLEVEL_ERR) << "Test list file open error" + string(strTestLstFile) << endl;
		cout << "Test list file open error : " << strTestLstFile << endl;
		return -1;
	}

	ofstream resultfile(strOutputFileName);

	while(in)
	{
		if(!getline(in,path,' ')) 
		{
			break;
		}
		
		getline(in,strShapeId);

		++total;

		intShapeId = atoi(strShapeId.c_str());

		LTKTraceGroup traceGroup;


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

		if(path.length() == 0)
		{
			LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << "Empty File name"  << endl;
			continue;
		}
			
		/* To get the Absolute Path from the ink file	*/					

		int error = getAbsolutePath(path, strLipiRootPath);

		/* Print the path name	*/

		cout << path << endl;			

		try
		{
			iErrorCode = LTKInkFileReader::readUnipenInkFile(path, traceGroup, captureDevice, screenContext);
		}
		catch(LTKException e)
		{
			cout << "Error while reading the input file" << endl;
			cout << getErrorMessage(iErrorCode) << endl;
			LOG( LTKLogger::LTK_LOGLEVEL_ERR) << getErrorMessage(iErrorCode) << endl;
			return FAILURE;
		}

		(*pReco)->setDeviceContext(captureDevice);
		iErrorCode = (*pReco)->recognize(traceGroup, screenContext, shapeSubSet, confThreshold, numChoices, results);
		if(iErrorCode != 0)
		{
			cout << "Error : " <<  getErrorMessage(iErrorCode) << endl;
			return -1;
		}
			
	//	pReco->deleteClass(0);
		resultfile << path << '\t' << intShapeId << ' ';

        if (results.size() == 0)
        {
            resultfile << -1 << ' ' << -1 << ' ';
        }

		for(int index =0; index < results.size(); ++index)
		{
			resultfile << results[index].getShapeId() << ' ' << results[index].getConfidence() << ' ';
		}

		resultfile << endl;

		results.clear();

	}

	if(total > 0)
	{
		LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << "total" + total <<
					"Error Count = " + errorCount << "  " +(total-errorCount)/total << endl;
		cout << total << ' ' << errorCount << ' ' << (total-errorCount)/total;
	}


	return SUCCESS;

}

/**********************************************************************************
* AUTHOR		: Thanigai Murugan K
* DATE			: 30-AUG-2005
* NAME			: CheckForOption
* DESCRIPTION	: Check if the argument is matching with any of the option strings
* ARGUMENTS		: strVal - Value to match
* RETURNS		: 1 if matching and 0 if not matching
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*************************************************************************************/
int CheckForOption(char* strVal)
{
	if( (LTKSTRCMP(strVal, OPTION_PERF) == 0) ||
		(LTKSTRCMP(strVal, OPTION_TRAIN) == 0) ||
		(LTKSTRCMP(strVal, OPTION_TEST) == 0) ||
		(LTKSTRCMP(strVal, OPTION_PROJECT) == 0) ||
		(LTKSTRCMP(strVal, OPTION_PROFILE) == 0) ||
		(LTKSTRCMP(strVal, OPTION_OUTPUT) == 0) ||
		(LTKSTRCMP(strVal, OPTION_HEADERINFO) == 0) ||
		(LTKSTRCMP(strVal, OPTION_LOGFILE) == 0) ||
		(LTKSTRCMP(strVal, OPTION_LOGLEVEL) == 0) ||
		(LTKSTRCMP(strVal, OPTION_NUMCHOICES) == 0))
		return 1;

	return 0;
}


/*****************************************************************************
* AUTHOR		: Nidhi Sharma
* DATE			: 30-AUG-2005
* NAME			: getAbsolutePath
* DESCRIPTION	: 
* ARGUMENTS		: 
* RETURNS		: 
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*****************************************************************************/
int getAbsolutePath (string &pathName, const string lipiRootPath)
{

	vector<string> tokens;

	//Split the path name into number of tokens based on the delimter
	LTKStringUtil::tokenizeString(pathName,  "\\/",  tokens);

	//The first token must be the $LIPI_ROOT. Otherwise return from the function
	if (tokens[0] != LIPIROOT)
	{
		return SUCCESS;
	}
	
	//Store the Environment variable into the tokens
	tokens[0] = lipiRootPath;

	//Reinitialize the pathName
	pathName = "";
	for(int i=0 ; i < tokens.size() ; i++)
	{
		pathName += tokens[i] + SEPARATOR;
	}

	// Erase the last character '\'
	pathName.erase(pathName.size()-1,1);

	return SUCCESS;
}

/*****************************************************************************
* AUTHOR		: Nidhi Sharma
* DATE			: 04-Jul-2008
* NAME			: cleanUp
* DESCRIPTION	: 
* ARGUMENTS		: 
* RETURNS		: 
* NOTES			:
* CHANGE HISTROY
* Author			Date				Description of change
*****************************************************************************/
int cleanUp(LTKShapeRecognizer** pReco, LTKOSUtil* utilPtr)
{
    int iErrorCode = SUCCESS;

    if (*pReco != NULL)
    {
        iErrorCode = ptrObj->deleteShapeRecognizer(pReco);
        
        if (iErrorCode != SUCCESS )
        {
            cout << "Error deleting shape recognizer " << iErrorCode <<
                getErrorMessage(iErrorCode) << endl;

    		return iErrorCode;
        }
    }

    
    // delete lipiengine instance
    deleteLTKLipiEngine();
    
	// Unload the DLL from memory
	iErrorCode = utilPtr->unloadSharedLib(hLipiEngine);

    if (iErrorCode != SUCCESS )
    {
        cout << "Error unloading lipiengine library " << endl;

		return FAILURE;
    }

    if (utilPtr != NULL )
    {
        delete utilPtr;
        utilPtr = NULL;
    }

	return SUCCESS;
}

⌨️ 快捷键说明

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