mv_machine_learning: introduce a new internal API for face recognition
authorInki Dae <inki.dae@samsung.com>
Tue, 23 May 2023 05:31:08 +0000 (14:31 +0900)
committerKwanghoon Son <k.son@samsung.com>
Tue, 4 Jul 2023 05:08:39 +0000 (14:08 +0900)
[Issue type] : new feature

Introduce new internal API - mv_face_recognition_get_confidence - for
face recognition task group, which provides raw tensor data to user.

Change-Id: I49b61dce7c97b00a16b7f1c49e0a2b96a4a17d2f
Signed-off-by: Inki Dae <inki.dae@samsung.com>
include/mv_face_recognition_internal.h [new file with mode: 0644]
mv_machine_learning/face_recognition/CMakeLists.txt
mv_machine_learning/face_recognition/src/mv_face_recognition.cpp
test/testsuites/machine_learning/face_recognition/test_face_recognition.cpp

diff --git a/include/mv_face_recognition_internal.h b/include/mv_face_recognition_internal.h
new file mode 100644 (file)
index 0000000..9ba4677
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * 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 __TIZEN_MEDIAVISION_MV_FACE_RECOGNITION_INTERNAL_H__
+#define __TIZEN_MEDIAVISION_MV_FACE_RECOGNITION_INTERNAL_H__
+
+#include <mv_common.h>
+#include <mv_face_recognition_type.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+/**
+ * @brief Gets confidences after a given face image is recognized.
+ * @details Use this function to get the confidences calling @ref mv_face_recognition_inference().
+ *
+ * @since_tizen 8.0
+ *
+ * @remarks The @a num_of_confidences and @a confidences must NOT be released using free()
+ *
+ * @param[in] handle              The handle to the face recognition object.
+ * @param[out] confidences        The array pointer to the confidence table which contains a confidence value of each class.
+ *                                This function returns memory pointer containing actual confidence values to @a confidences.
+ *                                And please note that @a confidences is valid only while handle is alive.
+ * @param[out] num_of_confidences A number of confidences to the classes registered.
+ *
+ * @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_face_recognition_inference()
+ */
+int mv_face_recognition_get_confidence(mv_face_recognition_h handle, const float **confidences,
+                                                                          size_t *num_of_confidences);
+
+/**
+ * @}
+ */
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __TIZEN_MEDIAVISION_MV_FACE_RECOGNITION_INTERNAL_H__ */
index c497a807d943f2a6f9e57333d1b710f5fdd36888..48b8a4fc8f346275a443275a7b1e3698eeeeee01 100644 (file)
@@ -23,5 +23,6 @@ install(
        DIRECTORY ${PROJECT_SOURCE_DIR}/../../include/ DESTINATION include/media
        FILES_MATCHING
        PATTERN "mv_face_recognition.h"
+       PATTERN "mv_face_recognition_internal.h"
        PATTERN "mv_face_recognition_type.h"
        )
\ No newline at end of file
index 9256f267fe4c592df8bdcfe1416fa764f272f566..61384807b5f180a0172f00ff2c3a44b368c2ad5a 100644 (file)
@@ -25,6 +25,7 @@
 #include "face_recognition_adapter.h"
 #include "facenet_adapter.h"
 #include "mv_face_recognition.h"
+#include "mv_face_recognition_internal.h"
 
 using namespace std;
 using namespace mediavision::common;
@@ -288,3 +289,32 @@ int mv_face_recognition_get_label(mv_face_recognition_h handle, const char **out
 
        return MEDIA_VISION_ERROR_NONE;
 }
+
+int mv_face_recognition_get_confidence(mv_face_recognition_h handle, const float **confidences,
+                                                                          size_t *num_of_confidences)
+{
+       lock_guard<mutex> lock(g_face_recognition_mutex);
+
+       MEDIA_VISION_SUPPORT_CHECK(_mv_inference_face_check_system_info_feature_supported());
+
+       MEDIA_VISION_INSTANCE_CHECK(handle);
+       MEDIA_VISION_NULL_ARG_CHECK(confidences);
+       MEDIA_VISION_NULL_ARG_CHECK(num_of_confidences);
+
+       MEDIA_VISION_FUNCTION_ENTER();
+
+       try {
+               auto context = static_cast<Context *>(handle);
+               auto face_recognition_task = static_cast<FaceRecognitionTask *>(context->__tasks["face_recognition"]);
+
+               *confidences = face_recognition_task->getOutput().raw_data.data();
+               *num_of_confidences = face_recognition_task->getOutput().raw_data.size();
+       } 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
index 015ca4cd96fe1c4d78bc43e188becc26fdd0e517..181c1ad1fa794ccf56ccf306ee0543a84a41b0e9 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "ImageHelper.h"
 #include "mv_face_recognition.h"
+#include "mv_face_recognition_internal.h"
 #include "face_recognition_test_util.h"
 
 #define TRAINING_IMAGE_PATH "/home/owner/media/res/face_recognition/res/test/training/"
@@ -117,6 +118,16 @@ TEST(FaceRecognitionTest, InferenceAfterTrainingShouldBeOk)
 
                ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
 
+               size_t num_of_confidences = 0;
+               const float *confidences = nullptr;
+
+               ret = mv_face_recognition_get_confidence(handle, &confidences, &num_of_confidences);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               for (unsigned int idx = 0; idx < num_of_confidences; ++idx)
+                       cout << confidences[idx] << " ";
+               cout << endl;
+
                string label_str(out_label);
 
                if (answers[image_idx++] == label_str)