4de8a124a9f9a694897c3b9d0320768f039270a9
[platform/core/api/mediavision.git] / mv_machine_learning / training / src / feature_vector_manager.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 <opencv2/opencv.hpp>
18 #include <opencv2/imgproc/imgproc.hpp>
19
20 #include "machine_learning_exception.h"
21 #include "feature_vector_manager.h"
22
23 using namespace std;
24 using namespace mediavision::machine_learning::exception;
25
26 FeatureVectorManager::FeatureVectorManager(const string feature_vector_file) : _feature_vector_file(feature_vector_file)
27 {}
28
29 const string &FeatureVectorManager::GetFileName()
30 {
31         return _feature_vector_file;
32 }
33
34 void FeatureVectorManager::GetVecFromImg(const string image_file, vector<float> &vec, unsigned int width,
35                                                                                  unsigned int height)
36 {
37         cv::Mat src, dst;
38
39         if (!FaceRecogUtil::IsImageFile(image_file))
40                 throw InvalidOperation("Invalid image file.");
41
42         src = cv::imread(image_file);
43
44         if (!src.data)
45                 throw InvalidOperation("Invalid image data.");
46
47         cv::cvtColor(src, src, cv::COLOR_BGR2RGB);
48
49         cv::Mat resized;
50
51         resize(src, resized, cv::Size(width, height), 0, 0, cv::INTER_CUBIC);
52
53         cv::Mat floatSrc;
54
55         resized.convertTo(floatSrc, CV_32FC3);
56
57         cv::Mat meaned = cv::Mat(floatSrc.size(), CV_32FC3, cv::Scalar(127.5f, 127.5f, 127.5f));
58
59         cv::subtract(floatSrc, meaned, dst);
60         dst /= 127.5f;
61
62         vec.assign((float *) dst.data, (float *) dst.data + dst.total() * dst.channels());
63 }
64
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)
67 {
68         cv::Mat cvSrc = cv::Mat(cv::Size(width, height), CV_MAKETYPE(CV_8U, 3), in_data).clone();
69
70         cv::Mat resized;
71
72         resize(cvSrc, resized, cv::Size(re_width, re_height), 0, 0, cv::INTER_CUBIC);
73
74         cv::Mat floatSrc;
75
76         resized.convertTo(floatSrc, CV_32FC3);
77
78         cv::Mat meaned = cv::Mat(floatSrc.size(), CV_32FC3, cv::Scalar(127.5f, 127.5f, 127.5f));
79         cv::Mat dst;
80
81         cv::subtract(floatSrc, meaned, dst);
82         dst /= 127.5f;
83
84         vec.assign((float *) dst.data, (float *) dst.data + dst.total() * dst.channels());
85 }
86
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)
89 {
90         cv::Mat argb(cv::Size(in_width, in_height), CV_8UC4, in_data);
91
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] };
95         cv::Mat rgb;
96         cv::merge(splitted, 3, rgb);
97
98         cv::Mat resized;
99
100         resize(rgb, resized, cv::Size(re_width, re_height), 0, 0, cv::INTER_CUBIC);
101
102         cv::Mat floatSrc;
103
104         resized.convertTo(floatSrc, CV_32FC3);
105
106         cv::Mat meaned = cv::Mat(floatSrc.size(), CV_32FC3, cv::Scalar(127.5f, 127.5f, 127.5f));
107
108         cv::Mat dst;
109
110         cv::subtract(floatSrc, meaned, dst);
111         dst /= 127.5f;
112
113         vec.assign((float *) dst.data, (float *) dst.data + dst.total() * dst.channels());
114 }