// Returns true if successful, false otherwise bool FeatureMatcher::loadRecognitionSet(string country) { std::ostringstream out; out << config->getKeypointsRuntimeDir() << "/" << country << "/"; string country_dir = out.str(); if (DirectoryExists(country_dir.c_str())) { vector<Mat> trainImages; vector<string> plateFiles = getFilesInDir(country_dir.c_str()); for (unsigned int i = 0; i < plateFiles.size(); i++) { if (hasEnding(plateFiles[i], ".jpg") == false) continue; string fullpath = country_dir + plateFiles[i]; Mat img = imread( fullpath ); // convert to gray and resize to the size of the templates cvtColor(img, img, CV_BGR2GRAY); resize(img, img, getSizeMaintainingAspect(img, config->stateIdImageWidthPx, config->stateIdimageHeightPx)); if( img.empty() ) { cout << "Can not read images" << endl; return -1; } Mat descriptors; vector<KeyPoint> keypoints; detector->detect( img, keypoints ); extractor->compute(img, keypoints, descriptors); if (descriptors.cols > 0) { billMapping.push_back(plateFiles[i].substr(0, 2)); trainImages.push_back(descriptors); trainingImgKeypoints.push_back(keypoints); } } this->descriptorMatcher->add(trainImages); this->descriptorMatcher->train(); return true; } return false; }
// Attempts to recognize the plate. Returns a confidence level. Updates teh "stateCode" variable // with the value of the country/state int StateIdentifier::recognize(Mat img, char* stateCode) { timespec startTime; getTime(&startTime); cvtColor(img, img, CV_BGR2GRAY); resize(img, img, getSizeMaintainingAspect(img, config->stateIdImageWidthPx, config->stateIdimageHeightPx)); Mat plateImg(img.size(), img.type()); //plateImg = equalizeBrightness(img); img.copyTo(plateImg); Mat debugImg(plateImg.size(), plateImg.type()); plateImg.copyTo(debugImg); vector<int> matchesArray(featureMatcher->numTrainingElements()); RecognitionResult result = featureMatcher->recognize(plateImg, true, &debugImg, true, matchesArray ); if (this->config->debugStateId) { displayImage(config, "State Identifier1", plateImg); displayImage(config, "State Identifier", debugImg); cout << result.haswinner << " : " << result.confidence << " : " << result.winner << endl; } if (config->debugTiming) { timespec endTime; getTime(&endTime); cout << "State Identification Time: " << diffclock(startTime, endTime) << "ms." << endl; } if (result.haswinner == false) return 0; strcpy(stateCode, result.winner.c_str()); return result.confidence; }
// Attempts to recognize the plate. Returns a confidence level. Updates the region code and confidence // If region is found, returns true. bool StateIdentifier::recognize(PipelineData* pipeline_data) { timespec startTime; getTime(&startTime); Mat plateImg = Mat(pipeline_data->grayImg, pipeline_data->regionOfInterest); resize(plateImg, plateImg, getSizeMaintainingAspect(plateImg, config->stateIdImageWidthPx, config->stateIdimageHeightPx)); Mat debugImg(plateImg.size(), plateImg.type()); plateImg.copyTo(debugImg); vector<int> matchesArray(featureMatcher->numTrainingElements()); RecognitionResult result = featureMatcher->recognize(plateImg, true, &debugImg, true, matchesArray ); if (this->config->debugStateId) { displayImage(config, "State Identifier1", plateImg); displayImage(config, "State Identifier", debugImg); cout << result.haswinner << " : " << result.confidence << " : " << result.winner << endl; } if (config->debugTiming) { timespec endTime; getTime(&endTime); cout << "State Identification Time: " << diffclock(startTime, endTime) << "ms." << endl; } if (result.haswinner == false) return 0; pipeline_data->region_code = result.winner; pipeline_data->region_confidence = result.confidence; if (result.confidence >= 10) return true; return false; }