mv_machine_learning: use carnel notation
[platform/core/api/mediavision.git] / mv_machine_learning / face_recognition / src / face_recognition_fvm.cpp
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 #include <fstream>
18
19 #include "machine_learning_exception.h"
20 #include "face_recognition_fvm.h"
21
22 using namespace std;
23 using namespace mediavision::machine_learning::exception;
24
25 FaceRecognitionFVM::FaceRecognitionFVM(const string feature_vector_file) : FeatureVectorManager(feature_vector_file)
26 {}
27
28 void FaceRecognitionFVM::writeHeader(size_t feature_size, size_t label_cnt, unsigned int data_set_cnt)
29 {
30         ofstream outFile { _feature_vector_file, ios::out | ios::binary | ios::app };
31
32         if (!outFile.is_open())
33                 throw InvalidOperation("fail to open a file");
34
35         FeaVecHeader fvHeader { FeatureVectorManager::feature_vector_signature, feature_size, label_cnt, data_set_cnt };
36
37         outFile.write((char *) &fvHeader, sizeof(FeaVecHeader));
38 }
39
40 void FaceRecognitionFVM::storeData(vector<vector<float> > &features_vec, vector<unsigned int> &label_index)
41 {
42         ofstream outFile { _feature_vector_file, ios::out | ios::binary | ios::app };
43
44         if (!outFile.is_open())
45                 throw InvalidOperation("fail to open a file.");
46
47         for (size_t idx = 0; idx < features_vec.size(); ++idx) {
48                 outFile.write(reinterpret_cast<char *>(features_vec[idx].data()), features_vec[idx].size() * sizeof(float));
49                 outFile.write(reinterpret_cast<char *>(&label_index[idx]), sizeof(unsigned int));
50         }
51 }
52
53 void FaceRecognitionFVM::remove()
54 {
55         // Remove existing file forcely.
56         int ret = ::remove(_feature_vector_file.c_str());
57         if (ret)
58                 throw InvalidOperation("Fail to remove feature vector file.");
59 }