mv_machine_learning: drop input and output type dependency from image classification
authorInki Dae <inki.dae@samsung.com>
Wed, 15 Nov 2023 10:31:30 +0000 (19:31 +0900)
committerKwanghoon Son <k.son@samsung.com>
Wed, 6 Dec 2023 01:36:46 +0000 (10:36 +0900)
[Issue type] : code refactoring

Drop input and output type dependency from image classification task group
by introducing new common input and output types, InputBaseType and
OutputBaseType, which is located in mv_machine_learning/common directory,
and by making the input and output types specific to the image
classification task group to be inherited from the common types,
and then by making adapter class of the image classification task group
to use the common type instead of specific one.

In MachineLearningType.h which is a new common header file,
struct InputBaseType {
...
};

struct OutputBaseType {
...
};

And in image_classification_type.h which is specific to image classification
task group,
struct ImageClassificationInput : public InputBaseType {
...
};

struct ImageClassificationOutput : public OutputBaseType {
...
};

With this, native API implementation - mv_image_classification.c - of the image
classification task group has no any dependency on the file so that we can
make all native implementation portions - the code in the module files starting
with 'mv_' prefix - to use common function in the mv_machine_learning/common
directory. The common function will be introduced soon.

This is a first step to drop the code duplication from the native API
implementation module of the each task group. And it's a time for second phase
since the completion of dropping the meta file dependency from the concrete
class of each task group.

Change-Id: I14764253aeafc36d4e0f39b204b35985b3ecc73a
Signed-off-by: Inki Dae <inki.dae@samsung.com>
mv_machine_learning/common/include/MachineLearningType.h [new file with mode: 0644]
mv_machine_learning/image_classification/include/image_classification_type.h
mv_machine_learning/image_classification/src/image_classification_adapter.cpp
mv_machine_learning/image_classification/src/mv_image_classification.cpp

diff --git a/mv_machine_learning/common/include/MachineLearningType.h b/mv_machine_learning/common/include/MachineLearningType.h
new file mode 100644 (file)
index 0000000..8e8eb1e
--- /dev/null
@@ -0,0 +1,40 @@
+/**
+ * Copyright (c) 2023 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_TYPE_H__
+#define __MACHINE_LEARNING_TYPE_H__
+
+#include <mv_common.h>
+#include <mv_inference_type.h>
+
+namespace mediavision
+{
+namespace machine_learning
+{
+struct InputBaseType {
+       mv_source_h inference_src {};
+       InputBaseType(mv_source_h src = NULL) : inference_src(src)
+       {}
+};
+
+struct OutputBaseType {
+       unsigned long frame_number {};
+};
+
+} // machine_learning
+} // mediavision
+
+#endif
\ No newline at end of file
index 2cee348..eb27c32 100644 (file)
 
 #include <mv_common.h>
 #include <mv_inference_type.h>
+#include "MachineLearningType.h"
 
 namespace mediavision
 {
 namespace machine_learning
 {
-struct ImageClassificationInput {
-       mv_source_h inference_src {};
-       // TODO.
+struct ImageClassificationInput : public InputBaseType {
+       ImageClassificationInput(mv_source_h src = NULL) : InputBaseType(src)
+       {}
 };
 
-/**
- * @brief The object detection result structure.
- * @details Contains object detection 3d result.
- */
-struct ImageClassificationResult {
-       unsigned long frame_number {};
+struct ImageClassificationResult : public OutputBaseType {
        std::string label;
 };
 
 enum class ImageClassificationTaskType {
        IMAGE_CLASSIFICATION_TASK_NONE = 0,
-       // TODO
 };
 
 }
index fdb2e30..1e4f748 100644 (file)
@@ -125,12 +125,12 @@ template<typename T, typename V> void ImageClassificationAdapter<T, V>::perform(
 
 template<typename T, typename V> void ImageClassificationAdapter<T, V>::performAsync(T &t)
 {
-       _image_classification->performAsync(t);
+       _image_classification->performAsync(static_cast<ImageClassificationInput &>(t));
 }
 
 template<typename T, typename V> V &ImageClassificationAdapter<T, V>::getOutput()
 {
-       return _image_classification->getOutput();
+       return static_cast<V &>(_image_classification->getOutput());
 }
 
 template<typename T, typename V> V &ImageClassificationAdapter<T, V>::getOutputCache()
@@ -138,6 +138,6 @@ template<typename T, typename V> V &ImageClassificationAdapter<T, V>::getOutputC
        throw InvalidOperation("Not support yet.");
 }
 
-template class ImageClassificationAdapter<ImageClassificationInput, ImageClassificationResult>;
+template class ImageClassificationAdapter<InputBaseType, OutputBaseType>;
 }
 }
index fe6a7bf..9908a0b 100644 (file)
@@ -33,7 +33,7 @@ using namespace mediavision::common;
 using namespace mediavision::machine_learning;
 using namespace MediaVision::Common;
 using namespace mediavision::machine_learning::exception;
-using ImageClassificationTask = ITask<ImageClassificationInput, ImageClassificationResult>;
+using ImageClassificationTask = ITask<InputBaseType, OutputBaseType>;
 
 int mv_image_classification_create(mv_image_classification_h *out_handle)
 {
@@ -47,7 +47,7 @@ int mv_image_classification_create(mv_image_classification_h *out_handle)
 
        try {
                context = new Context();
-               task = new ImageClassificationAdapter<ImageClassificationInput, ImageClassificationResult>();
+               task = new ImageClassificationAdapter<InputBaseType, OutputBaseType>();
 
                context->__tasks.insert(make_pair("image_classification", task));
                *out_handle = static_cast<mv_image_classification_h>(context);
@@ -58,7 +58,7 @@ int mv_image_classification_create(mv_image_classification_h *out_handle)
                return e.getError();
        }
 
-       LOGD("object detection 3d handle [%p] has been created", *out_handle);
+       LOGD("image classification handle [%p] has been created", *out_handle);
 
        MEDIA_VISION_FUNCTION_LEAVE();
 
@@ -143,7 +143,7 @@ int mv_image_classification_inference(mv_image_classification_h handle, mv_sourc
                auto context = static_cast<Context *>(handle);
                auto task = static_cast<ImageClassificationTask *>(context->__tasks.at("image_classification"));
 
-               ImageClassificationInput input = { source };
+               ImageClassificationInput input(source);
 
                task->setInput(input);
                task->perform();
@@ -169,7 +169,7 @@ int mv_image_classification_inference_async(mv_image_classification_h handle, mv
                auto context = static_cast<Context *>(handle);
                auto task = static_cast<ImageClassificationTask *>(context->__tasks.at("image_classification"));
 
-               ImageClassificationInput input = { source };
+               ImageClassificationInput input(source);
 
                task->performAsync(input);
        } catch (const BaseException &e) {
@@ -195,7 +195,7 @@ int mv_image_classification_get_label(mv_image_classification_h handle, const ch
                auto context = static_cast<Context *>(handle);
                auto task = static_cast<ImageClassificationTask *>(context->__tasks.at("image_classification"));
 
-               ImageClassificationResult &result = task->getOutput();
+               const auto &result = static_cast<ImageClassificationResult &>(task->getOutput());
 
                *out_label = result.label.c_str();
        } catch (const BaseException &e) {