inference: apply abstract factory pattern 53/310153/1
authorInki Dae <inki.dae@samsung.com>
Tue, 23 Apr 2024 06:50:16 +0000 (15:50 +0900)
committerInki Dae <inki.dae@samsung.com>
Tue, 23 Apr 2024 06:58:33 +0000 (15:58 +0900)
Apply abstract factory pattern. This patch uses abstract factory pattern
to create targeted inference service such as face detection, object detection
and so on from backend specific factory.

Regarding this, self-registeraion factory pattern is used to register
each inference service backend factory such as mediavision or mediapipe in
build time, and abstract factory pattern is used to create targeted inference
service from the inference service backend factory like below.

// create a given inference backend factory.
_factory = InferenceServiceFactory::instance().create("MvInferenceServiceFactory");

Ps. MvInferenceServiceFactory is inference service backend factory for Mediavision.
    So other backend factory such as Mediapipe will be added later.

// create a target inference task service from the given inference backend factory
_infernce_service = _factory->createFaceDetection();
or
_infernce_service = _factory->createObjectDetection();
...

Change-Id: I809bc733bd7e33a6779b6410422e3961129d7e0a
Signed-off-by: Inki Dae <inki.dae@samsung.com>
inference/CMakeLists.txt
inference/backends/mediavision/include/MvInferenceServiceFactory.h [deleted file]
inference/backends/mediavision/src/MvInferenceServiceFactory.cpp [deleted file]
inference/include/IInferenceServiceFactory.h
inference/include/IInferenceServiceInterface.h
inference/include/InferenceServiceDefault.h
inference/include/InferenceServiceFactory.h
inference/include/MvInferenceServiceFactory.h [new file with mode: 0644]
inference/src/InferenceServiceDefault.cpp
inference/src/MvInferenceServiceFactory.cpp [new file with mode: 0644]
services/auto_zoom/src/AutoZoom.cpp

index 5f0701e4d32f8077f4b79f08e64047de48652c4f..6313a1712c9ea7ecefd01f1af97f523c308ed543 100644 (file)
@@ -5,7 +5,9 @@ SET(INFERENCE_DIRECTORY ${ROOT_DIRECTORY}/inference)
 IF (${USE_EXTERNAL_INFERENCE_SERVICE})
     SET(SINGLEO_SERVICE_SOURCE_FILES ${SINGLEO_SERVICE_SOURCE_FILES} ${INFERENCE_DIRECTORY}/src/InferenceServiceExternal.cpp)
 ELSE()
-    SET(SINGLEO_SERVICE_SOURCE_FILES ${SINGLEO_SERVICE_SOURCE_FILES} ${INFERENCE_DIRECTORY}/src/InferenceServiceDefault.cpp)
+    SET(SINGLEO_SERVICE_SOURCE_FILES ${SINGLEO_SERVICE_SOURCE_FILES}
+                                     ${INFERENCE_DIRECTORY}/src/InferenceServiceDefault.cpp
+                                     ${INFERENCE_DIRECTORY}/src/MvInferenceServiceFactory.cpp)
 ENDIF()
 
 INCLUDE(${INFERENCE_DIRECTORY}/backends/mediavision/CMakeLists.txt)
diff --git a/inference/backends/mediavision/include/MvInferenceServiceFactory.h b/inference/backends/mediavision/include/MvInferenceServiceFactory.h
deleted file mode 100644 (file)
index 7ab72c9..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/**
- * Copyright (c) 2024 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 __MV_INFERENCE_SERVICE_FACTORY_H__
-#define __MV_INFERENCE_SERVICE_FACTORY_H__
-
-#include "IInferenceServiceFactory.h"
-
-namespace singleo
-{
-namespace inference
-{
-class MvInferenceServiceFactory : public IInferenceServiceFactory
-{
-private:
-       std::unique_ptr<IInferenceServiceInterface>createInferenceService(TaskType taskType);
-
-public:
-       MvInferenceServiceFactory() = default;
-       virtual ~MvInferenceServiceFactory() = default;
-
-       std::unique_ptr<IInferenceServiceInterface> createImageClassification() override;
-       std::unique_ptr<IInferenceServiceInterface> createObjectDetection() override;
-       std::unique_ptr<IInferenceServiceInterface> createFaceDetection() override;
-       std::unique_ptr<IInferenceServiceInterface> createFaceLandmarkDetection() override;
-};
-
-}
-}
-
-#endif
diff --git a/inference/backends/mediavision/src/MvInferenceServiceFactory.cpp b/inference/backends/mediavision/src/MvInferenceServiceFactory.cpp
deleted file mode 100644 (file)
index b53abf6..0000000
+++ /dev/null
@@ -1,63 +0,0 @@
-/**
- * Copyright (c) 2024 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 "MvInferenceServiceFactory.h"
-#include "InferenceServiceDefault.h"
-#include "SingleoLog.h"
-#include "SingleoException.h"
-
-using namespace std;
-using namespace singleo::exception;
-
-namespace singleo
-{
-namespace inference
-{
-namespace backends
-{
-
-std::unique_ptr<IInferenceServiceInterface>MvInferenceServiceFactory::createInferenceService(TaskType taskType)
-{
-       auto service = make_unique<InferenceServiceDefault>();
-
-       service->configure(taskType);
-
-       return service;
-}
-
-std::unique_ptr<IInferenceServiceInterface> MvInferenceServiceFactory::createImageClassification()
-{
-       throw InvalidOperation("Interface not supported yet.");
-}
-
-std::unique_ptr<IInferenceServiceInterface> MvInferenceServiceFactory::createObjectDetection()
-{
-       return createInferenceService(TaskType::OBJECT_DETECTION);
-}
-
-std::unique_ptr<IInferenceServiceInterface> MvInferenceServiceFactory::createFaceDetection()
-{
-       return createInferenceService(TaskType::FACE_DETECTION);
-}
-
-std::unique_ptr<IInferenceServiceInterface> MvInferenceServiceFactory::createFaceLandmarkDetection()
-{
-       throw InvalidOperation("Interface not supported yet.");
-}
-
-}
-}
-}
index 5dfdbbebf1d324f4cfbc634b7ea0ff0027b36d2c..7f0ffb884fffa8f89bba23c6e9bd6bd55ab048de 100644 (file)
@@ -34,7 +34,7 @@ public:
        virtual std::unique_ptr<IInferenceServiceInterface> createObjectDetection() = 0;
        virtual std::unique_ptr<IInferenceServiceInterface> createFaceDetection() = 0;
        virtual std::unique_ptr<IInferenceServiceInterface> createFaceLandmarkDetection() = 0;
-}
+};
 
 }
 }
index 3e5a0e86cdf6cf37336284621ccce9a5a512ebf8..f41228838304145d1258f976fd26e2e8bb677fb4 100644 (file)
@@ -29,7 +29,7 @@ class IInferenceServiceInterface
 public:
        virtual ~IInferenceServiceInterface() {};
 
-       virtual void configure(TaskType task_type) = 0;
+       virtual void configure() = 0;
        virtual void prepare() = 0;
        virtual void invoke(BaseDataType &input, bool async = false) = 0;
        virtual BaseResultType &result() = 0;
index a269b75bd31098a909c2c844b65bd3ed5a47442e..c33f326ca51adba7f30390e0ed7a49abddda6cf2 100644 (file)
@@ -31,13 +31,12 @@ class InferenceServiceDefault : public IInferenceServiceInterface
 {
 private:
        std::unique_ptr<IInferenceTaskInterface> _task;
-       static bool _registered;
 
 public:
-       InferenceServiceDefault() = default;
+       explicit InferenceServiceDefault(TaskType task_type);
        virtual ~InferenceServiceDefault() = default;
 
-       void configure(TaskType task_type) override;
+       void configure() override;
        void prepare() override;
        void invoke(BaseDataType &input, bool async = false) override;
        BaseResultType &result() override;
index bb2f099d3ff84f8580858c774e6623022e222c14..7011d1a4b78e888d2ac07191cc5cc36317ae2dfc 100644 (file)
@@ -22,7 +22,7 @@
 #include <string>
 #include <memory>
 #include <stdexcept>
-#include "IInferenceServiceInterface.h"
+#include "IInferenceServiceFactory.h"
 #include "SingleoException.h"
 #include "SingleoLog.h"
 
@@ -33,7 +33,7 @@ namespace inference
 class InferenceServiceFactory
 {
 public:
-       using createFunc = std::unique_ptr<IInferenceServiceInterface> (*)();
+       using createFunc = std::unique_ptr<IInferenceServiceFactory> (*)();
 
        static InferenceServiceFactory &instance()
        {
@@ -41,13 +41,13 @@ public:
                return factory;
        }
 
-       std::unique_ptr<IInferenceServiceInterface> create(const std::string &name)
+       std::unique_ptr<IInferenceServiceFactory> create(const std::string &name)
        {
                auto it = _inference_service_table.find(name);
                if (it != _inference_service_table.end())
                        return it->second();
 
-               throw singleo::exception::InvalidParameter("Invalid inference service.");
+               throw singleo::exception::InvalidParameter("Invalid inference service factory.");
        }
 
        void registerBackend(const std::string &name, const createFunc &func)
@@ -62,10 +62,10 @@ private:
        std::unordered_map<std::string, createFunc> _inference_service_table;
 };
 
-template<typename ServiceType> bool registerInferenceService(const std::string &name)
+template<typename FactoryType> bool registerInferenceServiceFactory(const std::string &name)
 {
        InferenceServiceFactory::instance().registerBackend(name, []() {
-               return std::unique_ptr<IInferenceServiceInterface>(new ServiceType());
+               return std::unique_ptr<IInferenceServiceFactory>(new FactoryType());
        });
 
        return true;
diff --git a/inference/include/MvInferenceServiceFactory.h b/inference/include/MvInferenceServiceFactory.h
new file mode 100644 (file)
index 0000000..351203e
--- /dev/null
@@ -0,0 +1,44 @@
+/**
+ * Copyright (c) 2024 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 __MV_INFERENCE_SERVICE_FACTORY_H__
+#define __MV_INFERENCE_SERVICE_FACTORY_H__
+
+#include "IInferenceServiceFactory.h"
+
+namespace singleo
+{
+namespace inference
+{
+class MvInferenceServiceFactory : public IInferenceServiceFactory
+{
+private:
+       static bool _registered;
+
+public:
+       MvInferenceServiceFactory() = default;
+       virtual ~MvInferenceServiceFactory() = default;
+
+       std::unique_ptr<IInferenceServiceInterface> createImageClassification() override;
+       std::unique_ptr<IInferenceServiceInterface> createObjectDetection() override;
+       std::unique_ptr<IInferenceServiceInterface> createFaceDetection() override;
+       std::unique_ptr<IInferenceServiceInterface> createFaceLandmarkDetection() override;
+};
+
+}
+}
+
+#endif
index 46d4c947d1bd50c18ee757ec20d14af8b139bfd4..4de47f48a7373f4a77de160b19544d0ff57d6bc9 100644 (file)
@@ -14,7 +14,6 @@
  * limitations under the License.
  */
 
-#include "InferenceServiceFactory.h"
 #include "InferenceServiceDefault.h"
 #include "MvObjectDetection.h"
 #include "MvFaceDetection.h"
@@ -26,10 +25,7 @@ namespace singleo
 {
 namespace inference
 {
-bool InferenceServiceDefault::_registered =
-               registerInferenceService<InferenceServiceDefault>("InferenceServiceDefault");
-
-void InferenceServiceDefault::configure(TaskType task_type)
+InferenceServiceDefault::InferenceServiceDefault(TaskType task_type)
 {
        // TODO. add other task types later.
        if (task_type != TaskType::OBJECT_DETECTION && task_type != TaskType::FACE_DETECTION)
@@ -43,7 +39,10 @@ void InferenceServiceDefault::configure(TaskType task_type)
                _task = make_unique<MvFaceDetection>();
                break;
        }
+}
 
+void InferenceServiceDefault::configure()
+{
        _task->configure();
 }
 
diff --git a/inference/src/MvInferenceServiceFactory.cpp b/inference/src/MvInferenceServiceFactory.cpp
new file mode 100644 (file)
index 0000000..87b3a09
--- /dev/null
@@ -0,0 +1,54 @@
+/**
+ * Copyright (c) 2024 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 "InferenceServiceFactory.h"
+#include "InferenceServiceDefault.h"
+#include "MvInferenceServiceFactory.h"
+#include "SingleoLog.h"
+#include "SingleoException.h"
+
+using namespace std;
+using namespace singleo::exception;
+
+namespace singleo
+{
+namespace inference
+{
+bool MvInferenceServiceFactory::_registered =
+               registerInferenceServiceFactory<MvInferenceServiceFactory>("MvInferenceServiceFactory");
+
+std::unique_ptr<IInferenceServiceInterface> MvInferenceServiceFactory::createImageClassification()
+{
+       throw InvalidOperation("Interface not supported yet.");
+}
+
+std::unique_ptr<IInferenceServiceInterface> MvInferenceServiceFactory::createObjectDetection()
+{
+       return make_unique<InferenceServiceDefault>(TaskType::OBJECT_DETECTION);
+}
+
+std::unique_ptr<IInferenceServiceInterface> MvInferenceServiceFactory::createFaceDetection()
+{
+       return make_unique<InferenceServiceDefault>(TaskType::FACE_DETECTION);
+}
+
+std::unique_ptr<IInferenceServiceInterface> MvInferenceServiceFactory::createFaceLandmarkDetection()
+{
+       throw InvalidOperation("Interface not supported yet.");
+}
+
+}
+}
index c6388d583c9ad18391df68ca1b1d177520b93e74..bf3c5df25c809568dbc4dce8cf2be1682f4f4271 100644 (file)
@@ -17,6 +17,7 @@
 #include <algorithm>
 #include "SingleoException.h"
 #include "AutoZoom.h"
+#include "IInferenceServiceFactory.h"
 #include "InferenceServiceFactory.h"
 #include "SingleoLog.h"
 #include "ImagePreprocessor.h"
@@ -40,9 +41,11 @@ REGISTER_SERVICE(AutoZoom)
 
 AutoZoom::AutoZoom(InputConfigBase &config)
 {
-       // In default, we will use InferenceServiceDefault class to use Mediavision framework
+       // In default, we will use Inference service factory for Mediavision to use Mediavision framework
        // for inference service. TODO. introduce meta config file approach later.
-       _inference_service = InferenceServiceFactory::instance().create("InferenceServiceDefault");
+       auto factory = InferenceServiceFactory::instance().create("MvInferenceServiceFactory");
+
+       _inference_service = factory->createFaceDetection();
 
        // Create InputCamera service if input service type is CAMERA.
        if (config._input_feed_type == InputFeedType::CAMERA) {
@@ -62,7 +65,7 @@ AutoZoom::AutoZoom(InputConfigBase &config)
                SINGLEO_LOGD("Camera input service has been initialized.");
        }
 
-       _inference_service->configure(_task_type);
+       _inference_service->configure();
        _inference_service->prepare();
        _postprocessor = make_unique<Postprocessor>();
 }