test: add hand detection model test case
authorInki Dae <inki.dae@samsung.com>
Mon, 22 Jul 2024 07:58:09 +0000 (16:58 +0900)
committerKwanghoon Son <k.son@samsung.com>
Thu, 26 Sep 2024 05:11:58 +0000 (14:11 +0900)
Change-Id: Ia752b12148b672e960e82cf14d8becfc3bba31b8
Signed-off-by: Inki Dae <inki.dae@samsung.com>
test/testsuites/machine_learning/object_detection/test_object_detection.cpp
test/testsuites/machine_learning/object_detection/test_object_detection_async.cpp

index 151aafd5e9346afead5cdfc7c279e01ae1ce0045..6ab0410f924f4cef9f3e0d162f271e0eec1d6585 100644 (file)
 #include "ImageHelper.h"
 #include "mv_face_detection.h"
 #include "mv_face_detection_internal.h"
+#include "mv_hand_detection.h"
+#include "mv_hand_detection_internal.h"
 #include "mv_object_detection.h"
 #include "mv_object_detection_internal.h"
 
 #define IMG_DOG TEST_RES_PATH "/res/inference/images/dog2.jpg"
 #define IMG_FACE TEST_RES_PATH "/res/inference/images/faceDetection.jpg"
+#define IMG_HAND TEST_RES_PATH "/res/inference/images/handDetection.jpg"
 
 using namespace testing;
 using namespace std;
@@ -109,6 +112,43 @@ TEST(FaceDetectionTest, GettingAvailableInferenceEnginesInfoShouldBeOk)
        ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
 }
 
+TEST(HandDetectionTest, GettingAvailableInferenceEnginesInfoShouldBeOk)
+{
+       mv_object_detection_h handle;
+
+       int ret = mv_hand_detection_create(&handle);
+       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+       unsigned int engine_count = 0;
+
+       ret = mv_hand_detection_get_engine_count(handle, &engine_count);
+       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+       ASSERT_GE(engine_count, 1);
+
+       for (unsigned int engine_idx = 0; engine_idx < engine_count; ++engine_idx) {
+               char *engine_type = nullptr;
+
+               ret = mv_hand_detection_get_engine_type(handle, engine_idx, &engine_type);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               unsigned int device_count = 0;
+
+               ret = mv_hand_detection_get_device_count(handle, engine_type, &device_count);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+               ASSERT_GE(device_count, 1);
+
+               for (unsigned int device_idx = 0; device_idx < device_count; ++device_idx) {
+                       char *device_type = nullptr;
+
+                       ret = mv_hand_detection_get_device_type(handle, engine_type, device_idx, &device_type);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+               }
+       }
+
+       ret = mv_hand_detection_destroy(handle);
+       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+}
+
 TEST(ObjectDetectionTest, InferenceShouldBeOk)
 {
        mv_object_detection_h handle;
@@ -246,3 +286,65 @@ TEST(FaceDetectionTest, InferenceShouldBeOk)
        ret = mv_destroy_source(mv_source);
        ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
 }
+
+TEST(HandDetectionTest, InferenceShouldBeOk)
+{
+       mv_object_detection_h handle;
+       vector<test_model_input> test_models {
+               {} // If empty then default model will be used.
+               // TODO.
+       };
+       const int coordinate_answers[][4] = { { 0, 125, 93, 181 }, { 118, 112, 211, 167 }, { 299, 141, 419, 211 } };
+
+       mv_source_h mv_source = NULL;
+       int ret = mv_create_source(&mv_source);
+       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+       ret = ImageHelper::loadImageToSource(IMG_HAND, mv_source);
+       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+       for (auto &model : test_models) {
+               ret = mv_hand_detection_create(&handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_set_model(handle, model.model_file.c_str(), model.meta_file.c_str(),
+                                                                                 model.label_file.c_str(), NULL);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_set_engine(handle, "tflite", "cpu");
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_configure(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_prepare(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_inference(handle, mv_source);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               unsigned long frame_number;
+               unsigned int number_of_objects;
+
+               ret = mv_hand_detection_get_result_count(handle, &frame_number, &number_of_objects);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               for (unsigned int idx = 0; idx < number_of_objects; ++idx) {
+                       int left, top, right, bottom;
+
+                       int ret = mv_hand_detection_get_bound_box(handle, idx, &left, &top, &right, &bottom);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+                       ASSERT_EQ(coordinate_answers[idx][0], left);
+                       ASSERT_EQ(coordinate_answers[idx][1], top);
+                       ASSERT_EQ(coordinate_answers[idx][2], right);
+                       ASSERT_EQ(coordinate_answers[idx][3], bottom);
+               }
+
+               ret = mv_hand_detection_destroy(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+       }
+
+       ret = mv_destroy_source(mv_source);
+       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+}
index 94c44246f4e046b39264bf09391ec76b179eabde..86d0e271413aee0fdc62f5051b9dcf90aa04c0ea 100644 (file)
 #include "ImageHelper.h"
 #include "mv_face_detection.h"
 #include "mv_face_detection_internal.h"
+#include "mv_hand_detection.h"
+#include "mv_hand_detection_internal.h"
 #include "mv_object_detection.h"
 #include "mv_object_detection_internal.h"
 
 #define IMG_DOG TEST_RES_PATH "/res/inference/images/dog2.jpg"
 #define IMG_FACE TEST_RES_PATH "/res/inference/images/faceDetection.jpg"
+#define IMG_HAND TEST_RES_PATH "/res/inference/images/handDetection.jpg"
 #define MAX_INFERENCE_ITERATION 50
 
 using namespace testing;
@@ -307,6 +310,144 @@ TEST(FaceDetectionAsyncTest, InferenceShouldBeOkWithDestroyFirst)
                ret = mv_face_detection_destroy(handle);
                ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
 
+               thread_handle->join();
+       }
+}
+
+void hand_detection_callback(void *user_data)
+{
+       mv_hand_detection_h handle = static_cast<mv_object_detection_h>(user_data);
+       const int coordinate_answers[][4] = { { 0, 125, 93, 181 }, { 118, 112, 211, 167 }, { 299, 141, 419, 211 } };
+
+       bool is_loop_exit = false;
+
+       while (!is_loop_exit) {
+               unsigned long frame_number;
+               unsigned int number_of_objects;
+
+               int ret = mv_hand_detection_get_result_count(handle, &frame_number, &number_of_objects);
+               if (ret == MEDIA_VISION_ERROR_INVALID_OPERATION)
+                       break;
+
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               for (unsigned int idx = 0; idx < number_of_objects; ++idx) {
+                       int left, top, right, bottom;
+
+                       int ret = mv_hand_detection_get_bound_box(handle, idx, &left, &top, &right, &bottom);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+                       ASSERT_EQ(coordinate_answers[idx][0], left);
+                       ASSERT_EQ(coordinate_answers[idx][1], top);
+                       ASSERT_EQ(coordinate_answers[idx][2], right);
+                       ASSERT_EQ(coordinate_answers[idx][3], bottom);
+
+                       if (frame_number > MAX_INFERENCE_ITERATION - 10)
+                               is_loop_exit = true;
+               }
+       }
+}
+
+TEST(HandDetectionAsyncTest, InferenceShouldBeOk)
+{
+       mv_hand_detection_h handle;
+       vector<test_model_input> test_models {
+               {} // If empty then default model will be used.
+               // TODO.
+       };
+
+       for (auto &model : test_models) {
+               int ret = mv_hand_detection_create(&handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_set_model(handle, model.model_file.c_str(), model.meta_file.c_str(),
+                                                                                 model.label_file.c_str(), NULL);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_set_engine(handle, "tflite", "cpu");
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_configure(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_prepare(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               unique_ptr<thread> thread_handle;
+
+               for (unsigned int iter = 0; iter < MAX_INFERENCE_ITERATION; ++iter) {
+                       mv_source_h mv_source = NULL;
+                       ret = mv_create_source(&mv_source);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+                       ret = ImageHelper::loadImageToSource(IMG_HAND, mv_source);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+                       ret = mv_hand_detection_inference_async(handle, mv_source);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+                       if (iter == 0)
+                               thread_handle = make_unique<thread>(&hand_detection_callback, static_cast<void *>(handle));
+
+                       ret = mv_destroy_source(mv_source);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+               }
+
+               thread_handle->join();
+
+               ret = mv_hand_detection_destroy(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+       }
+}
+
+TEST(HandDetectionAsyncTest, InferenceShouldBeOkWithDestroyFirst)
+{
+       mv_hand_detection_h handle;
+       vector<test_model_input> test_models {
+               {} // If empty then default model will be used.
+               // TODO.
+       };
+
+       for (auto &model : test_models) {
+               int ret = mv_hand_detection_create(&handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_set_model(handle, model.model_file.c_str(), model.meta_file.c_str(),
+                                                                                 model.label_file.c_str(), NULL);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_set_engine(handle, "tflite", "cpu");
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_configure(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_hand_detection_prepare(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               unique_ptr<thread> thread_handle;
+
+               for (unsigned int iter = 0; iter < MAX_INFERENCE_ITERATION; ++iter) {
+                       mv_source_h mv_source = NULL;
+                       ret = mv_create_source(&mv_source);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+                       ret = ImageHelper::loadImageToSource(IMG_HAND, mv_source);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+                       ret = mv_hand_detection_inference_async(handle, mv_source);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+                       if (iter == 0)
+                               thread_handle = make_unique<thread>(&hand_detection_callback, static_cast<void *>(handle));
+
+                       ret = mv_destroy_source(mv_source);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+               }
+
+               ret = mv_hand_detection_destroy(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
                thread_handle->join();
        }
 }
\ No newline at end of file