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

📄 runwordrec.cpp

📁 An open source handwriting recongnition package!!!
💻 CPP
📖 第 1 页 / 共 2 页
字号:

		if(LTKSTRCMP(globalArg[i], OPTION_NUMCHOICES) == 0)
		{
			if(getIntValue(i, &numChoices) != 0)
			{
				printf("\nInvalid or no value specified for -numchoices option.\n");
				return -1;
			}
			else { i++; continue;}
		}

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

			strcpy(tempMode, globalArg[i]);

			// Now get the list file name
			if(getStringValue(i, tempVal) != 0)
			{
				printf("\nMissing list file name for %s option.\n", globalArg[i]);
				return -1;
			}
			i++;

			if(LTKSTRCMP(tempMode, OPTION_TEST)==0)
			{
				strcpy(strTestLstFile, tempVal);
			}
			continue;
		}

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

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

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

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

		if(LTKSTRCMP(globalArg[i], OPTION_LOGFILE) == 0)
		{
			if(getStringValue(i, strLogFile) != 0)
			{
				printf("\nInvalid or no value specified for -logfile option.\n");
				return -1;
			}
			else { i++; continue;}
		}

		if(LTKSTRCMP(globalArg[i], LIPI_ROOT) == 0)
		{
			if(getStringValue(i, strLipiRootPath) != 0)
			{
				printf("\nInvalid or no value specified for -lipiroot option.\n");
				return -1;
			}
			else { i++;  continue;}
		}
		
		if(LTKSTRCMP(globalArg[i], OPTION_LOGLEVEL) == 0)
		{
			if(getStringValue(i, strLogLevel) != 0)
			{
				printf("\nInvalid or no value specified for -loglevel option.\n");
				return -1;
			}
			else 
			{
				i++; 
				continue;
			}
		}

		printf("\nInvalid or Unknown option.\n");
		return -1;
	}

	if(LTKSTRCMP(strProjectName, "") == 0)
	{
		printf("\nNo project name specified using -project.\n");
		return -1;
	}

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

	if(bProfileAssumed)
		printf("\n Profile not specified, assuming %s profile.\n", DEFAULT_PROFILE);

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

/**********************************************************************************
* 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_TEST) == 0) ||
		(LTKSTRCMP(strVal, OPTION_PROJECT) == 0) ||
		(LTKSTRCMP(strVal, OPTION_PROFILE) == 0) ||
		(LTKSTRCMP(strVal, OPTION_OUTPUT) == 0) ||
		(LTKSTRCMP(strVal, OPTION_LOGFILE) == 0) ||
		(LTKSTRCMP(strVal, OPTION_LOGLEVEL) == 0) ||
		(LTKSTRCMP(strVal, OPTION_NUMCHOICES) == 0))
		return 1;

	return 0;
}

//This currently assume the anotation
//is sequential
int readWordFile(string fileName, vector<LTKTraceGroup>& traceGroupVec, LTKCaptureDevice& dc, LTKScreenContext& sc)
{

	//anotation Info
	map<string, string>anotInfo;   //annotation info
	LTKTraceGroup traceGroup;      //trace group
	vector<string> strIdscs;       //comma separated strings
	vector<string> strIdshs;       //hyphen separated strings
	vector<int> intIds;            //intIds
	int firstId, secondId;         //first and last IDs when there are hyphenated IDs
	int loopIndex;                 //index of Ids
	
	vector<string>::iterator strIter;

	//Reading the anotation file
	if(LTKInkFileReader::readUnipenInkFileWithAnnotation(fileName,"CHARACTER","ALL",traceGroup,anotInfo, dc, sc) != SUCCESS)
	{
		cout<<"Error reading ink file:"<<fileName<<endl;
		LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Error reading ink file:"<<fileName<<endl;
		return FAILURE;
	}

	for(map<string,string>::iterator annotationIter=anotInfo.begin();annotationIter!=anotInfo.end();++annotationIter)
	{
		LTKTraceGroup charTraceGroup;  //TraceGroup corresponding to this char

		string strokeIndices=annotationIter->first;
		string comment=annotationIter->second;

		//Finding out the stroke IDs
		LTKStringUtil::tokenizeString(strokeIndices,  " ,\t",  strIdscs);
		for(strIter = strIdscs.begin(); strIter != strIdscs.end(); ++strIter)
		{
			if((*strIter).find('-')!=-1)
			{
				//tokenize string again if there are hyphens
				LTKStringUtil::tokenizeString(*strIter, "-", strIdshs);
				firstId  = atoi((strIdshs.front()).c_str()); //first ID
				secondId = atoi((strIdshs.back()).c_str());  //second ID

				for(loopIndex = firstId; loopIndex <=secondId; ++loopIndex )
				{
					intIds.push_back(loopIndex);
				}
			}
			else
			{
				intIds.push_back(atoi((*strIter).c_str() ));
			}
			strIdshs.clear();
		}

		strIdscs.clear();

		//sort the IDs in the sequence
		sort(intIds.begin(), intIds.end(),less<int>());

		//constructing a temporary trace group
		for(loopIndex = 0; loopIndex < intIds.size(); ++loopIndex)
		{

			if(intIds.at(loopIndex) < 0 || intIds.at(loopIndex) >= traceGroup.getNumTraces())
			{
				cout<<"Annotation and trace group read from ink file:"<<fileName<<" do not match"<<endl;
				LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Annotation and trace group read from ink file:"<<fileName<<" do not match"<<endl;
				return FAILURE;

			}

			LTKTrace tempTrace;
			traceGroup.getTraceAt(intIds.at(loopIndex), tempTrace);
			
			charTraceGroup.addTrace(tempTrace);
		}

		
		//push back this trace vector.
		traceGroupVec.push_back(charTraceGroup);


		intIds.clear();
	}

	return SUCCESS;
}


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

⌨️ 快捷键说明

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