summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorInki Dae <inki.dae@samsung.com>2022-08-19 19:51:00 +0900
committerInki Dae <inki.dae@samsung.com>2022-09-02 17:09:25 +0900
commit8a0bcb13b9aa25b0205080cefaac53aba7833aca (patch)
tree8414128b1ca51e8a3d7ddb0bfa1f6f245d0110c9
parent38af500c31778fcacbff4890289931a2b2b4d2c9 (diff)
downloadmediavision-8a0bcb13b9aa25b0205080cefaac53aba7833aca.tar.gz
mediavision-8a0bcb13b9aa25b0205080cefaac53aba7833aca.tar.bz2
mediavision-8a0bcb13b9aa25b0205080cefaac53aba7833aca.zip
mv_machine_learning: code refactoring to Inference class
[Issue type] : code refactoring Did code refactoring to Run member function of Inference class by doing, - extracted a function which converts mv sources to cv ones. And this converting function is now considered for multiple sources. - introduced a new util file which can contain common things to be used by machine learning relevant code. - and did code sliding and renaming several variables. This is just a step for next code refactoring and there are many things to do. Change-Id: Ic78ef26156481def5ecda537657638c613a304d1 Signed-off-by: Inki Dae <inki.dae@samsung.com>
-rw-r--r--mv_common/include/util.h85
-rwxr-xr-xmv_machine_learning/inference/src/Inference.cpp92
2 files changed, 114 insertions, 63 deletions
diff --git a/mv_common/include/util.h b/mv_common/include/util.h
new file mode 100644
index 00000000..5cb6b8e0
--- /dev/null
+++ b/mv_common/include/util.h
@@ -0,0 +1,85 @@
+/**
+ * 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 __UTIL_H__
+#define __UTIL_H__
+
+#include <vector>
+
+#include <opencv2/core.hpp>
+#include <opencv2/imgproc.hpp>
+
+namespace mediavision
+{
+namespace common
+{
+namespace util
+{
+
+ static int ConvertToCvSource(std::vector<mv_source_h> &mv_srcs, std::vector<cv::Mat>& cv_srcs, std::vector<mv_rectangle_s>& rects)
+ {
+ unsigned int rect_idx = 0;
+
+ for (auto& mv_src : mv_srcs) {
+ mv_colorspace_e colorspace = MEDIA_VISION_COLORSPACE_INVALID;
+ mv_rectangle_s *roi = &rects[rect_idx++];
+ 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))
+ return MEDIA_VISION_ERROR_INTERNAL;
+
+ // TODO. Let's support various color spaces.
+
+ if (colorspace != MEDIA_VISION_COLORSPACE_RGB888) {
+ LOGE("Not Supported format.");
+ return MEDIA_VISION_ERROR_NOT_SUPPORTED_FORMAT;
+ }
+
+ /* convert mv_source to cv::Mat */
+ cv::Mat cvSource;
+
+ if (roi == NULL) {
+ cvSource = cv::Mat(cv::Size(width, height), CV_MAKETYPE(CV_8U, 3), buffer).clone();
+ } else {
+ cv::Rect cvRoi;
+
+ cvRoi.x = roi->point.x;
+ cvRoi.y = roi->point.y;
+ cvRoi.width = unsigned(roi->point.x + roi->width) >= width ? width - roi->point.x : roi->width;
+ cvRoi.height = unsigned(roi->point.y + roi->height) >= height ? height - roi->point.y : roi->height;
+ cvSource = cv::Mat(cv::Size(width, height), CV_MAKETYPE(CV_8U, 3), buffer)(cvRoi).clone();
+ }
+
+ cv_srcs.push_back(cvSource);
+ LOGI("Size: w:%u, h:%u", cvSource.size().width, cvSource.size().height);
+ }
+
+ return MEDIA_VISION_ERROR_NONE;
+ }
+
+
+} // util
+} // common
+} // mediavision
+
+#endif \ No newline at end of file
diff --git a/mv_machine_learning/inference/src/Inference.cpp b/mv_machine_learning/inference/src/Inference.cpp
index b655f225..0e3123e4 100755
--- a/mv_machine_learning/inference/src/Inference.cpp
+++ b/mv_machine_learning/inference/src/Inference.cpp
@@ -19,6 +19,7 @@
#include "InferenceIni.h"
#include "ObjectDecoder.h"
#include "PoseDecoder.h"
+#include "util.h"
#include <map>
#include <list>
@@ -43,6 +44,8 @@ typedef enum {
InputAttrBool = 6,
} InputAttrType;
+using namespace mediavision::common::util;
+
namespace mediavision
{
namespace inference
@@ -278,7 +281,6 @@ namespace inference
int Inference::Preprocess(cv::Mat cvImg, cv::Mat cvDst, int data_type)
{
- mSourceSize = cvImg.size();
int width = mInputSize.width;
int height = mInputSize.height;
const int ch = mConfig.mTensorInfo.ch;
@@ -931,87 +933,51 @@ namespace inference
return MEDIA_VISION_ERROR_INVALID_PARAMETER;
}
- // TODO. Consider multiple sources.
- mv_source_h mvSource = mvSources.front();
- mv_rectangle_s *roi = rects.empty() ? NULL : &(rects.front());
- 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(mvSource, &width) != MEDIA_VISION_ERROR_NONE ||
- mv_source_get_height(mvSource, &height) !=
- MEDIA_VISION_ERROR_NONE ||
- mv_source_get_colorspace(mvSource, &colorspace) !=
- MEDIA_VISION_ERROR_NONE ||
- mv_source_get_buffer(mvSource, &buffer, &bufferSize))
- return MEDIA_VISION_ERROR_INTERNAL;
-
- // TODO. Let's support various color spaces.
+ if (!rects.empty() && rects.size() != mvSources.size()) {
+ LOGE("mvSources.size() should be same as rects.size() if rects.empty() is false.");
+ return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+ }
- if (colorspace != MEDIA_VISION_COLORSPACE_RGB888) {
- LOGE("Not Supported format!\n");
- return MEDIA_VISION_ERROR_NOT_SUPPORTED_FORMAT;
+ if (mConfig.mTensorInfo.ch != 1 && mConfig.mTensorInfo.ch != 3) {
+ LOGE("Channel not supported.");
+ return MEDIA_VISION_ERROR_INVALID_PARAMETER;
}
- /* convert mv_source to cv::Mat */
- cv::Mat cvSource;
- cv::Rect cvRoi;
+ std::vector<cv::Mat> cvSources;
- if (roi == NULL) {
- cvSource = cv::Mat(cv::Size(width, height), CV_MAKETYPE(CV_8U, 3),
- buffer)
- .clone();
- } else {
- cvRoi.x = roi->point.x;
- cvRoi.y = roi->point.y;
- cvRoi.width = unsigned(roi->point.x + roi->width) >= width ?
- width - roi->point.x :
- roi->width;
- cvRoi.height = unsigned(roi->point.y + roi->height) >= height ?
- height - roi->point.y :
- roi->height;
- cvSource = cv::Mat(cv::Size(width, height), CV_MAKETYPE(CV_8U, 3),
- buffer)(cvRoi)
- .clone();
+ ret = ConvertToCvSource(mvSources, cvSources, rects);
+ if (ret != MEDIA_VISION_ERROR_NONE) {
+ LOGE("Fail to convert mv source to cv source.");
+ return ret;
}
- LOGI("Size: w:%u, h:%u", cvSource.size().width, cvSource.size().height);
+ // mSourceSize is original input image's size
+ // TODO. consider multiple cv sources.
+ mSourceSize = cvSources[0].size();
- if (mConfig.mTensorInfo.ch != 1 && mConfig.mTensorInfo.ch != 3) {
- LOGE("Channel not supported.");
- return MEDIA_VISION_ERROR_INVALID_PARAMETER;
- }
+ unsigned int src_idx = 0;
- if (mMetadata.GetInputMeta().IsParsed()) {
- for (auto& buffer : mInputTensorBuffers.getIETensorBuffer()) {
- inference_engine_tensor_buffer& tensor_buffer = buffer.second;
+ for (auto& buffer : mInputTensorBuffers.getIETensorBuffer()) {
+ inference_engine_tensor_buffer& tensor_buffer = buffer.second;
+ int data_type = ConvertToCv(tensor_buffer.data_type);
+
+ if (mMetadata.GetInputMeta().IsParsed()) {
const LayerInfo& layerInfo = mMetadata.GetInputMeta().GetLayer().at(buffer.first);
const Options& opt = mMetadata.GetInputMeta().GetOption().empty() ? Options() : mMetadata.GetInputMeta().GetOption().at(buffer.first);
+ mv_colorspace_e colorspace = MEDIA_VISION_COLORSPACE_INVALID;
- int data_type = ConvertToCv(tensor_buffer.data_type);
+ mv_source_get_colorspace(mvSources[src_idx], &colorspace);
- // mSourceSize is original input image's size
- mSourceSize = cvSource.size();
// TODO: try-catch{} error handling
- ret = mPreProc.Run(cvSource, colorspace, data_type, layerInfo, opt, tensor_buffer.buffer);
+ ret = mPreProc.Run(cvSources[src_idx++], colorspace, data_type, layerInfo, opt, tensor_buffer.buffer);
if (ret != MEDIA_VISION_ERROR_NONE) {
LOGE("Fail to run pre-process.");
return ret;
}
- }
- } else {
- for (auto& buffer : mInputTensorBuffers.getIETensorBuffer()) {
- inference_engine_tensor_buffer& tensor_buffer = buffer.second;
-
- int data_type = ConvertToCv(tensor_buffer.data_type);
-
+ } else {
// Convert color space of input tensor data and then normalize it.
- ret = Preprocess(cvSource,
- cv::Mat(mInputSize.height, mInputSize.width,
- data_type, tensor_buffer.buffer),
- data_type);
+ ret = Preprocess(cvSources[src_idx++], cv::Mat(mInputSize.height, mInputSize.width, data_type, tensor_buffer.buffer), data_type);
if (ret != MEDIA_VISION_ERROR_NONE) {
LOGE("Fail to preprocess input tensor data.");
return ret;