2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <mv_private.h>
20 #include "machine_learning_exception.h"
21 #include "label_manager.h"
24 using namespace mediavision::machine_learning::exception;
26 LabelManager::LabelManager(string label_file, double decision_threshold) : _labels_and_files(), _label_file(label_file)
28 _decision_threshold = decision_threshold;
30 LOGD("labal file path is %s", label_file.c_str());
31 LOGD("decision_threshold value is %lf", _decision_threshold);
34 LabelManager::~LabelManager()
37 void LabelManager::Clear()
39 _labels_and_files.clear();
42 float LabelManager::GetDecisionThreshold()
44 return _decision_threshold;
47 float LabelManager::GetDecisionWeight()
49 return _decision_weight;
52 unsigned int LabelManager::GetLabelIndex(const string given_label)
56 readFile.open(_label_file.c_str());
61 throw InvalidOperation("Fail to open " + _label_file + " file.");
65 while (getline(readFile, line)) {
68 if (line.compare(given_label) == 0) {
76 throw InvalidOperation("Label index not found.");
79 bool LabelManager::IsExist(const string given_label)
83 readFile.open(_label_file.c_str());
88 throw InvalidOperation("Fail to open " + _label_file + " file.");
92 while (getline(readFile, line)) {
95 if (line.compare(given_label) == 0) {
105 unsigned int LabelManager::RemoveLabel(const string given_label)
109 readFile.open(_label_file.c_str());
113 string new_label_file(_label_file);
115 new_label_file += ".new";
117 writeFile.open(new_label_file.c_str(), ios::out | ios::binary);
121 if (readFile.fail() || !writeFile.is_open())
122 throw InvalidOperation("Fail to open " + _label_file + "or " + new_label_file + " file.");
126 while (getline(readFile, line)) {
127 if (line.compare(given_label) != 0) {
129 writeFile.write(line.c_str(), line.size());
137 if (label_index == 0) {
138 // Just keep original version in case of no copy so drop the new label file.
139 int ret = ::remove(new_label_file.c_str());
141 throw InvalidOperation("Fail to remove label file.");
143 int ret = ::remove(_label_file.c_str());
145 throw InvalidOperation("Fail to remove label file.");
147 ret = ::rename(new_label_file.c_str(), _label_file.c_str());
149 throw InvalidOperation("Fail to rename new labal file to original one.");
155 int LabelManager::GetLabelString(string &label, const int idx)
159 readFile.open(_label_file.c_str());
161 int label_index = -1;
165 throw InvalidOperation("Fail to open " + _label_file + " file.");
169 while (getline(readFile, line)) {
173 if (idx == label_index) {
175 label.erase(remove(label.begin(), label.end(), '\n'), label.end());
184 unsigned int LabelManager::AddLabelToFile(string given_label)
188 writeFile.open(_label_file.c_str(), ios::out | ios::app);
190 if (!writeFile.is_open())
191 throw InvalidOperation("Fail to open " + _label_file + " file.");
194 writeFile.write(given_label.c_str(), given_label.size());
197 return GetMaxLabel(_label_file);
200 int LabelManager::ImportLabel(void)
202 // label count is 0 if lael file doesn't exist.
203 if (!FaceRecogUtil::IsFileExist(_label_file))
208 readFile.open(_label_file);
213 throw InvalidOperation("Fail to open " + _label_file + " file.");
217 while (getline(readFile, line)) {
218 bool duplicated = AddLabelToMap(line, line);
230 bool LabelManager::AddLabelToMap(const string given_label, const string image_file)
232 // Find same one if not empty. If same one exists in the map then skip.
233 if (!_labels_and_files.empty()) {
234 auto item = _labels_and_files.find(given_label);
235 if (item != _labels_and_files.end())
239 _labels_and_files.insert(pair<string, string>(given_label, image_file));
244 size_t LabelManager::GetMaxLabel(const string label_file)
246 // label count is 0 if lael file doesn't exist.
247 if (!FaceRecogUtil::IsFileExist(label_file))
252 readFile.open(label_file.c_str());
254 size_t label_cnt = 0;
257 throw InvalidOperation("Fail to open " + label_file + " file.");
261 while (getline(readFile, line))
269 size_t LabelManager::GetMaxLabel()
271 return GetMaxLabel(_label_file);
274 string LabelManager::GetLabelFromAnswer(const vector<float> &result)
277 throw InvalidParameter("result vector is empty.");
279 int answer_idx = max_element(result.begin(), result.end()) - result.begin();
280 if (result[answer_idx] < _decision_threshold)
281 throw InvalidOperation("Not recognized.");
285 int ret = GetLabelString(answer_label, answer_idx);
287 throw InvalidOperation("answer label not found.");
292 void LabelManager::Remove()
294 // Remove existing files forcely.
295 int ret = ::remove(_label_file.c_str());
297 throw InvalidOperation("Fail to remove label file.");