mv_machine_learning: add get label API support for semantic segmentation task 53/320353/3
authorInki Dae <inki.dae@samsung.com>
Thu, 27 Feb 2025 06:11:12 +0000 (15:11 +0900)
committerInki Dae <inki.dae@samsung.com>
Fri, 7 Mar 2025 08:18:18 +0000 (17:18 +0900)
Add get label API support for semantic segmentation task.

Change-Id: Ic381b0157706d6b939e72517db134977570ca4a3
Signed-off-by: Inki Dae <inki.dae@samsung.com>
include/mv_semantic_segmentation_internal.h
mv_machine_learning/image_segmentation/include/image_segmentation_type.h
mv_machine_learning/image_segmentation/src/Stdc1.cpp
mv_machine_learning/image_segmentation/src/mv_semantic_segmentation.cpp

index a190f28134357e5dbf6d3623987bc172e45fbbc8..43cb20d214b61da5a1989e210044ca32646c5df9 100644 (file)
@@ -240,6 +240,51 @@ int mv_semantic_segmentation_get_result_count(mv_semantic_segmentation_h handle,
 int mv_semantic_segmentation_get_result(mv_semantic_segmentation_h handle, unsigned int *width, unsigned int *height,
                                                                          unsigned int *pixel_size, const unsigned char **data);
 
+/**
+ * @internal
+ * @brief Gets a number of labels.
+ *
+ * @since_tizen 10.0
+ *
+ * @param[in] handle              The handle to the inference
+ * @param[out] cnt              A number of labels.
+ *
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #MEDIA_VISION_ERROR_NONE Successful
+ * @retval #MEDIA_VISION_ERROR_NOT_SUPPORTED Not supported
+ * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @pre Create a source handle by calling mv_create_source()
+ * @pre Create an inference handle by calling mv_semantic_segmentation_create()
+ * @pre Prepare an inference by calling mv_semantic_segmentation_configure()
+ * @pre Prepare an inference by calling mv_semantic_segmentation_prepare()
+ * @pre Prepare an inference by calling mv_semantic_segmentation_inference()
+ */
+int mv_semantic_segmentation_get_label_count(mv_semantic_segmentation_h handle, unsigned int *cnt);
+
+/**
+ * @internal
+ * @brief Gets a label to segmented object region.
+ *
+ * @since_tizen 10.0
+ *
+ * @param[in] handle              The handle to the inference
+ * @param[in] index               A result index.
+ * @param[out] label              A label name to a detected object.
+ *
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #MEDIA_VISION_ERROR_NONE Successful
+ * @retval #MEDIA_VISION_ERROR_NOT_SUPPORTED Not supported
+ * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @pre Create a source handle by calling mv_create_source()
+ * @pre Create an inference handle by calling mv_semantic_segmentation_create()
+ * @pre Prepare an inference by calling mv_semantic_segmentation_configure()
+ * @pre Prepare an inference by calling mv_semantic_segmentation_prepare()
+ * @pre Prepare an inference by calling mv_semantic_segmentation_inference()
+ */
+int mv_semantic_segmentation_get_label(mv_semantic_segmentation_h handle, unsigned int index, const char **label);
+
 /**
  * @internal
  * @brief Set user-given inference engine and device types for inference.
index 904a56ee61bc491daafd6a2eb513bae790bb538c..232cdec82e9fad3df2cc96561bdfb2107216b83b 100644 (file)
@@ -41,6 +41,7 @@ struct ImageSegmentationResult : public OutputBaseType {
        unsigned int height {};
        unsigned int pixel_size {};
        std::vector<unsigned char> data;
+       unsigned int num_labels {};
        std::vector<std::string> labels;
 };
 
index c3221300cf051f4de0ead282e34de0f47c562766..278ea0da274e9292613d43b62a857078fea3d578 100644 (file)
@@ -72,6 +72,7 @@ template<typename T> ImageSegmentationResult &Stdc1<T>::result()
        _result.width = static_cast<int>(input_tensor_width * width_ratio);
        _result.pixel_size = 1;
        _result.labels = _labels;
+       _result.num_labels = _labels.size();
 
        cv::Mat cvSource = cv::Mat(cv::Size(width, height), CV_MAKETYPE(CV_8U, 1), outputTensor.data());
        cv::Mat cvDest(_result.height, _result.width, CV_8UC1);
index 1269e6850b1757f686bde21c485a08ef6e48e2f1..bd708c1b473cb47dd463396c60d1f61134537efd 100644 (file)
@@ -351,5 +351,57 @@ int mv_semantic_segmentation_get_result(mv_semantic_segmentation_h handle, unsig
 
        MEDIA_VISION_FUNCTION_LEAVE();
 
+       return MEDIA_VISION_ERROR_NONE;
+}
+
+int mv_semantic_segmentation_get_label_count(mv_semantic_segmentation_h handle, unsigned int *cnt)
+{
+       MEDIA_VISION_SUPPORT_CHECK(mv_check_feature_key(feature_keys, num_keys, true));
+       MEDIA_VISION_INSTANCE_CHECK(cnt);
+
+       MEDIA_VISION_FUNCTION_ENTER();
+
+       try {
+               auto &result =
+                               static_cast<ImageSegmentationResult &>(machine_learning_native_get_result_cache(handle, TASK_NAME));
+
+               *cnt = result.num_labels;
+       } catch (const BaseException &e) {
+               LOGE("%s", e.what());
+               return e.getError();
+       }
+
+       MEDIA_VISION_FUNCTION_LEAVE();
+
+       return MEDIA_VISION_ERROR_NONE;
+}
+
+int mv_semantic_segmentation_get_label(mv_semantic_segmentation_h handle, unsigned int index, const char **label)
+{
+       MEDIA_VISION_SUPPORT_CHECK(mv_check_feature_key(feature_keys, num_keys, true));
+       MEDIA_VISION_INSTANCE_CHECK(label);
+
+       MEDIA_VISION_FUNCTION_ENTER();
+
+       try {
+               auto &result =
+                               static_cast<ImageSegmentationResult &>(machine_learning_native_get_result_cache(handle, TASK_NAME));
+
+               if (result.labels.empty())
+                       return MEDIA_VISION_ERROR_NO_DATA;
+
+               if (index >= result.labels.size()) {
+                       LOGE("Invalid index(index = %u, number of labels = %zu).", index, result.labels.size());
+                       return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+               }
+
+               *label = result.labels[index].c_str();
+       } catch (const BaseException &e) {
+               LOGE("%s", e.what());
+               return e.getError();
+       }
+
+       MEDIA_VISION_FUNCTION_LEAVE();
+
        return MEDIA_VISION_ERROR_NONE;
 }
\ No newline at end of file