0ba0be4e9e8c596337649502991ad87163489640
[platform/core/api/mediavision.git] / mv_machine_learning / face_recognition / include / face_recognition.h
1 /**
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef __FACE_RECOGNITION_H__
18 #define __FACE_RECOGNITION_H__
19
20 #include <unordered_map>
21 #include <mv_common.h>
22 #include <mv_inference_type.h>
23
24 #include "training_engine_error.h"
25 #include "training_engine_common_impl.h"
26 #include "inference_engine_common_impl.h"
27 #include "Inference.h"
28 #include "label_manager.h"
29 #include "backbone_model_info.h"
30 #include "feature_vector_manager.h"
31 #include "data_set_manager.h"
32 #include "simple_shot.h"
33
34 namespace mediavision
35 {
36 namespace machine_learning
37 {
38 namespace face_recognition
39 {
40 namespace status
41 {
42 enum { NONE = 0, INITIALIZED, REGISTERED, INFERENCED, DELETED };
43 } // status
44
45 namespace mode
46 {
47 enum { REGISTER = 0, INFERENCE, DELETE };
48 } // mode
49
50 } // face_recognition
51
52 struct face_recognition_register_input_s {
53         std::vector<float> data;
54         std::string label;
55 };
56
57 struct mv_face_recognition_input_s {
58         unsigned int mode;
59         std::vector<face_recognition_register_input_s> register_src;
60         std::vector<std::vector<float> > inputs;
61         std::vector<std::string> labels;
62 };
63
64 /**
65  * @brief The face recognition result structure.
66  * @details Contains face recognition result such as label, label index, raw data,
67  *          and raw data count.
68  */
69 struct mv_face_recognition_result_s {
70         unsigned int label_idx; /**< label index of label file. */
71         std::vector<float> raw_data; /**< raw data to each label. */
72         std::string label; /**< label string. */
73         bool is_valid; /**< inference result is valid or not. */
74 };
75
76 struct FaceRecognitionConfig {
77         training_target_type_e training_target_device_type;
78         training_backend_type_e training_engine_backend_type;
79         mv_inference_target_device_e inference_target_device_type;
80         mv_inference_backend_type_e inference_engine_backend_type;
81         std::string internal_model_file_path;
82         std::string label_file_path;
83         std::string feature_vector_file_path;
84         double decision_threshold;
85         std::vector<size_t> input_tensor_shape;
86 };
87
88 class FaceRecognition
89 {
90 private:
91         unsigned int _status;
92         std::unique_ptr<mediavision::inference::Inference> _internal;
93         std::unique_ptr<mediavision::inference::Inference> _backbone;
94         std::unique_ptr<IBackboneModelInfo> _backbone_model_info;
95         std::unique_ptr<TrainingModel> _training_model;
96         std::unique_ptr<LabelManager> _label_manager;
97         FaceRecognitionConfig _config;
98         mv_face_recognition_result_s _result;
99
100         // FYI. This function should be called every time a new face is registered.
101         void ImportLabel();
102         void CheckFeatureVectorFile(std::string fv_file_name, std::string new_fv_file_name);
103         void StoreDataSet(std::unique_ptr<DataSetManager> &data_set, unsigned int label_cnt);
104         int GetAnswer();
105         std::vector<model_layer_info> &GetBackboneInputLayerInfo();
106         int GetVecFromMvSource(mv_source_h img_src, std::vector<float> &out_vec);
107
108 public:
109         FaceRecognition();
110         ~FaceRecognition();
111
112         int Initialize();
113         void SetConfig(FaceRecognitionConfig &config);
114         int RegisterNewFace(std::vector<float> &input_vec, std::string label_name);
115         int RecognizeFace(std::vector<float> &input_vec);
116         int DeleteLabel(std::string label_name);
117         int GetLabel(const char **out_label);
118         mv_face_recognition_result_s &result();
119 };
120
121 } // machine_learning
122 } // mediavision
123
124 #endif