, _result()
{}
-FaceRecognition::~FaceRecognition()
-{
- if (_label_manager)
- _label_manager->clear();
-}
-
void FaceRecognition::checkFeatureVectorFile(string fv_file_name, string new_fv_file_name)
{
// Change new feature vector file to existing one in case that current process is terminated just after removing existing feature vector file but
// Import label data from a label file.
importLabel();
- // 1. Store only label names to label file, which aren't duplicated.
- bool duplicated = _label_manager->addLabelToMap(label_name, label_name);
- if (!duplicated) {
+ // 1. Store a new label name to label file if the given label doesn't exist.
+ if (!_label_manager->isExist(label_name)) {
int ret = _label_manager->addLabelToFile(label_name);
if (ret == 0)
return MEDIA_VISION_ERROR_INVALID_OPERATION;
// Import label data from a label file.
importLabel();
- if (_label_manager->isExist(label_name) == false) {
+ if (!_label_manager->isExist(label_name)) {
LOGE("%s doesn't exist in label file.", label_name.c_str());
return MEDIA_VISION_ERROR_INVALID_OPERATION;
}
class LabelManager
{
private:
- std::map<std::string, std::string> _labels_and_files;
+ std::vector<std::string> _labels;
std::string _label_file;
float _decision_threshold;
static constexpr float _decision_weight = 0.01;
public:
LabelManager(std::string label_file, double decision_threshold);
~LabelManager();
- void clear();
float getDecisionThreshold();
float getDecisionWeight();
- unsigned int getLabelIndex(const std::string given_label);
+ unsigned int getLabelIndex(const std::string &given_label);
bool isExist(const std::string given_label);
unsigned int removeLabel(const std::string given_label);
- int getLabelString(std::string &label, const int idx);
+ void getLabelString(std::string &label, const int idx);
unsigned int addLabelToFile(std::string given_label);
- int importLabel(void);
- bool addLabelToMap(const std::string given_label, const std::string image_file);
- size_t getMaxLabel(const std::string label_file);
+ size_t importLabel(void);
+ std::vector<std::string> &getLabels(void);
size_t getMaxLabel();
- std::string getLabelFromAnswer(const std::vector<float> &result);
void removeFile();
};
using namespace std;
using namespace mediavision::machine_learning::exception;
-LabelManager::LabelManager(string label_file, double decision_threshold) : _labels_and_files(), _label_file(label_file)
+LabelManager::LabelManager(string label_file, double decision_threshold) : _labels(), _label_file(label_file)
{
_decision_threshold = decision_threshold;
LabelManager::~LabelManager()
{}
-void LabelManager::clear()
-{
- _labels_and_files.clear();
-}
-
float LabelManager::getDecisionThreshold()
{
return _decision_threshold;
return _decision_weight;
}
-unsigned int LabelManager::getLabelIndex(const string given_label)
+unsigned int LabelManager::getLabelIndex(const string &given_label)
{
- ifstream readFile;
-
- readFile.open(_label_file.c_str());
+ importLabel();
- int label_index = -1;
-
- if (readFile.fail())
- throw InvalidOperation("Fail to open " + _label_file + " file.");
-
- string line;
+ auto iter = find(_labels.begin(), _labels.end(), given_label);
+ if (iter == _labels.end())
+ throw InvalidOperation("Label index not found.");
- while (getline(readFile, line)) {
- label_index++;
-
- if (line.compare(given_label) == 0) {
- readFile.close();
-
- return label_index;
- }
- }
-
- readFile.close();
- throw InvalidOperation("Label index not found.");
-}
-
-bool LabelManager::isExist(const string given_label)
-{
- ifstream readFile;
-
- readFile.open(_label_file.c_str());
-
- int label_index = -1;
-
- if (readFile.fail())
- throw InvalidOperation("Fail to open " + _label_file + " file.");
-
- string line;
-
- while (getline(readFile, line)) {
- label_index++;
-
- if (line.compare(given_label) == 0) {
- readFile.close();
- return true;
- }
- }
-
- readFile.close();
- return false;
+ return distance(_labels.begin(), iter);
}
unsigned int LabelManager::removeLabel(const string given_label)
return label_index;
}
-int LabelManager::getLabelString(string &label, const int idx)
+void LabelManager::getLabelString(string &label, const int idx)
{
- ifstream readFile;
-
- readFile.open(_label_file.c_str());
+ importLabel();
- int label_index = -1;
- int ret = 0;
+ if (idx < 0 || _labels.size() <= idx)
+ throw InvalidOperation("A given label index is invalid.");
- if (readFile.fail())
- throw InvalidOperation("Fail to open " + _label_file + " file.");
-
- string line;
-
- while (getline(readFile, line)) {
- label_index++;
- line += "\n";
-
- if (idx == label_index) {
- label = line;
- label.erase(remove(label.begin(), label.end(), '\n'), label.end());
- break;
- }
- }
-
- readFile.close();
- return ret;
+ label = _labels[idx];
}
unsigned int LabelManager::addLabelToFile(string given_label)
writeFile.write(given_label.c_str(), static_cast<streamsize>(given_label.size()));
writeFile.close();
- return getMaxLabel(_label_file);
+ return getMaxLabel();
}
-int LabelManager::importLabel(void)
+size_t LabelManager::importLabel(void)
{
// label count is 0 if lael file doesn't exist.
if (!FaceRecogUtil::isFileExist(_label_file))
readFile.open(_label_file);
- int label_cnt = 0;
-
if (readFile.fail())
throw InvalidOperation("Fail to open " + _label_file + " file.");
string line;
+ _labels.clear();
+
while (getline(readFile, line)) {
- bool duplicated = addLabelToMap(line, line);
- if (duplicated)
+ // If a given label alreadys in the label file then just skip.
+ if (isExist(line))
continue;
- label_cnt++;
+ _labels.push_back(line);
}
readFile.close();
- return label_cnt;
+ return _labels.size();
}
-bool LabelManager::addLabelToMap(const string given_label, const string image_file)
+bool LabelManager::isExist(const string given_label)
{
- // Find same one if not empty. If same one exists in the map then skip.
- if (!_labels_and_files.empty()) {
- auto item = _labels_and_files.find(given_label);
- if (item != _labels_and_files.end())
- return true;
- }
-
- _labels_and_files.insert(pair<string, string>(given_label, image_file));
-
- return false;
-}
-
-size_t LabelManager::getMaxLabel(const string label_file)
-{
- // label count is 0 if lael file doesn't exist.
- if (!FaceRecogUtil::isFileExist(label_file))
- return 0;
-
- ifstream readFile;
-
- readFile.open(label_file.c_str());
-
- size_t label_cnt = 0;
-
- if (readFile.fail())
- throw InvalidOperation("Fail to open " + label_file + " file.");
-
- string line;
-
- while (getline(readFile, line))
- label_cnt++;
-
- readFile.close();
-
- return label_cnt;
+ return (find(_labels.begin(), _labels.end(), given_label) != _labels.end());
}
size_t LabelManager::getMaxLabel()
{
- return getMaxLabel(_label_file);
-}
-
-string LabelManager::getLabelFromAnswer(const vector<float> &result)
-{
- if (result.empty())
- throw InvalidParameter("result vector is empty.");
-
- int answer_idx = max_element(result.begin(), result.end()) - result.begin();
- if (result[answer_idx] < _decision_threshold)
- throw InvalidOperation("Not recognized.");
-
- string answer_label;
-
- int ret = getLabelString(answer_label, answer_idx);
- if (ret)
- throw InvalidOperation("answer label not found.");
+ importLabel();
- return answer_label;
+ return _labels.size();
}
void LabelManager::removeFile()