mv_machine_learning: add async API for face detection task group
[platform/core/api/mediavision.git] / test / testsuites / machine_learning / object_detection / test_object_detection_async.cpp
index e5f0536..65cca28 100644 (file)
 
 #include "ImageHelper.h"
 #include "mv_object_detection_internal.h"
+#include "mv_face_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 MAX_INFERENCE_ITERATION 20
 
 using namespace testing;
@@ -40,7 +42,7 @@ struct model_info {
        mv_source_h source;
 };
 
-void completion_callback(mv_object_detection_h handle, void *user_data)
+void object_detection_completion_callback(mv_object_detection_h handle, void *user_data)
 {
        unsigned int number_of_objects;
        const int *left, *top, *right, *bottom;
@@ -50,7 +52,7 @@ void completion_callback(mv_object_detection_h handle, void *user_data)
 
        int ret = mv_object_detection_get_result(handle, &number_of_objects, &indices, &confidences, &left, &top, &right,
                                                                                         &bottom);
-       ASSERT_EQ(ret, 0);
+       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
 
        for (unsigned int idx = 0; idx < number_of_objects; ++idx) {
                cout << "index = " << indices[idx] << " probability = " << confidences[idx] << " " << left[idx] << " x "
@@ -61,14 +63,14 @@ void completion_callback(mv_object_detection_h handle, void *user_data)
                const char *label;
 
                ret = mv_object_detection_get_label(handle, indices[idx], &label);
-               ASSERT_EQ(ret, 0);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
                cout << "index = " << indices[idx] << " label = " << label << endl;
 
                string label_str(label);
 
                transform(label_str.begin(), label_str.end(), label_str.begin(), ::toupper);
 
-               ASSERT_TRUE(label_str == test_model->answer);
+               ASSERT_EQ(label_str, test_model->answer);
        }
 }
 
@@ -108,9 +110,9 @@ TEST(ObjectDetectionAsyncTest, InferenceShouldBeOk)
 
                        model.source = mv_source;
 
-                       ret = mv_object_detection_inference_async(handle, mv_source, completion_callback,
+                       ret = mv_object_detection_inference_async(handle, mv_source, object_detection_completion_callback,
                                                                                                          reinterpret_cast<void *>(&model));
-                       ASSERT_EQ(ret, 0);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
 
                        ret = mv_destroy_source(mv_source);
                        ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
@@ -119,4 +121,83 @@ TEST(ObjectDetectionAsyncTest, InferenceShouldBeOk)
                ret = mv_object_detection_destroy(handle);
                ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
        }
+}
+
+void face_detection_completion_callback(mv_object_detection_h handle, void *user_data)
+{
+       unsigned int number_of_objects;
+       const int *left, *top, *right, *bottom;
+       const unsigned int *indices;
+       const float *confidences;
+       model_info *test_model(static_cast<model_info *>(user_data));
+
+       int ret = mv_face_detection_get_result(handle, &number_of_objects, &indices, &confidences, &left, &top, &right,
+                                                                                  &bottom);
+       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+       for (unsigned int idx = 0; idx < number_of_objects; ++idx) {
+               cout << "index = " << indices[idx] << " probability = " << confidences[idx] << " " << left[idx] << " x "
+                        << top[idx] << " ~ " << right[idx] << " x " << bottom[idx] << endl;
+       }
+
+       for (unsigned int idx = 0; idx < number_of_objects; ++idx) {
+               const char *label;
+
+               ret = mv_face_detection_get_label(handle, indices[idx], &label);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+               cout << "index = " << indices[idx] << " label = " << label << endl;
+
+               string label_str(label);
+
+               transform(label_str.begin(), label_str.end(), label_str.begin(), ::toupper);
+
+               ASSERT_EQ(label_str, test_model->answer);
+       }
+}
+
+TEST(FaceDetectionAsyncTest, InferenceShouldBeOk)
+{
+       mv_object_detection_h handle;
+       vector<model_info> test_models {
+               { "", "", "", "", "FACE" } // If empty then default model will be used.
+               // TODO.
+       };
+
+       for (auto &model : test_models) {
+               int ret = mv_face_detection_create(&handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               cout << "model name : " << model.model_file << endl;
+
+               mv_face_detection_set_model(handle, model.model_name.c_str(), model.model_file.c_str(), model.meta_file.c_str(),
+                                                                       model.label_file.c_str());
+               mv_face_detection_set_engine(handle, "tflite", "cpu");
+
+               ret = mv_face_detection_configure(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               ret = mv_face_detection_prepare(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+               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_FACE, mv_source);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+                       model.source = mv_source;
+
+                       ret = mv_face_detection_inference_async(handle, mv_source, face_detection_completion_callback,
+                                                                                                       reinterpret_cast<void *>(&model));
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+
+                       ret = mv_destroy_source(mv_source);
+                       ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+               }
+
+               ret = mv_face_detection_destroy(handle);
+               ASSERT_EQ(ret, MEDIA_VISION_ERROR_NONE);
+       }
 }
\ No newline at end of file