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.
17 #include <opencv2/opencv.hpp>
18 #include <opencv2/imgproc/imgproc.hpp>
20 #include "machine_learning_exception.h"
21 #include "feature_vector_manager.h"
24 using namespace mediavision::machine_learning::exception;
26 FeatureVectorManager::FeatureVectorManager(const string feature_vector_file) : _feature_vector_file(feature_vector_file)
29 const string &FeatureVectorManager::GetFileName()
31 return _feature_vector_file;
34 void FeatureVectorManager::GetVecFromImg(const string image_file, vector<float> &vec, unsigned int width,
39 if (!FaceRecogUtil::IsImageFile(image_file))
40 throw InvalidOperation("Invalid image file.");
42 src = cv::imread(image_file);
45 throw InvalidOperation("Invalid image data.");
47 cv::cvtColor(src, src, cv::COLOR_BGR2RGB);
51 resize(src, resized, cv::Size(width, height), 0, 0, cv::INTER_CUBIC);
55 resized.convertTo(floatSrc, CV_32FC3);
57 cv::Mat meaned = cv::Mat(floatSrc.size(), CV_32FC3, cv::Scalar(127.5f, 127.5f, 127.5f));
59 cv::subtract(floatSrc, meaned, dst);
62 vec.assign((float *) dst.data, (float *) dst.data + dst.total() * dst.channels());
65 void FeatureVectorManager::GetVecFromRGB(unsigned char *in_data, vector<float> &vec, unsigned int width,
66 unsigned int height, size_t re_width, size_t re_height)
68 cv::Mat cvSrc = cv::Mat(cv::Size(width, height), CV_MAKETYPE(CV_8U, 3), in_data).clone();
72 resize(cvSrc, resized, cv::Size(re_width, re_height), 0, 0, cv::INTER_CUBIC);
76 resized.convertTo(floatSrc, CV_32FC3);
78 cv::Mat meaned = cv::Mat(floatSrc.size(), CV_32FC3, cv::Scalar(127.5f, 127.5f, 127.5f));
81 cv::subtract(floatSrc, meaned, dst);
84 vec.assign((float *) dst.data, (float *) dst.data + dst.total() * dst.channels());
87 void FeatureVectorManager::GetVecFromXRGB(unsigned char *in_data, vector<float> &vec, unsigned int in_width,
88 unsigned int in_height, unsigned int re_width, unsigned int re_height)
90 cv::Mat argb(cv::Size(in_width, in_height), CV_8UC4, in_data);
92 cv::Mat split_rgbx[4];
93 cv::split(argb, split_rgbx);
94 cv::Mat splitted[] = { split_rgbx[0], split_rgbx[1], split_rgbx[2] };
96 cv::merge(splitted, 3, rgb);
100 resize(rgb, resized, cv::Size(re_width, re_height), 0, 0, cv::INTER_CUBIC);
104 resized.convertTo(floatSrc, CV_32FC3);
106 cv::Mat meaned = cv::Mat(floatSrc.size(), CV_32FC3, cv::Scalar(127.5f, 127.5f, 127.5f));
110 cv::subtract(floatSrc, meaned, dst);
113 vec.assign((float *) dst.data, (float *) dst.data + dst.total() * dst.channels());