/********************************************************************************** * AUTHOR : Deepu V. * DATE : 22-AUG-2005 * NAME : recognizeTraces * DESCRIPTION : performs the recognition of the new strokes added to rc * : pre condition - markers are present in this vector * : - m_numTracesProcessed and m_numCharsProcessed * : set to proper value * ARGUMENTS : rc - The recognitino context * RETURNS : * NOTES : * CHANGE HISTROY * Author Date Description of *************************************************************************************/ int BoxedFieldRecognizer::recognizeTraces(LTKRecognitionContext& rc ) { LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) <<"Entering: BoxedFieldRecognizer::recognizeTraces" <<endl; vector<LTKTrace>::const_iterator traceIter,traceEnd,traceBegin; //iterator for the traces int errorCode = FAILURE; int recUnit; //unit for recognition (should be char) LTKTraceGroup emptyChar; //TraceGroup object that buffers //all ink corresponding to a character vector<int> subSet; //passing a null arguement for shape subset vector<LTKShapeRecoResult> shapeRecoResults; //The object to hold the output from shape recognizer LTKScreenContext screenContext = rc.getScreenContext(); //retrieving the screen context LTKCaptureDevice captureDevice = rc.getDeviceContext(); //retrieving the device context const LTKTraceVector & traces = rc.getAllInk(); //retrieving the traces from recognition context string tempStr; //temporary string object if(m_shapeRecognizer == NULL) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Shape recognizer not initialized" <<endl; LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error : "<< ENULL_POINTER <<":"<< getErrorMessage(ENULL_POINTER) <<" BoxedFieldRecognizer::recognizeTraces" <<endl; LTKReturnError(ENULL_POINTER); } else if( (errorCode = m_shapeRecognizer->setDeviceContext(captureDevice)) != SUCCESS) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Unable to set device context in shape rec : " << getErrorMessage(errorCode) <<endl; LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error: BoxedFieldRecognizer::recognizeTraces"<<endl; LTKReturnError(errorCode); } shapeRecoResults.reserve(m_numShapeRecoResults+1);//reserving memory if(m_numTracesProcessed > traces.size()) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Invalid number of traces processed. " << "Traces processed = " << m_numTracesProcessed << " > total number of traces" << traces.size() <<endl; LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error : "<< EINVALID_NUM_OF_TRACES <<":"<< getErrorMessage(EINVALID_NUM_OF_TRACES) <<" BoxedFieldRecognizer::recognizeTraces" <<endl; LTKReturnError(EINVALID_NUM_OF_TRACES); } //Start processing from the number of traces processed. traceBegin = traces.begin() + m_numTracesProcessed; traceEnd = traces.end();int r=0; for(traceIter = traceBegin; traceIter != traceEnd; ++traceIter) { /* Marker strokes are inserted to detect * end of segment. The marker strokes are * identified by 9number of channels == 0) */ if((*traceIter).getNumberOfPoints() == 0) { tempStr = REC_UNIT_INFO; if((errorCode = rc.getFlag(tempStr,recUnit)) != SUCCESS) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error: BoxedFieldRecognizer::recognizeTraces"<<endl; LTKReturnError(errorCode); } switch(recUnit) { /* The segment is character * This algorithm recognizes * only character segments */ case REC_UNIT_CHAR: shapeRecoResults.clear(); //calling the shape recognizer's recognize method. if(m_boxedChar.getNumTraces() == 0) { LTKShapeRecoResult T; T.setShapeId(SHRT_MAX); T.setConfidence(1.0); shapeRecoResults.push_back(T); } else if( (errorCode = m_shapeRecognizer->recognize(m_boxedChar,screenContext,subSet, m_shapeRecoMinConfidence, m_numShapeRecoResults, shapeRecoResults ))!= SUCCESS ) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Shape recognition failed : " << getErrorMessage(errorCode) <<endl; LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error: BoxedFieldRecognizer::recognizeTraces"<<endl; LTKReturnError(errorCode); } //This updates the recognition results using //current shape recognition results if((errorCode = updateRecognitionResults(shapeRecoResults,rc)) != SUCCESS) { LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error: BoxedFieldRecognizer::recognizeTraces"<<endl; LTKReturnError(errorCode); } for(r=0;r<shapeRecoResults.size();++r) { LTKShapeRecoResult& tempResult=shapeRecoResults[r]; } m_boxedChar = emptyChar;//making the trace group empty again break; default: LOG(LTKLogger::LTK_LOGLEVEL_ERR) << "Unsupported reccognizer mode by Box Field" <<endl; LOG(LTKLogger::LTK_LOGLEVEL_ERR) <<"Error : "<< EINVALID_RECOGNITION_MODE <<":"<< getErrorMessage(EINVALID_RECOGNITION_MODE) <<" BoxedFieldRecognizer::recognizeTraces" <<endl; LTKReturnError(EINVALID_RECOGNITION_MODE); } ++m_numCharsProcessed; //incrementing number of characters processed } else { m_boxedChar.addTrace(*traceIter); //buffering the trace to the temp TraceGroup for recognition } ++m_numTracesProcessed; //incrementing the number of traces processed } LOG(LTKLogger::LTK_LOGLEVEL_DEBUG) <<"Exiting: BoxedFieldRecognizer::recognizeTraces" <<endl; return SUCCESS; }
int HolisticRecognizer::recognize(const LTKTraceGroup& traceGroup, const LTKScreenContext& screenContext, const vector<bool>& shapeSubSet, float confThreshold, int numChoices, vector<LTKShapeRecoResult>& resultVec) { LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << "Entered HolisticRecognizer::recognize" << endl; float2DVector testFeatureVector; intVector kNNindices; // indices of the min distance models floatVector kNNdist; // kNN distances corresponding to the indices LTKShapeRecoResult outResult; // result int shapeIndex; // index used for looping over all the shapes int loopIndex; // index used for looping vector<HolisticShapeModel> subSetOfReferenceModels; // subset on which the comparisons have to be made intVector labelVec; // list of current top choices floatVector distanceVec; // list of current top holistic distances intVector recognitionCount(m_numShapes,0); // count of recognized classes float confidence; // the confidence of the recognition // extracting the features from the incoming tracegroup m_featureExtractorObj.extractFeatures(traceGroup, m_captureDevice, screenContext, ltkShapeRecPtr, testFeatureVector); // if a particular class is not present in the shapeSubSet then it has to be rejected if (shapeSubSet.empty()==true) { subSetOfReferenceModels = m_referenceModels; } else { for(loopIndex = 0; loopIndex < m_referenceModels.size(); ++loopIndex) { int shapeId; shapeId = (m_referenceModels.at(loopIndex)).getShapeId(); if (shapeSubSet.at(shapeId) == true) { subSetOfReferenceModels.push_back(m_referenceModels.at(loopIndex)); } } } findkNN(testFeatureVector, subSetOfReferenceModels, holisticDistance, m_numNNs, m_worstCaseValue, kNNindices, kNNdist); // counting the number of correct recognitions corresponding to each shapeId for(loopIndex = 0; loopIndex < m_numNNs; ++loopIndex) { int shapeId; shapeId = (subSetOfReferenceModels.at(kNNindices.at(loopIndex))).getShapeId(); ++(recognitionCount.at(shapeId)); } // getting the top N counts and populating the resultVec for(loopIndex = 0; loopIndex < numChoices; ++loopIndex) { int maxCount = 0; int maxCountIndex = -1; // finding the class for which recognitionCount is the maximum for(shapeIndex = 0; shapeIndex < m_numShapes; ++shapeIndex) { if (maxCount < recognitionCount.at(shapeIndex)) { maxCount = recognitionCount.at(shapeIndex); maxCountIndex = shapeIndex; } } // to find the class with next maximum recognitionCount, the outResult.setShapeId(maxCountIndex); if(maxCountIndex != -1) { confidence = (float)recognitionCount.at(maxCountIndex)/(float)m_numNNs; outResult.setConfidence(m_confMapper.mapDistanceToConfidence(-(confidence))); } else { outResult.setConfidence(0); } resultVec.push_back(outResult); // present class recognitionCount is set to zero if (maxCountIndex != -1) { recognitionCount.at(maxCountIndex) = 0; } } LOG( LTKLogger::LTK_LOGLEVEL_DEBUG) << "Holistic Recognize Called" << endl; return SUCCESS; }