_config.input_tensor_shape, _config.internal_model_file_path);
_internal = make_unique<Inference>();
+ _label_manager = make_unique<LabelManager>(_config.label_file_path, _config.decision_threshold);
int ret = _internal->bind(_config.inference_engine_backend_type, _config.inference_target_device_type);
if (ret != MEDIA_VISION_ERROR_NONE)
void FaceRecognition::importLabel()
{
try {
- // Prepare can be called several times after initialization is done so previous data should be dropped.
- _label_manager.reset();
- _label_manager = make_unique<LabelManager>(_config.label_file_path, _config.decision_threshold);
-
// Update label manager from a given label file.
int cnt = _label_manager->importLabel();
// TODO. consider data augmentation.
try {
+ // 1. Store a new label name to label file if the given label doesn't exist.
+ if (!_label_manager->isExist(label_name))
+ _label_manager->addLabelToFile(label_name);
+
// Import label data from a label file.
importLabel();
- // 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;
- }
-
// Get label index and count.
unsigned int label_idx = _label_manager->getLabelIndex(label_name);
unsigned int label_cnt = _label_manager->getMaxLabel();
_result.raw_data.clear();
copy(raw_buffer, raw_buffer + internal_output_buffer->size / sizeof(float), back_inserter(_result.raw_data));
+
+ _result.labels.clear();
+ _result.labels = _label_manager->getLabels();
_status = WorkingStatus::INFERENCED;
return getAnswer();
// Deleting a given label is to remove existing registered person from label and feature vector files.
try {
- // Import label data from a label file.
- importLabel();
-
if (!_label_manager->isExist(label_name)) {
LOGE("%s doesn't exist in label file.", label_name.c_str());
return MEDIA_VISION_ERROR_INVALID_OPERATION;
}
+ // Import label data from a label file.
+ importLabel();
+
unsigned int target_label_idx = _label_manager->getLabelIndex(label_name);
auto label_cnt_ori = _label_manager->getMaxLabel();
if (!_result.is_valid)
throw NoData("Inference result not ready yet.");
- importLabel();
-
if (!_label_manager)
throw NoData("Label file doesn't exist.");
try {
- _result.labels = _label_manager->getLabels();
_label_manager->getLabelString(_result.label, _result.label_idx);
} catch (const BaseException &e) {
LOGE("%s", e.what());
void LabelManager::getLabelString(string &label, const int idx)
{
- importLabel();
-
if (idx < 0 || _labels.size() <= static_cast<size_t>(idx))
throw InvalidOperation("A given label index is invalid.");
label = _labels[idx];
}
-unsigned int LabelManager::addLabelToFile(string given_label)
+void LabelManager::addLabelToFile(string given_label)
{
ofstream writeFile;
given_label += "\n";
writeFile.write(given_label.c_str(), static_cast<streamsize>(given_label.size()));
writeFile.close();
-
- return getMaxLabel();
}
size_t LabelManager::importLabel(void)
{
- // label count is 0 if lael file doesn't exist.
+ // label count is 0 if label file doesn't exist.
if (!FaceRecogUtil::isFileExist(_label_file))
return 0;
bool LabelManager::isExist(const string given_label)
{
+ // return false if label file doesn't exist.
+ if (!FaceRecogUtil::isFileExist(_label_file))
+ return false;
+
return (find(_labels.begin(), _labels.end(), given_label) != _labels.end());
}
size_t LabelManager::getMaxLabel()
{
- importLabel();
-
return _labels.size();
}
RemoveModelResources();
}
+TEST(FaceRecognitionTest, LabelUpdateAfterInferenceShouldBeOk)
+{
+ mv_face_recognition_h handle;
+ vector<string> image_names = { "037830.png", "038965.png", "045978.png", "061310.png", "062261.png",
+ "029342.png", "000928.png", "008922.png", "054616.png" };
+ vector<string> label_names = { "2929", "2929", "2929", "7779", "7779", "7779", "3448", "3448", "3448" };
+
+ int ret = mv_face_recognition_create(&handle);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ ret = mv_face_recognition_prepare(handle);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ // Register two labels.
+ for (size_t idx = 0; idx < image_names.size() - 3; ++idx) {
+ const string image_path = string(TRAINING_IMAGE_PATH) + image_names[idx];
+ mv_source_h mv_source = NULL;
+
+ int ret = mv_create_source(&mv_source);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ ret = ImageHelper::loadImageToSource(image_path.c_str(), mv_source);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ ret = mv_face_recognition_register(handle, mv_source, label_names[idx].c_str());
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ ret = mv_face_recognition_inference(handle, mv_source);
+ if (ret != MEDIA_VISION_ERROR_NO_DATA) {
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+ }
+
+ ret = mv_destroy_source(mv_source);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+ }
+
+ size_t num_of_confidences = 0;
+ const float *confidences = nullptr;
+
+ ret = mv_face_recognition_get_confidence(handle, &confidences, &num_of_confidences);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ // num_of_confidences must be 2.
+ ASSERT_EQ(num_of_confidences, 2);
+
+ // Register other one.
+ for (size_t idx = image_names.size() - 3; idx < image_names.size(); ++idx) {
+ const string image_path = string(TRAINING_IMAGE_PATH) + image_names[idx];
+ mv_source_h mv_source = NULL;
+
+ int ret = mv_create_source(&mv_source);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ ret = ImageHelper::loadImageToSource(image_path.c_str(), mv_source);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ ret = mv_face_recognition_register(handle, mv_source, label_names[idx].c_str());
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ size_t num_of_confidences = 0;
+ const float *confidences = nullptr;
+
+ ret = mv_face_recognition_get_confidence(handle, &confidences, &num_of_confidences);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ // num_of_confidence must be 2 because of no inference request.
+ ASSERT_EQ(num_of_confidences, 2);
+
+ // If input is last one then request an inference.
+ if (idx == image_names.size() - 1) {
+ ret = mv_face_recognition_inference(handle, mv_source);
+ if (ret != MEDIA_VISION_ERROR_NO_DATA) {
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+ }
+
+ ret = mv_face_recognition_get_confidence(handle, &confidences, &num_of_confidences);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ // num_of_confidence must be 3 now.
+ ASSERT_EQ(num_of_confidences, 3);
+ }
+
+ ret = mv_destroy_source(mv_source);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+ }
+
+ ret = mv_face_recognition_destroy(handle);
+ ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+ RemoveModelResources();
+}
+
TEST(FaceRecognitionTest, GetLabelWithoutInferenceShouldBeError)
{
mv_face_recognition_h handle;