mv_machine_learning: do not expose structure to user
authorInki Dae <inki.dae@samsung.com>
Mon, 19 Sep 2022 13:19:35 +0000 (22:19 +0900)
committerInki Dae <inki.dae@samsung.com>
Mon, 7 Nov 2022 03:12:39 +0000 (12:12 +0900)
[Issue type] code cleanup

Do not allow for user to access structure type instead, introduce
each CAPI corresponding to each member of the structure.

Change-Id: Iac3e475ebb54b862c0b007b84f228154a10f1308
Signed-off-by: Inki Dae <inki.dae@samsung.com>
18 files changed:
include/mv_object_detection_3d.h
include/mv_object_detection_3d_type.h
mv_machine_learning/common/include/itask.h
mv_machine_learning/face_recognition/include/face_recognition.h
mv_machine_learning/face_recognition/include/face_recognition_adapter.h
mv_machine_learning/face_recognition/src/face_recognition.cpp
mv_machine_learning/face_recognition/src/face_recognition_adapter.cpp
mv_machine_learning/face_recognition/src/mv_face_recognition_open.cpp
mv_machine_learning/object_detection/include/mv_object_detection_3d_open.h
mv_machine_learning/object_detection/include/object_detection.h
mv_machine_learning/object_detection/include/object_detection_adapter.h
mv_machine_learning/object_detection/include/object_detection_type.h
mv_machine_learning/object_detection/include/objectron.h
mv_machine_learning/object_detection/src/mv_object_detection_3d.c
mv_machine_learning/object_detection/src/mv_object_detection_3d_open.cpp
mv_machine_learning/object_detection/src/object_detection_adapter.cpp
mv_machine_learning/object_detection/src/objectron.cpp
test/testsuites/machine_learning/object_detection/test_object_detection_3d.cpp

index 8438f6d..15d3662 100644 (file)
@@ -139,15 +139,15 @@ int mv_object_detection_3d_prepare(mv_object_detection_3d_h infer);
 int mv_object_detection_3d_inference(mv_object_detection_3d_h infer, mv_source_h source);
 
 /**
- * @brief Gets the object detection 3d result.
- * @details Use this function to get the detected result after calling @ref mv_object_detection_3d_inference().
+ * @brief Gets the probability value to the detected object.
+ * @details Use this function to get the probability value after calling @ref mv_object_detection_3d_inference().
  *
  * @since_tizen 7.0
  *
  * @remarks The @a result must NOT be released using free()
  *
- * @param[in] handle         The handle to the face recognition object.
- * @param[out] out_result    The structure point which contains object detection 3d inference result.
+ * @param[in] handle              The handle to the face recognition object.
+ * @param[out] out_probability    A pointer to probability array.
  *
  * @return @c 0 on success, otherwise a negative error value
  * @retval #MEDIA_VISION_ERROR_NONE Successful
@@ -156,7 +156,48 @@ int mv_object_detection_3d_inference(mv_object_detection_3d_h infer, mv_source_h
  *
  * @pre Request an inference by calling @ref mv_object_detection_3d_inference()
  */
-int mv_object_detection_3d_get_result(mv_object_detection_3d_h handle, mv_object_detection_3d_result_s *out_result);
+int mv_object_detection_3d_get_probability(mv_object_detection_3d_h handle, unsigned int *out_probability);
+
+/**
+ * @brief Gets the number of points to the 3D bounding box of the detected object.
+ * @details Use this function to get the number of points after calling @ref mv_object_detection_3d_inference().
+ *
+ * @since_tizen 7.0
+ *
+ * @remarks The @a result must NOT be released using free()
+ *
+ * @param[in] handle                The handle to the face recognition object.
+ * @param[out] out_num_of_points    Number of points.
+ *
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #MEDIA_VISION_ERROR_NONE Successful
+ * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #MEDIA_VISION_ERROR_INVALID_OPERATION Invalid operation
+ *
+ * @pre Request an inference by calling @ref mv_object_detection_3d_inference()
+ */
+int mv_object_detection_3d_get_num_of_points(mv_object_detection_3d_h handle, unsigned int *out_num_of_points);
+
+/**
+ * @brief Gets the x and y coordinates values to the 3D bounding box of the detected object.
+ * @details Use this function to get the coordinates values after calling @ref mv_object_detection_3d_inference().
+ *
+ * @since_tizen 7.0
+ *
+ * @remarks The @a result must NOT be released using free()
+ *
+ * @param[in] handle    The handle to the face recognition object.
+ * @param[out] out_x    A pointer to x coordinates array.
+ * @param[out] out_y    A pointer to y coordinates array.
+  *
+ * @return @c 0 on success, otherwise a negative error value
+ * @retval #MEDIA_VISION_ERROR_NONE Successful
+ * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #MEDIA_VISION_ERROR_INVALID_OPERATION Invalid operation
+ *
+ * @pre Request an inference by calling @ref mv_object_detection_3d_inference()
+ */
+int mv_object_detection_3d_get_points(mv_object_detection_3d_h handle, unsigned int **out_x, unsigned int **out_y);
 /**
  * @}
  */
index fae4eda..5e207be 100644 (file)
@@ -34,32 +34,6 @@ extern "C" {
  */
 
 /**
- * @brief                The structure to object detection 3d result.
- *
- * @since_tizen 7.0
- * @remarks @a points should not be released by app.
- *          The number of elements in @a points is equal to @a number_of_points.
- *
- * @probability          The probability value to the detected object.
- * @number_of_points     The number of points.
- * @points               2d coodinations to keypoints.
- * @number_of_edges      The number of edges.
- * @edge_indexes         pair index per a edge to the points array.
- *
- * @pre Call mv_object_detection_3d_inference() function to perform 3d detection of the objects
- *      in @a source and to get a result.
- *
- * @see mv_object_detect_3d_inference()
- */
-typedef struct mv_object_detection_3d_result {
-       unsigned int probability;
-       unsigned int number_of_points;
-       mv_point_s points[9];
-       unsigned int number_of_edges;
-       unsigned int edge_indexes[12][2];
-} mv_object_detection_3d_result_s;
-
-/**
  * @brief The object detection 3d object handle.
  *
  * @since_tizen 7.0
index 940668e..420e050 100644 (file)
@@ -31,7 +31,7 @@ public:
        virtual void prepare() = 0;
        virtual void setInput(T &t) = 0;
        virtual void perform() = 0;
-       virtual V &getOutput() = 0;
+       virtual VgetOutput() = 0;
 };
 } // namespace
 } // namespace
index c2fb953..18c28bb 100644 (file)
@@ -133,10 +133,11 @@ public:
        int RegisterNewFace(mv_source_h img_src, std::string label_name);
        int RecognizeFace(mv_source_h img_src);
        int DeleteLabel(std::string label_name);
-       mv_face_recognition_result_s &GetResult();
+       int GetLabel(const char **out_label);
+       mv_face_recognition_result_s* GetResult();
 };
 
 } // machine_learning
 } // mediavision
 
-#endif
\ No newline at end of file
+#endif
index d77d938..253c395 100644 (file)
@@ -81,7 +81,7 @@ public:
        void prepare() override;
        void setInput(T &t) override;
        void perform() override;
-       V &getOutput() override;
+       VgetOutput() override;
 };
 
 } // machine_learning
index 633e0bf..6e71086 100644 (file)
@@ -644,7 +644,26 @@ int FaceRecognition::DeleteLabel(string label_name)
        return MEDIA_VISION_ERROR_NONE;
 }
 
-mv_face_recognition_result_s &FaceRecognition::GetResult()
+int FaceRecognition::GetLabel(const char **out_label)
+{
+       if (_status != INFERENCED) {
+               LOGE("Inference not completed yet.");
+               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+       }
+
+       try {
+               _label_manager->GetLabelString(_result.label, _result.label_idx);
+       } catch (const BaseException &e) {
+               LOGE("%s", e.what());
+               return e.getError();
+       }
+
+       *out_label = _result.label.c_str();
+
+       return MEDIA_VISION_ERROR_NONE;
+}
+
+mv_face_recognition_result_s* FaceRecognition::GetResult()
 {
        if (_status != INFERENCED)
                throw InvalidOperation("Inference not completed yet.");
@@ -659,8 +678,8 @@ mv_face_recognition_result_s &FaceRecognition::GetResult()
                throw e;
        }
 
-       return _result;
+       return &_result;
 }
 
 } // machine_learning
-} // mediavision
\ No newline at end of file
+} // mediavision
index 7961901..b018b84 100644 (file)
@@ -127,7 +127,7 @@ template<typename T, typename V> void FaceRecognitionAdapter<T, V>::perform()
        }
 }
 
-template<typename T, typename V> V &FaceRecognitionAdapter<T, V>::getOutput()
+template<typename T, typename V> VFaceRecognitionAdapter<T, V>::getOutput()
 {
        return _face_recognition->GetResult();
 }
index 5d305f3..c5c7713 100644 (file)
@@ -213,7 +213,7 @@ int mv_face_recognition_get_label_open(mv_face_recognition_h handle, const char
                Context *context = static_cast<Context *>(handle);
                auto task = static_cast<FaceRecognitionTask *>(context->__tasks["face_recognition"]);
 
-               *out_label = task->getOutput().label.c_str();
+               *out_label = task->getOutput()->label.c_str();
        } catch (const BaseException &e) {
                LOGE("%s", e.what());
                return e.getError();
index 29a94be..edec702 100644 (file)
@@ -123,22 +123,65 @@ extern "C"
        int mv_object_detection_3d_inference_open(mv_object_detection_3d_h handle, mv_source_h source);
 
        /**
-        * @brief Get a label name and store it to @a out_label.
-        * @details Use this function to get a label name after calling mv_object_detection_3d_inference_open function.
+        * @brief Gets the probability value to the detected object.
+        * @details Use this function to get the probability value after calling @ref mv_object_detection_3d_inference().
         *
         * @since_tizen 7.0
         *
-        * @param[in] handle         The handle to the object detection 3d object.
-        * @param[out]
+        * @remarks The @a result must NOT be released using free()
+        *
+        * @param[in] handle              The handle to the face recognition object.
+        * @param[out] out_probability    A pointer to probability array.
+        *
+        * @return @c 0 on success, otherwise a negative error value
+        * @retval #MEDIA_VISION_ERROR_NONE Successful
+        * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
+        * @retval #MEDIA_VISION_ERROR_INVALID_OPERATION Invalid operation
+        *
+        * @pre Request an inference by calling @ref mv_object_detection_3d_inference()
+        */
+       int mv_object_detection_3d_get_probability_open(mv_object_detection_3d_h handle, unsigned int *out_probability);
+
+       /**
+        * @brief Gets the number of points to the 3D bounding box of the detected object.
+        * @details Use this function to get the number of points after calling @ref mv_object_detection_3d_inference().
+        *
+        * @since_tizen 7.0
+        *
+        * @remarks The @a result must NOT be released using free()
+        *
+        * @param[in] handle                The handle to the face recognition object.
+        * @param[out] out_num_of_points    Number of points.
+        *
+        * @return @c 0 on success, otherwise a negative error value
+        * @retval #MEDIA_VISION_ERROR_NONE Successful
+        * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
+        * @retval #MEDIA_VISION_ERROR_INVALID_OPERATION Invalid operation
+        *
+        * @pre Request an inference by calling @ref mv_object_detection_3d_inference()
+        */
+       int mv_object_detection_3d_get_num_of_points_open(mv_object_detection_3d_h handle, unsigned int *out_num_of_points);
+
+       /**
+        * @brief Gets the x and y coordinates values to the 3D bounding box of the detected object.
+        * @details Use this function to get the coordinates values after calling @ref mv_object_detection_3d_inference().
+        *
+        * @since_tizen 7.0
+        *
+        * @remarks The @a result must NOT be released using free()
+        *
+        * @param[in] handle    The handle to the face recognition object.
+        * @param[out] out_x    A pointer to x coordinates array.
+        * @param[out] out_y    A pointer to y coordinates array.
         *
         * @return @c 0 on success, otherwise a negative error value
         * @retval #MEDIA_VISION_ERROR_NONE Successful
         * @retval #MEDIA_VISION_ERROR_INVALID_PARAMETER Invalid parameter
         * @retval #MEDIA_VISION_ERROR_INVALID_OPERATION Invalid operation
         *
-        * @pre Request a inference by calling @ref mv_object_detection_3d_inference_open()
+        * @pre Request an inference by calling @ref mv_object_detection_3d_inference()
         */
-       int mv_object_detection_3d_get_result_open(mv_object_detection_3d_h handle, mv_object_detection_3d_result_s *out_result);
+       int mv_object_detection_3d_get_points_open(mv_object_detection_3d_h handle, unsigned int **out_x, unsigned int **out_y);
 
 #ifdef __cplusplus
 }
index 8ad9cda..b1e2a8d 100644 (file)
@@ -47,7 +47,7 @@ public:
        void configure();
        void prepare();
        void inference(mv_source_h source);
-       virtual object_detection_3d_result_s& getResult() = 0;
+       virtual object_detection_3d_result_s* getResult() = 0;
 };
 
 } // machine_learning
index 6aff8eb..dcf136c 100644 (file)
@@ -45,7 +45,7 @@ public:
        void prepare() override;
        void setInput(T &t) override;
        void perform() override;
-       V &getOutput() override;
+       VgetOutput() override;
 };
 
 } // machine_learning
index 980598b..1131c9e 100644 (file)
@@ -36,7 +36,8 @@ typedef struct {
 typedef struct {
        unsigned int probability;
        unsigned int number_of_points;
-       mv_point_s points[9];
+       unsigned int x[9];
+       unsigned int y[9];
        unsigned int number_of_edges;
        unsigned int edge_indexes[12][2];
 } object_detection_3d_result_s;
index bfe3f9f..4087fd6 100644 (file)
@@ -36,7 +36,7 @@ public:
        Objectron();
        ~Objectron();
        void parseMetaFile() override;
-       object_detection_3d_result_s& getResult() override;
+       object_detection_3d_result_s* getResult() override;
 };
 
 } // machine_learning
index 94f876d..cc2f81f 100644 (file)
@@ -105,19 +105,58 @@ int mv_object_detection_3d_inference(mv_object_detection_3d_h infer, mv_source_h
        return ret;
 }
 
-int mv_object_detection_3d_get_result(mv_object_detection_3d_h handle, mv_object_detection_3d_result_s *out_result)
+int mv_object_detection_3d_get_probability(mv_object_detection_3d_h handle, unsigned int *out_probability)
 {
        MEDIA_VISION_SUPPORT_CHECK(
                _mv_inference_face_check_system_info_feature_supported());
 
        MEDIA_VISION_INSTANCE_CHECK(handle);
-       MEDIA_VISION_NULL_ARG_CHECK(out_result);
+       MEDIA_VISION_NULL_ARG_CHECK(out_probability);
 
        MEDIA_VISION_FUNCTION_ENTER();
 
        int ret = MEDIA_VISION_ERROR_NONE;
 
-       ret = mv_object_detection_3d_get_result_open(handle, out_result);
+       ret = mv_object_detection_3d_get_probability_open(handle, out_probability);
+
+       MEDIA_VISION_FUNCTION_LEAVE();
+
+       return ret;
+}
+
+int mv_object_detection_3d_get_num_of_points(mv_object_detection_3d_h handle, unsigned int *out_num_of_points)
+{
+       MEDIA_VISION_SUPPORT_CHECK(
+               _mv_inference_face_check_system_info_feature_supported());
+
+       MEDIA_VISION_INSTANCE_CHECK(handle);
+       MEDIA_VISION_NULL_ARG_CHECK(out_num_of_points);
+
+       MEDIA_VISION_FUNCTION_ENTER();
+
+       int ret = MEDIA_VISION_ERROR_NONE;
+
+       ret = mv_object_detection_3d_get_num_of_points_open(handle, out_num_of_points);
+
+       MEDIA_VISION_FUNCTION_LEAVE();
+
+       return ret;
+}
+
+int mv_object_detection_3d_get_points(mv_object_detection_3d_h handle, unsigned int **out_x, unsigned int **out_y)
+{
+       MEDIA_VISION_SUPPORT_CHECK(
+               _mv_inference_face_check_system_info_feature_supported());
+
+       MEDIA_VISION_INSTANCE_CHECK(handle);
+       MEDIA_VISION_NULL_ARG_CHECK(out_x);
+       MEDIA_VISION_NULL_ARG_CHECK(out_y);
+
+       MEDIA_VISION_FUNCTION_ENTER();
+
+       int ret = MEDIA_VISION_ERROR_NONE;
+
+       ret = mv_object_detection_3d_get_points_open(handle, out_x, out_y);
 
        MEDIA_VISION_FUNCTION_LEAVE();
 
index 3705a54..16bfe16 100644 (file)
@@ -164,7 +164,7 @@ int mv_object_detection_3d_inference_open(mv_object_detection_3d_h handle, mv_so
        return MEDIA_VISION_ERROR_NONE;
 }
 
-int mv_object_detection_3d_get_result_open(mv_object_detection_3d_h handle, mv_object_detection_3d_result_s *out_result)
+int mv_object_detection_3d_get_probability_open(mv_object_detection_3d_h handle, unsigned int *out_probability)
 {
        LOGD("ENTER");
 
@@ -177,24 +177,70 @@ int mv_object_detection_3d_get_result_open(mv_object_detection_3d_h handle, mv_o
                auto context = static_cast<Context *>(handle);
                auto task = static_cast<ObjectDetectionTask *>(context->__tasks["objectron"]);
 
-               object_detection_3d_result_s result = task->getOutput();
+               object_detection_3d_result_s *result = task->getOutput();
 
-               out_result->probability = result.probability;
-               out_result->number_of_points = result.number_of_points;
-               out_result->number_of_edges = result.number_of_edges;
+               *out_probability = result->probability;
+       } catch (const BaseException &e) {
+               LOGE("%s", e.what());
+               return e.getError();
+       }
+
+       LOGD("LEAVE");
+
+       return MEDIA_VISION_ERROR_NONE;
+}
+
+int mv_object_detection_3d_get_num_of_points_open(mv_object_detection_3d_h handle, unsigned int *out_num_of_points)
+{
+       LOGD("ENTER");
+
+       if (!handle) {
+               LOGE("Handle is NULL.");
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+       }
+
+       try {
+               auto context = static_cast<Context *>(handle);
+               auto task = static_cast<ObjectDetectionTask *>(context->__tasks["objectron"]);
+
+               auto result = task->getOutput();
 
-               for (unsigned int idx = 0; idx < out_result->number_of_points; ++idx)
-                       out_result->points[idx] = result.points[idx];
+               *out_num_of_points = result->number_of_points;
+       } catch (const BaseException &e) {
+               LOGE("%s", e.what());
+               return e.getError();
+       }
+
+       LOGD("LEAVE");
+
+       return MEDIA_VISION_ERROR_NONE;
+}
+
+int mv_object_detection_3d_get_points_open(mv_object_detection_3d_h handle, unsigned int **out_x, unsigned int **out_y)
+{
+       LOGD("ENTER");
+
+       if (!handle) {
+               LOGE("Handle is NULL.");
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+       }
 
-               for (unsigned int idx = 0; idx < out_result->number_of_edges; ++idx) {
-                       out_result->edge_indexes[idx][0] = result.edge_indexes[idx][0];
-                       out_result->edge_indexes[idx][1] = result.edge_indexes[idx][1];
-               }
+       try {
+               Context *context = static_cast<Context *>(handle);
+               auto task = static_cast<ObjectDetectionTask *>(context->__tasks["objectron"]);
+
+               auto result = task->getOutput();
+
+               *out_x = result->x;
+               *out_y = result->y;
        } catch (const BaseException &e) {
                LOGE("%s", e.what());
                return e.getError();
        }
 
+       for (unsigned int i = 0; i < 9; ++i)
+               LOGI("%d %d", (*out_x)[i], (*out_y)[i]);
+
        LOGD("LEAVE");
 
        return MEDIA_VISION_ERROR_NONE;
index 7d95585..b1b9ce7 100644 (file)
@@ -80,7 +80,7 @@ template<typename T, typename V> void ObjectDetectionAdapter<T, V>::perform()
        }
 }
 
-template<typename T, typename V> V& ObjectDetectionAdapter<T, V>::getOutput()
+template<typename T, typename V> V* ObjectDetectionAdapter<T, V>::getOutput()
 {
        return _object_detection->getResult();
 }
index 9bb7d2b..df229f0 100644 (file)
@@ -80,7 +80,7 @@ void Objectron::parseMetaFile()
                throw InvalidOperation("Fail to ParseMetadata");
 }
 
-object_detection_3d_result_s& Objectron::getResult()
+object_detection_3d_result_s* Objectron::getResult()
 {
        TensorBuffer& tensor_buffer_obj = _inference->GetOutputTensorBuffer();
        IETensorBuffer &ie_tensor_buffer = tensor_buffer_obj.getIETensorBuffer();
@@ -108,8 +108,8 @@ object_detection_3d_result_s& Objectron::getResult()
        float y_scale = static_cast<float>(_inference->getSourceHeight()) / static_cast<float>(_inference->getInputHeight());
 
        for (unsigned int idx = 0; idx < output_size; idx += 2) {
-               _result.points[result_idx].x = static_cast<int>(keypoints[idx] * x_scale);
-               _result.points[result_idx++].y = static_cast<int>(keypoints[idx + 1] * y_scale);
+               _result.x[result_idx] = static_cast<int>(keypoints[idx] * x_scale);
+               _result.y[result_idx++] = static_cast<int>(keypoints[idx + 1] * y_scale);
        }
 
        _result.number_of_points = output_size / 2;
@@ -133,7 +133,7 @@ object_detection_3d_result_s& Objectron::getResult()
 
        ::copy(&edges[0][0], &edges[0][0] + 12 * 2, &_result.edge_indexes[0][0]);
 
-       return _result;
+       return &_result;
 }
 
 }
index f349413..0fc8db7 100644 (file)
@@ -55,21 +55,26 @@ TEST(ObjectDetection3DTest, InferenceShouldBeOk)
        ret = mv_object_detection_3d_inference(handle, mv_source);
        ASSERT_EQ(ret, 0);
 
-       mv_object_detection_3d_result_s result;
-       ret = mv_object_detection_3d_get_result(handle, &result);
+       unsigned int probability;
+
+       ret = mv_object_detection_3d_get_probability(handle, &probability);
        ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
 
-       std::cout << "Probability = " << result.probability << std::endl;
+       std::cout << "Probability = " << probability << std::endl;
 
-       for (unsigned int idx = 0; idx < result.number_of_points; ++idx) {
-               std::cout << "index = " << idx + 1 << " : " << result.points[idx].x
-                                << " x " << result.points[idx].y << std::endl;
-       }
+       unsigned int num_of_points;
+
+       ret = mv_object_detection_3d_get_num_of_points(handle, &num_of_points);
+       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
 
-       std::cout << "Number of edges = " << result.number_of_edges << std::endl;
+       unsigned int *x_array, *y_array;
 
-       for (unsigned int idx = 0; idx < result.number_of_edges; ++idx)
-               std::cout << result.edge_indexes[idx][0] << " to " << result.edge_indexes[idx][1] << std::endl;
+       ret = mv_object_detection_3d_get_points(handle, &x_array, &y_array);
+       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+       for (unsigned int idx = 0; idx < num_of_points; ++idx) {
+               std::cout << "index = " << idx + 1 << " : " << x_array[idx] << " x " << y_array[idx] << std::endl;
+       }
 
        ret = mv_destroy_source(mv_source);
        ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);