backends/mediavision: add hand landmark API support 97/317597/1
authorInki Dae <inki.dae@samsung.com>
Tue, 23 Jul 2024 05:07:57 +0000 (14:07 +0900)
committerInki Dae <inki.dae@samsung.com>
Thu, 12 Sep 2024 06:00:16 +0000 (15:00 +0900)
Change-Id: I9ad37221cdbc9b39192e9e24d1879ec3a342d680
Signed-off-by: Inki Dae <inki.dae@samsung.com>
inference/backends/mediavision/CMakeLists.txt
inference/backends/mediavision/include/MvHandLandmark.h [new file with mode: 0644]
inference/backends/mediavision/include/MvInferenceTaskFactory.h
inference/backends/mediavision/src/MvHandLandmark.cpp [new file with mode: 0644]
inference/backends/mediavision/src/MvInferenceTaskFactory.cpp
inference/include/IInferenceTaskFactory.h

index d1482ebdace8bb890581f606a3b47afdcac35fe7..380115cb5d1147715c2bd64a727054c6a576a6b0 100644 (file)
@@ -7,6 +7,7 @@ SET(INFERENCE_MEDIAVISION_BACKEND_DIRECTORY ${INFERENCE_DIRECTORY}/backends/medi
 
 SET(SINGLEO_SERVICE_SOURCE_FILES
     ${SINGLEO_SERVICE_SOURCE_FILES}
+    ${INFERENCE_MEDIAVISION_BACKEND_DIRECTORY}/src/MvHandLandmark.cpp
     ${INFERENCE_MEDIAVISION_BACKEND_DIRECTORY}/src/MvHandDetection.cpp
     ${INFERENCE_MEDIAVISION_BACKEND_DIRECTORY}/src/MvFaceDetection.cpp
     ${INFERENCE_MEDIAVISION_BACKEND_DIRECTORY}/src/MvFaceLandmark.cpp
diff --git a/inference/backends/mediavision/include/MvHandLandmark.h b/inference/backends/mediavision/include/MvHandLandmark.h
new file mode 100644 (file)
index 0000000..901d1c8
--- /dev/null
@@ -0,0 +1,51 @@
+/**
+ * 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_HAND_LANDMARK_H__
+#define __MV_HAND_LANDMARK_H__
+
+#include "IInferenceTaskInterface.h"
+#include "mv_hand_landmark.h"
+#include "mv_hand_landmark_internal.h"
+#include "SingleoCommonTypes.h"
+
+namespace singleo
+{
+namespace inference
+{
+namespace backends
+{
+class MvHandLandmark : public IInferenceTaskInterface
+{
+private:
+       mv_hand_landmark_h _handle {};
+       FldResultType _output_data;
+
+public:
+       MvHandLandmark();
+       virtual ~MvHandLandmark();
+
+       void configure() override;
+       void prepare() override;
+       void invoke(BaseDataType &input, bool async) override;
+       BaseResultType &result() override;
+};
+
+} // backends
+} // inference
+} // singleo
+
+#endif
index 94be99fd7590d7a9be46ebcf6631266ba91c8dcc..f8023c1c2b2f4891b4f5a36abc389b83ba8f6a27 100644 (file)
@@ -38,6 +38,7 @@ public:
        std::unique_ptr<IInferenceTaskInterface> createFaceLandmarkDetection() override;
        std::unique_ptr<IInferenceTaskInterface> createFaceRecognition() override;
        std::unique_ptr<IInferenceTaskInterface> createHandDetection() override;
+       std::unique_ptr<IInferenceTaskInterface> createHandLandmark() override;
 };
 
 }
diff --git a/inference/backends/mediavision/src/MvHandLandmark.cpp b/inference/backends/mediavision/src/MvHandLandmark.cpp
new file mode 100644 (file)
index 0000000..ab9e9a6
--- /dev/null
@@ -0,0 +1,116 @@
+/**
+ * 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 <stdexcept>
+#include "MvHandLandmark.h"
+#include "SingleoLog.h"
+
+using namespace std;
+
+namespace singleo
+{
+namespace inference
+{
+namespace backends
+{
+MvHandLandmark::MvHandLandmark()
+{
+       int ret = mv_hand_landmark_create(&_handle);
+       if (ret != MEDIA_VISION_ERROR_NONE)
+               throw runtime_error("Fail to create hand landmark detection handle.");
+}
+
+MvHandLandmark::~MvHandLandmark()
+{
+       mv_hand_landmark_destroy(_handle);
+}
+
+void MvHandLandmark::configure()
+{
+       int ret = mv_hand_landmark_configure(_handle);
+       if (ret != MEDIA_VISION_ERROR_NONE)
+               throw runtime_error("Fail to configure hand landmark detection.");
+}
+
+void MvHandLandmark::prepare()
+{
+       int ret = mv_hand_landmark_prepare(_handle);
+       if (ret != MEDIA_VISION_ERROR_NONE)
+               throw runtime_error("Fail to prepare hand landmark detection.");
+}
+
+void MvHandLandmark::invoke(BaseDataType &input, bool async)
+{
+       ImageDataType &data = dynamic_cast<ImageDataType &>(input);
+
+       if (data._data_type != DataType::IMAGE) {
+               SINGLEO_LOGE("Invalid input type.");
+               throw invalid_argument("Input type not support.");
+       }
+
+       mv_source_h mv_src;
+
+       int ret = mv_create_source(&mv_src);
+       if (ret != MEDIA_VISION_ERROR_NONE)
+               throw runtime_error("Fail to create mv source.");
+
+       try {
+               ret = mv_source_fill_by_buffer(mv_src, data.ptr, data.width * data.height * data.byte_per_pixel, data.width,
+                                                                          data.height, MEDIA_VISION_COLORSPACE_RGB888);
+               if (ret != MEDIA_VISION_ERROR_NONE)
+                       throw runtime_error("Fail to convert to mv source.");
+
+               ret = mv_hand_landmark_inference(_handle, mv_src);
+               if (ret != MEDIA_VISION_ERROR_NONE)
+                       throw runtime_error("Fail to invoke hand landmark detection.");
+       } catch (std::runtime_error &e) {
+               SINGLEO_LOGE("%s", e.what());
+       }
+
+       ret = mv_destroy_source(mv_src);
+       if (ret != MEDIA_VISION_ERROR_NONE)
+               throw runtime_error("Fail to destroy mv source.");
+}
+
+BaseResultType &MvHandLandmark::result()
+{
+       unsigned long frame_number;
+       unsigned int result_cnt;
+
+       int ret = mv_hand_landmark_get_result_count(_handle, &frame_number, &result_cnt);
+       if (ret != MEDIA_VISION_ERROR_NONE)
+               throw runtime_error("Fail to get hand landmark detection result count.");
+
+       _output_data._is_empty = result_cnt == 0;
+       _output_data._points.clear();
+       _output_data._frame_number = frame_number;
+
+       for (unsigned int idx = 0; idx < result_cnt; ++idx) {
+               Point point;
+
+               ret = mv_hand_landmark_get_position(_handle, idx, (unsigned int *) &point.x, (unsigned int *) &point.y);
+               if (ret != MEDIA_VISION_ERROR_NONE)
+                       throw runtime_error("Fail to get hand landmark detection point.");
+
+               _output_data._points.push_back(point);
+       }
+
+       return _output_data;
+}
+
+}
+}
+}
index 5e9e7cb2c5732b4c84fc0ecefa50f0eef300b48f..5485fbf3ec0ff666dc6503a05360890c0937ef39 100644 (file)
@@ -22,6 +22,7 @@
 #include "MvObjectDetection.h"
 #include "MvImageClassification.h"
 #include "MvHandDetection.h"
+#include "MvHandLandmark.h"
 #include "SingleoLog.h"
 #include "SingleoException.h"
 
@@ -66,5 +67,10 @@ std::unique_ptr<IInferenceTaskInterface> MvInferenceTaskFactory::createHandDetec
        return make_unique<MvHandDetection>();
 }
 
+std::unique_ptr<IInferenceTaskInterface> MvInferenceTaskFactory::createHandLandmark()
+{
+       return make_unique<MvHandLandmark>();
+}
+
 }
 }
index 7bc3e9011d6d4a5947e484f5714695655b475b7e..893084d47d2a7a4c22a7bc0744949af92f5a0f8f 100644 (file)
@@ -36,6 +36,7 @@ public:
        virtual std::unique_ptr<IInferenceTaskInterface> createFaceLandmarkDetection() = 0;
        virtual std::unique_ptr<IInferenceTaskInterface> createFaceRecognition() = 0;
        virtual std::unique_ptr<IInferenceTaskInterface> createHandDetection() = 0;
+       virtual std::unique_ptr<IInferenceTaskInterface> createHandLandmark() = 0;
 };
 
 }