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.
19 #include "machine_learning_exception.h"
20 #include "face_recognition_fvm.h"
23 using namespace mediavision::machine_learning::exception;
25 FaceRecognitionFVM::FaceRecognitionFVM(const string feature_vector_file) : FeatureVectorManager(feature_vector_file)
28 void FaceRecognitionFVM::writeHeader(size_t feature_size, size_t label_cnt, unsigned int data_set_cnt)
30 ofstream outFile { _feature_vector_file, ios::out | ios::binary | ios::app };
32 if (!outFile.is_open())
33 throw InvalidOperation("fail to open a file");
35 FeaVecHeader fvHeader { FeatureVectorManager::feature_vector_signature, feature_size, label_cnt, data_set_cnt };
37 outFile.write((char *) &fvHeader, sizeof(FeaVecHeader));
40 void FaceRecognitionFVM::storeData(vector<vector<float> > &features_vec, vector<unsigned int> &label_index)
42 ofstream outFile { _feature_vector_file, ios::out | ios::binary | ios::app };
44 if (!outFile.is_open())
45 throw InvalidOperation("fail to open a file.");
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));
53 void FaceRecognitionFVM::remove()
55 // Remove existing file forcely.
56 int ret = ::remove(_feature_vector_file.c_str());
58 throw InvalidOperation("Fail to remove feature vector file.");