mv_machine_learning: rename Preprocess header file
authorInki Dae <inki.dae@samsung.com>
Mon, 24 Jul 2023 00:38:53 +0000 (09:38 +0900)
committerKwanghoon Son <k.son@samsung.com>
Mon, 7 Aug 2023 04:25:06 +0000 (13:25 +0900)
[Issue type] : code cleanup

Rename Preprocess.h to machine_learning_preprocess.h.
The header file name, Preprocess, is a generic one so let's change
the file name with specific one.

Change-Id: Ic497702eb27c00172159801757c16d1faf6896a4
Signed-off-by: Inki Dae <inki.dae@samsung.com>
mv_machine_learning/face_recognition/include/facenet.h
mv_machine_learning/image_classification/include/image_classification.h
mv_machine_learning/landmark_detection/include/landmark_detection.h
mv_machine_learning/meta/CMakeLists.txt
mv_machine_learning/meta/include/Preprocess.h [deleted file]
mv_machine_learning/meta/include/machine_learning_preprocess.h [new file with mode: 0644]
mv_machine_learning/meta/src/Preprocess.cpp [deleted file]
mv_machine_learning/meta/src/machine_learning_preprocess.cpp [new file with mode: 0644]
mv_machine_learning/object_detection/include/object_detection.h
mv_machine_learning/object_detection_3d/include/object_detection_3d.h
packaging/capi-media-vision.spec

index c15e8d5e1a008b0e21d79c9403e65b24f8e9de82..5602db84ca7aedeb173f132cc2c4266cfb04eeb8 100644 (file)
@@ -26,7 +26,7 @@
 #include "Inference.h"
 #include "facenet_parser.h"
 #include "face_recognition_type.h"
-#include "Preprocess.h"
+#include "machine_learning_preprocess.h"
 
 namespace mediavision
 {
index 1d9c9da5c2cae1fcdb791422ad3991b143f326de..110c7e28ca3b5fbbe5ebc6a11455011a6f989959 100644 (file)
@@ -26,7 +26,7 @@
 #include "Inference.h"
 #include "image_classification_type.h"
 #include "ImageClassificationParser.h"
-#include "Preprocess.h"
+#include "machine_learning_preprocess.h"
 
 namespace mediavision
 {
index 9d3c8f63c44775ec66d9393a9265ed94c1a13232..a73520d945ddc82a4fc276dc0d781cf68e469b6b 100644 (file)
@@ -26,7 +26,7 @@
 #include "Inference.h"
 #include "landmark_detection_type.h"
 #include "LandmarkDetectionParser.h"
-#include "Preprocess.h"
+#include "machine_learning_preprocess.h"
 
 namespace mediavision
 {
index 03f842befe14fca67b1a187b5267e6bb036b724e..3c427bb637e236489c841c70719eae03d53496ee 100644 (file)
@@ -4,5 +4,5 @@ cmake_minimum_required(VERSION 2.6...3.13)
 install(
     DIRECTORY ${PROJECT_SOURCE_DIR}/include/ DESTINATION include/media
     FILES_MATCHING
-    PATTERN "Preprocess.h"
+    PATTERN "machine_learning_preprocess.h"
     )
\ No newline at end of file
diff --git a/mv_machine_learning/meta/include/Preprocess.h b/mv_machine_learning/meta/include/Preprocess.h
deleted file mode 100644 (file)
index 4ef3dd0..0000000
+++ /dev/null
@@ -1,94 +0,0 @@
-/**
- * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef __PREPROCESS_H__
-#define __PREPORCESS_H__
-
-#include <string>
-#include <map>
-#include <memory>
-#include <vector>
-
-#include <inference_engine_type.h>
-#include <mv_inference_type.h>
-
-#include "mv_private.h"
-#include "mv_common.h"
-
-#include <opencv2/core.hpp>
-#include <opencv2/imgproc.hpp>
-
-/**
- * @file Preprocess.h
- * @brief This file contains the Preprocess class definition which
- *        provides Preprocess before running inference.
- */
-
-namespace mediavision
-{
-namespace machine_learning
-{
-struct PreprocessConfig {
-       bool skip_csc {}; /**< It indicates whether color space conversion operation should be skipped or not. */
-       mv_colorspace_e output_format {}; /**< The pixel format of output tensor to be converted. */
-       mv_inference_data_type_e output_data_type {}; /**< The data type of output tensor to be converted. */
-       int output_channel {}; /** The channel size of output tensor te be converted. */
-       int output_width {}; /** The width size of output tensor to be converted. */
-       int output_height {}; /** The height size of output tensor to be converted. */
-       bool normalize {}; /**< It indicates whether normalization to input data should be performed or not. */
-       std::vector<double> mean;
-       std::vector<double> std;
-       bool quantize {}; /**< It indicates whether quantization to input data should be performed or not. */
-       std::vector<double> scale;
-       std::vector<double> zeropoint;
-};
-
-class Preprocess
-{
-public:
-       Preprocess()
-       {}
-       ~Preprocess() = default;
-
-       void setConfig(const PreprocessConfig &config);
-       template<typename T> void run(mv_source_h &mv_src, std::vector<T> &inputVector);
-
-       std::vector<unsigned int> &getImageWidth()
-       {
-               return _vImageWidth;
-       }
-       std::vector<unsigned int> &getImageHeight()
-       {
-               return _vImageHeight;
-       }
-
-private:
-       std::vector<unsigned int> _vImageWidth;
-       std::vector<unsigned int> _vImageHeight;
-       PreprocessConfig _config;
-
-       int convertToCv(int given_type, int ch);
-       void colorConvert(cv::Mat &source, cv::Mat &dest, int sType, int dType);
-       void convertToCvSource(std::vector<mv_source_h> &mv_srcs, std::vector<cv::Mat> &cv_srcs);
-       void normalize(cv::Mat &source, cv::Mat &dest, const std::vector<double> &mean, const std::vector<double> &std);
-       void quantize(cv::Mat &source, cv::Mat &dest, const std::vector<double> &scale,
-                                 const std::vector<double> &zeropoint);
-};
-
-} /* machine_learning */
-} /* mediavision */
-
-#endif /* __PREPROCESS_H__ */
diff --git a/mv_machine_learning/meta/include/machine_learning_preprocess.h b/mv_machine_learning/meta/include/machine_learning_preprocess.h
new file mode 100644 (file)
index 0000000..080a39c
--- /dev/null
@@ -0,0 +1,94 @@
+/**
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MACHINE_LEARNING_PREPROCESS_H__
+#define __MACHINE_LEARNING_PREPROCESS_H__
+
+#include <string>
+#include <map>
+#include <memory>
+#include <vector>
+
+#include <inference_engine_type.h>
+#include <mv_inference_type.h>
+
+#include "mv_private.h"
+#include "mv_common.h"
+
+#include <opencv2/core.hpp>
+#include <opencv2/imgproc.hpp>
+
+/**
+ * @file machine_learning_preprocess.h
+ * @brief This file contains the Preprocess class definition which
+ *        provides Preprocess before running inference.
+ */
+
+namespace mediavision
+{
+namespace machine_learning
+{
+struct PreprocessConfig {
+       bool skip_csc {}; /**< It indicates whether color space conversion operation should be skipped or not. */
+       mv_colorspace_e output_format {}; /**< The pixel format of output tensor to be converted. */
+       mv_inference_data_type_e output_data_type {}; /**< The data type of output tensor to be converted. */
+       int output_channel {}; /** The channel size of output tensor te be converted. */
+       int output_width {}; /** The width size of output tensor to be converted. */
+       int output_height {}; /** The height size of output tensor to be converted. */
+       bool normalize {}; /**< It indicates whether normalization to input data should be performed or not. */
+       std::vector<double> mean;
+       std::vector<double> std;
+       bool quantize {}; /**< It indicates whether quantization to input data should be performed or not. */
+       std::vector<double> scale;
+       std::vector<double> zeropoint;
+};
+
+class Preprocess
+{
+public:
+       Preprocess()
+       {}
+       ~Preprocess() = default;
+
+       void setConfig(const PreprocessConfig &config);
+       template<typename T> void run(mv_source_h &mv_src, std::vector<T> &inputVector);
+
+       std::vector<unsigned int> &getImageWidth()
+       {
+               return _vImageWidth;
+       }
+       std::vector<unsigned int> &getImageHeight()
+       {
+               return _vImageHeight;
+       }
+
+private:
+       std::vector<unsigned int> _vImageWidth;
+       std::vector<unsigned int> _vImageHeight;
+       PreprocessConfig _config;
+
+       int convertToCv(int given_type, int ch);
+       void colorConvert(cv::Mat &source, cv::Mat &dest, int sType, int dType);
+       void convertToCvSource(std::vector<mv_source_h> &mv_srcs, std::vector<cv::Mat> &cv_srcs);
+       void normalize(cv::Mat &source, cv::Mat &dest, const std::vector<double> &mean, const std::vector<double> &std);
+       void quantize(cv::Mat &source, cv::Mat &dest, const std::vector<double> &scale,
+                                 const std::vector<double> &zeropoint);
+};
+
+} /* machine_learning */
+} /* mediavision */
+
+#endif /* __MACHINE_LEARNING_PREPROCESS_H__ */
diff --git a/mv_machine_learning/meta/src/Preprocess.cpp b/mv_machine_learning/meta/src/Preprocess.cpp
deleted file mode 100644 (file)
index d611e06..0000000
+++ /dev/null
@@ -1,201 +0,0 @@
-/**
- * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <unistd.h>
-#include <fstream>
-#include <string>
-#include <queue>
-#include <algorithm>
-#include "Preprocess.h"
-#include "machine_learning_exception.h"
-
-constexpr int colorConvertTable[][12] = {
-       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
-       { 0, -1, 0, 0, 0, 0, 0, 0, 0, cv::COLOR_GRAY2BGR565, cv::COLOR_GRAY2RGB, cv::COLOR_GRAY2RGBA },
-       { 0, cv::COLOR_YUV2GRAY_I420, -1, 0, 0, 0, 0, 0, 0, 0, cv::COLOR_RGBA2GRAY, cv::COLOR_YUV2RGBA_I420 },
-       { 0, cv::COLOR_YUV2GRAY_NV12, 0, -1, 0, 0, 0, 0, 0, 0, cv::COLOR_YUV2RGB_NV12, cv::COLOR_YUV2RGBA_NV12 },
-       { 0, cv::COLOR_YUV2GRAY_YV12, 0, 0, -1, 0, 0, 0, 0, 0, cv::COLOR_YUV2RGB_YV12, cv::COLOR_YUV2RGBA_YV12 },
-       { 0, cv::COLOR_YUV2GRAY_NV21, 0, 0, 0, -1, 0, 0, 0, 0, cv::COLOR_YUV2RGB_NV21, cv::COLOR_YUV2RGBA_NV21 },
-       { 0, cv::COLOR_YUV2GRAY_YUYV, 0, 0, 0, 0, -1, 0, 0, 0, cv::COLOR_YUV2RGB_YUYV, cv::COLOR_YUV2RGBA_YUYV },
-       { 0, cv::COLOR_YUV2GRAY_UYVY, 0, 0, 0, 0, 0, -1, 0, 0, cv::COLOR_YUV2BGR_UYVY, cv::COLOR_YUV2BGRA_UYVY },
-       { 0, cv::COLOR_YUV2GRAY_Y422, 0, 0, 0, 0, 0, 0, -1, 0, cv::COLOR_YUV2RGB_Y422, cv::COLOR_YUV2RGBA_Y422 },
-       { 0, cv::COLOR_BGR5652GRAY, 0, 0, 0, 0, 0, 0, 0, -1, cv::COLOR_BGR5652BGR, cv::COLOR_BGR5652BGRA },
-       { 0, cv::COLOR_RGB2GRAY, 0, 0, 0, 0, 0, 0, 0, 0, -1, cv::COLOR_RGB2RGBA },
-       { 0, cv::COLOR_RGBA2GRAY, 0, 0, 0, 0, 0, 0, 0, cv::COLOR_BGRA2BGR565, cv::COLOR_RGBA2RGB, -1 }
-};
-
-using namespace std;
-using namespace mediavision::machine_learning::exception;
-
-namespace mediavision
-{
-namespace machine_learning
-{
-void Preprocess::colorConvert(cv::Mat &source, cv::Mat &dest, int sType, int dType)
-{
-       LOGI("ENTER");
-
-       auto conversionColor = static_cast<int>(colorConvertTable[sType][dType]);
-       if (conversionColor == -1) { /* Don't need conversion */
-               dest = source;
-       } else if (conversionColor > 0) {
-               /* Class for representation the given image as cv::Mat before conversion */
-               cv::cvtColor(source, dest, conversionColor);
-       } else {
-               throw InvalidOperation("Fail to ColorConvert");
-       }
-
-       LOGI("LEAVE");
-}
-
-void Preprocess::normalize(cv::Mat &source, cv::Mat &dest, const vector<double> &mean, const vector<double> &std)
-{
-       LOGI("ENTER");
-
-       try {
-               cv::subtract(source, cv::Scalar(mean[0], mean[1], mean[2]), dest);
-               source = dest;
-               cv::divide(source, cv::Scalar(std[0], std[1], std[2]), dest);
-       } catch (cv::Exception &e) {
-               throw InvalidOperation("Fail to substract/divide");
-       }
-
-       LOGI("LEAVE");
-}
-
-void Preprocess::quantize(cv::Mat &source, cv::Mat &dest, const vector<double> &scale, const vector<double> &zeropoint)
-{
-       LOGI("ENTER");
-
-       try {
-               cv::subtract(source, cv::Scalar(zeropoint[0], zeropoint[1], zeropoint[2]), dest);
-               source = dest;
-               cv::multiply(source, cv::Scalar(scale[0], scale[1], scale[2]), dest);
-       } catch (cv::Exception &e) {
-               throw InvalidOperation("Fail to subtract/multiply");
-       }
-
-       LOGI("LEAVE");
-}
-
-int Preprocess::convertToCv(int given_type, int ch)
-{
-       int type = 0;
-
-       switch (given_type) {
-       case INFERENCE_TENSOR_DATA_TYPE_UINT8:
-       case MV_INFERENCE_DATA_UINT8:
-               LOGI("Type is %d ch with UINT8", ch);
-               type = ch == 1 ? CV_8UC1 : CV_8UC3;
-               break;
-       case INFERENCE_TENSOR_DATA_TYPE_FLOAT32:
-       case MV_INFERENCE_DATA_FLOAT32:
-               LOGI("Type is %d ch with FLOAT32", ch);
-               type = ch == 1 ? CV_32FC1 : CV_32FC3;
-               break;
-       default:
-               LOGI("unknown data type so FLOAT32 data type will be used in default");
-               type = ch == 1 ? CV_32FC1 : CV_32FC3;
-               break;
-       }
-
-       return type;
-}
-
-void Preprocess::convertToCvSource(vector<mv_source_h> &mv_srcs, vector<cv::Mat> &cv_srcs)
-{
-       for (auto &mv_src : mv_srcs) {
-               mv_colorspace_e colorspace = MEDIA_VISION_COLORSPACE_INVALID;
-               unsigned int width = 0, height = 0;
-               unsigned int bufferSize = 0;
-               unsigned char *buffer = NULL;
-
-               if (mv_source_get_width(mv_src, &width) != MEDIA_VISION_ERROR_NONE ||
-                       mv_source_get_height(mv_src, &height) != MEDIA_VISION_ERROR_NONE ||
-                       mv_source_get_colorspace(mv_src, &colorspace) != MEDIA_VISION_ERROR_NONE ||
-                       mv_source_get_buffer(mv_src, &buffer, &bufferSize))
-                       throw InvalidParameter("Invalid Parameter.");
-
-               _vImageWidth.push_back(width);
-               _vImageHeight.push_back(height);
-
-               // TODO. Let's support various color spaces.
-
-               if (colorspace != MEDIA_VISION_COLORSPACE_RGB888)
-                       throw InvalidOperation("Not Supported format.");
-
-               /* convert mv_source to cv::Mat */
-               cv::Mat cvSource = cv::Mat(cv::Size(width, height), CV_MAKETYPE(CV_8U, 3), buffer).clone();
-
-               cv_srcs.push_back(cvSource);
-               LOGI("Size: w:%u, h:%u", cvSource.size().width, cvSource.size().height);
-       }
-}
-
-void Preprocess::setConfig(const PreprocessConfig &config)
-{
-       _config = config;
-}
-
-template<typename T> void Preprocess::run(mv_source_h &mv_src, vector<T> &inputVector)
-{
-       LOGI("ENTER");
-
-       vector<cv::Mat> oriCvSources;
-       vector<mv_source_h> mv_srcs = { mv_src };
-
-       _vImageWidth.clear();
-       _vImageHeight.clear();
-       convertToCvSource(mv_srcs, oriCvSources);
-
-       inputVector.resize(_config.output_height * _config.output_width * _config.output_channel);
-
-       int data_type = convertToCv(_config.output_data_type, _config.output_channel);
-       // dest is a wrapper of the buffer.
-       cv::Mat dest(cv::Size(_config.output_width, _config.output_height), data_type, inputVector.data());
-       cv::Mat cvSource, cvDest;
-
-       // cvSource has new allocation with dest.size()
-       cv::resize(oriCvSources[0], cvSource, dest.size());
-
-       if (_config.skip_csc) {
-               cvSource.convertTo(dest, dest.type());
-       } else {
-               mv_colorspace_e colorspace = MEDIA_VISION_COLORSPACE_INVALID;
-               int ret = mv_source_get_colorspace(mv_srcs[0], &colorspace);
-               if (ret != MEDIA_VISION_ERROR_NONE)
-                       throw InvalidOperation("Fail to get color space.");
-
-               // cvDest is allocated if colorspace is not RGB888, and
-               // cvDest shares the data with cvSource if the colorspace is RGB888.
-               colorConvert(cvSource, cvDest, colorspace, _config.output_format);
-               cvDest.convertTo(dest, dest.type());
-       }
-
-       if (_config.normalize)
-               normalize(dest, dest, _config.mean, _config.std);
-
-       if (_config.quantize)
-               quantize(dest, dest, _config.scale, _config.zeropoint);
-
-       LOGI("LEAVE");
-}
-
-template void Preprocess::run<float>(mv_source_h &mv_src, vector<float> &inputVector);
-template void Preprocess::run<unsigned char>(mv_source_h &mv_src, vector<unsigned char> &inputVector);
-
-} /* machine_learning */
-} /* mediavision */
diff --git a/mv_machine_learning/meta/src/machine_learning_preprocess.cpp b/mv_machine_learning/meta/src/machine_learning_preprocess.cpp
new file mode 100644 (file)
index 0000000..919f70e
--- /dev/null
@@ -0,0 +1,201 @@
+/**
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <unistd.h>
+#include <fstream>
+#include <string>
+#include <queue>
+#include <algorithm>
+#include "machine_learning_preprocess.h"
+#include "machine_learning_exception.h"
+
+constexpr int colorConvertTable[][12] = {
+       { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
+       { 0, -1, 0, 0, 0, 0, 0, 0, 0, cv::COLOR_GRAY2BGR565, cv::COLOR_GRAY2RGB, cv::COLOR_GRAY2RGBA },
+       { 0, cv::COLOR_YUV2GRAY_I420, -1, 0, 0, 0, 0, 0, 0, 0, cv::COLOR_RGBA2GRAY, cv::COLOR_YUV2RGBA_I420 },
+       { 0, cv::COLOR_YUV2GRAY_NV12, 0, -1, 0, 0, 0, 0, 0, 0, cv::COLOR_YUV2RGB_NV12, cv::COLOR_YUV2RGBA_NV12 },
+       { 0, cv::COLOR_YUV2GRAY_YV12, 0, 0, -1, 0, 0, 0, 0, 0, cv::COLOR_YUV2RGB_YV12, cv::COLOR_YUV2RGBA_YV12 },
+       { 0, cv::COLOR_YUV2GRAY_NV21, 0, 0, 0, -1, 0, 0, 0, 0, cv::COLOR_YUV2RGB_NV21, cv::COLOR_YUV2RGBA_NV21 },
+       { 0, cv::COLOR_YUV2GRAY_YUYV, 0, 0, 0, 0, -1, 0, 0, 0, cv::COLOR_YUV2RGB_YUYV, cv::COLOR_YUV2RGBA_YUYV },
+       { 0, cv::COLOR_YUV2GRAY_UYVY, 0, 0, 0, 0, 0, -1, 0, 0, cv::COLOR_YUV2BGR_UYVY, cv::COLOR_YUV2BGRA_UYVY },
+       { 0, cv::COLOR_YUV2GRAY_Y422, 0, 0, 0, 0, 0, 0, -1, 0, cv::COLOR_YUV2RGB_Y422, cv::COLOR_YUV2RGBA_Y422 },
+       { 0, cv::COLOR_BGR5652GRAY, 0, 0, 0, 0, 0, 0, 0, -1, cv::COLOR_BGR5652BGR, cv::COLOR_BGR5652BGRA },
+       { 0, cv::COLOR_RGB2GRAY, 0, 0, 0, 0, 0, 0, 0, 0, -1, cv::COLOR_RGB2RGBA },
+       { 0, cv::COLOR_RGBA2GRAY, 0, 0, 0, 0, 0, 0, 0, cv::COLOR_BGRA2BGR565, cv::COLOR_RGBA2RGB, -1 }
+};
+
+using namespace std;
+using namespace mediavision::machine_learning::exception;
+
+namespace mediavision
+{
+namespace machine_learning
+{
+void Preprocess::colorConvert(cv::Mat &source, cv::Mat &dest, int sType, int dType)
+{
+       LOGI("ENTER");
+
+       auto conversionColor = static_cast<int>(colorConvertTable[sType][dType]);
+       if (conversionColor == -1) { /* Don't need conversion */
+               dest = source;
+       } else if (conversionColor > 0) {
+               /* Class for representation the given image as cv::Mat before conversion */
+               cv::cvtColor(source, dest, conversionColor);
+       } else {
+               throw InvalidOperation("Fail to ColorConvert");
+       }
+
+       LOGI("LEAVE");
+}
+
+void Preprocess::normalize(cv::Mat &source, cv::Mat &dest, const vector<double> &mean, const vector<double> &std)
+{
+       LOGI("ENTER");
+
+       try {
+               cv::subtract(source, cv::Scalar(mean[0], mean[1], mean[2]), dest);
+               source = dest;
+               cv::divide(source, cv::Scalar(std[0], std[1], std[2]), dest);
+       } catch (cv::Exception &e) {
+               throw InvalidOperation("Fail to substract/divide");
+       }
+
+       LOGI("LEAVE");
+}
+
+void Preprocess::quantize(cv::Mat &source, cv::Mat &dest, const vector<double> &scale, const vector<double> &zeropoint)
+{
+       LOGI("ENTER");
+
+       try {
+               cv::subtract(source, cv::Scalar(zeropoint[0], zeropoint[1], zeropoint[2]), dest);
+               source = dest;
+               cv::multiply(source, cv::Scalar(scale[0], scale[1], scale[2]), dest);
+       } catch (cv::Exception &e) {
+               throw InvalidOperation("Fail to subtract/multiply");
+       }
+
+       LOGI("LEAVE");
+}
+
+int Preprocess::convertToCv(int given_type, int ch)
+{
+       int type = 0;
+
+       switch (given_type) {
+       case INFERENCE_TENSOR_DATA_TYPE_UINT8:
+       case MV_INFERENCE_DATA_UINT8:
+               LOGI("Type is %d ch with UINT8", ch);
+               type = ch == 1 ? CV_8UC1 : CV_8UC3;
+               break;
+       case INFERENCE_TENSOR_DATA_TYPE_FLOAT32:
+       case MV_INFERENCE_DATA_FLOAT32:
+               LOGI("Type is %d ch with FLOAT32", ch);
+               type = ch == 1 ? CV_32FC1 : CV_32FC3;
+               break;
+       default:
+               LOGI("unknown data type so FLOAT32 data type will be used in default");
+               type = ch == 1 ? CV_32FC1 : CV_32FC3;
+               break;
+       }
+
+       return type;
+}
+
+void Preprocess::convertToCvSource(vector<mv_source_h> &mv_srcs, vector<cv::Mat> &cv_srcs)
+{
+       for (auto &mv_src : mv_srcs) {
+               mv_colorspace_e colorspace = MEDIA_VISION_COLORSPACE_INVALID;
+               unsigned int width = 0, height = 0;
+               unsigned int bufferSize = 0;
+               unsigned char *buffer = NULL;
+
+               if (mv_source_get_width(mv_src, &width) != MEDIA_VISION_ERROR_NONE ||
+                       mv_source_get_height(mv_src, &height) != MEDIA_VISION_ERROR_NONE ||
+                       mv_source_get_colorspace(mv_src, &colorspace) != MEDIA_VISION_ERROR_NONE ||
+                       mv_source_get_buffer(mv_src, &buffer, &bufferSize))
+                       throw InvalidParameter("Invalid Parameter.");
+
+               _vImageWidth.push_back(width);
+               _vImageHeight.push_back(height);
+
+               // TODO. Let's support various color spaces.
+
+               if (colorspace != MEDIA_VISION_COLORSPACE_RGB888)
+                       throw InvalidOperation("Not Supported format.");
+
+               /* convert mv_source to cv::Mat */
+               cv::Mat cvSource = cv::Mat(cv::Size(width, height), CV_MAKETYPE(CV_8U, 3), buffer).clone();
+
+               cv_srcs.push_back(cvSource);
+               LOGI("Size: w:%u, h:%u", cvSource.size().width, cvSource.size().height);
+       }
+}
+
+void Preprocess::setConfig(const PreprocessConfig &config)
+{
+       _config = config;
+}
+
+template<typename T> void Preprocess::run(mv_source_h &mv_src, vector<T> &inputVector)
+{
+       LOGI("ENTER");
+
+       vector<cv::Mat> oriCvSources;
+       vector<mv_source_h> mv_srcs = { mv_src };
+
+       _vImageWidth.clear();
+       _vImageHeight.clear();
+       convertToCvSource(mv_srcs, oriCvSources);
+
+       inputVector.resize(_config.output_height * _config.output_width * _config.output_channel);
+
+       int data_type = convertToCv(_config.output_data_type, _config.output_channel);
+       // dest is a wrapper of the buffer.
+       cv::Mat dest(cv::Size(_config.output_width, _config.output_height), data_type, inputVector.data());
+       cv::Mat cvSource, cvDest;
+
+       // cvSource has new allocation with dest.size()
+       cv::resize(oriCvSources[0], cvSource, dest.size());
+
+       if (_config.skip_csc) {
+               cvSource.convertTo(dest, dest.type());
+       } else {
+               mv_colorspace_e colorspace = MEDIA_VISION_COLORSPACE_INVALID;
+               int ret = mv_source_get_colorspace(mv_srcs[0], &colorspace);
+               if (ret != MEDIA_VISION_ERROR_NONE)
+                       throw InvalidOperation("Fail to get color space.");
+
+               // cvDest is allocated if colorspace is not RGB888, and
+               // cvDest shares the data with cvSource if the colorspace is RGB888.
+               colorConvert(cvSource, cvDest, colorspace, _config.output_format);
+               cvDest.convertTo(dest, dest.type());
+       }
+
+       if (_config.normalize)
+               normalize(dest, dest, _config.mean, _config.std);
+
+       if (_config.quantize)
+               quantize(dest, dest, _config.scale, _config.zeropoint);
+
+       LOGI("LEAVE");
+}
+
+template void Preprocess::run<float>(mv_source_h &mv_src, vector<float> &inputVector);
+template void Preprocess::run<unsigned char>(mv_source_h &mv_src, vector<unsigned char> &inputVector);
+
+} /* machine_learning */
+} /* mediavision */
index c552427c09fc4c2db4827cedc0608c87cc7a2a0f..64ca00f6507889b56116c1f54e182c4d697e6313 100644 (file)
@@ -29,7 +29,7 @@
 #include "Inference.h"
 #include "object_detection_type.h"
 #include "ObjectDetectionParser.h"
-#include "Preprocess.h"
+#include "machine_learning_preprocess.h"
 #include "iobject_detection.h"
 
 namespace mediavision
index 62e332ddd793b194c8524334ef693ef543ad1c49..e4401bb22e33965ef2b495d05dc3f343efaad07e 100644 (file)
@@ -26,7 +26,7 @@
 #include "Inference.h"
 #include "object_detection_3d_type.h"
 #include "ObjectDetection3dParser.h"
-#include "Preprocess.h"
+#include "machine_learning_preprocess.h"
 
 namespace mediavision
 {
index c62219fbfb3d6c5cdc026c5de2dec14bf3448fdf..eaf6ed7a9bf60eedb6a385ed66eac2e425278697 100644 (file)
@@ -420,7 +420,7 @@ find . -name '*.gcno' -not -path "./test/*" -not -path "./mv_machine_learning/*"
 %files machine_learning-devel
 %{_includedir}/media/mv_infer*.h
 %{_includedir}/media/machine_learning_exception.h
-%{_includedir}/media/Preprocess.h
+%{_includedir}/media/machine_learning_preprocess.h
 %{_libdir}/pkgconfig/*inference.pc
 %if "%{enable_ml_face_recognition}" == "1"
 %{_includedir}/media/mv_face_recognition*.h