test: Add Post Estimation test
authorInki Dae <inki.dae@samsung.com>
Tue, 25 Feb 2020 02:52:37 +0000 (11:52 +0900)
committerInki Dae <inki.dae@samsung.com>
Tue, 14 Apr 2020 00:41:08 +0000 (09:41 +0900)
This patch adds post estimation test code,
and ARMNN backend support of post estimation inference.

Change-Id: I1d433e36ba18b19678b87e0315ea4a21cff56f54
Signed-off-by: Inki Dae <inki.dae@samsung.com>
test/testsuites/inference/inference_test_suite.c

index 186bfd5..9a27c36 100644 (file)
 //Facila LandmarkDetection
 #define FLD_TFLITE_WEIGHT_PATH "/usr/share/capi-media-vision/models/FLD/tflite/fld_tflite_model1.tflite"
 
+//Pose Estimation
+#define PE_TFLITE_WEIGHT_PATH "/usr/share/capi-media-vision/models/PE/tflite/ped_tflite_model.tflite"
+
 /******
  * Public model:
  *  IC: mobilenet caffe, tf?
  *  OD: mobilenetv1-ssd caffe, tf?
  *  FD: caffe, tf
  *  FLD: caffe, tf
+ *  PE: cpm model, tf and tflite.
+ *      link : https://github.com/edvardHua/PoseEstimationForMobile/tree/master/release/cpm_model
+ *      Ps. media vision supports cpm and hourglass models for pose estimaion for now.
  */
 
 void _object_detected_cb (
@@ -111,6 +117,18 @@ void _facial_landmark_detected_cb (
     }
 }
 
+void _pose_estimation_detected_cb (
+        mv_source_h source,
+        const int number_of_pose_estimation,
+        const mv_point_s *locations,
+        void *user_data)
+{
+    printf("In callback, %d pose estimation\n", number_of_pose_estimation);
+    for (int n = 0; n < number_of_pose_estimation; n++) {
+        printf("%d: x[%d], y[%d]\n", n, locations[n].x, locations[n].y);
+    }
+}
+
 void _image_classified_cb (
            mv_source_h source,
            const int number_of_classes,
@@ -1919,15 +1937,246 @@ int perform_facial_landmark_detection()
 
     return MEDIA_VISION_ERROR_NONE;
 }
+
+int perform_armnn_pose_estimation_detection(mv_engine_config_h *engine_cfg)
+{
+    int err = MEDIA_VISION_ERROR_NONE;
+
+    mv_engine_config_h handle = NULL;
+    err = mv_create_engine_config(&handle);
+    if (err != MEDIA_VISION_ERROR_NONE) {
+        printf("Fail to create engine configuration handle.\n");
+        if (handle) {
+            int err2 = mv_destroy_engine_config(handle);
+            if (err2 != MEDIA_VISION_ERROR_NONE) {
+                printf("Fail to destroy engine cofniguration.\n");
+            }
+        }
+        return err;
+    }
+
+    char *inputNodeName = "image";
+    char *outputNodeName[1] = {"Convolutional_Pose_Machine/stage_5_out"};
+
+    mv_engine_config_set_string_attribute(handle,
+                        MV_INFERENCE_MODEL_WEIGHT_FILE_PATH,
+                        PE_TFLITE_WEIGHT_PATH);
+
+    mv_engine_config_set_double_attribute(handle,
+                        MV_INFERENCE_MODEL_MEAN_VALUE,
+                        0.0);
+
+    mv_engine_config_set_double_attribute(handle,
+                        MV_INFERENCE_MODEL_STD_VALUE,
+                        1.0);
+
+    mv_engine_config_set_int_attribute(handle,
+                        MV_INFERENCE_BACKEND_TYPE,
+                        MV_INFERENCE_BACKEND_ARMNN);
+
+    mv_engine_config_set_int_attribute(handle,
+                        MV_INFERENCE_TARGET_TYPE,
+                        MV_INFERENCE_TARGET_CPU);
+
+    mv_engine_config_set_int_attribute(handle,
+                        MV_INFERENCE_INPUT_TENSOR_WIDTH,
+                        192);
+
+    mv_engine_config_set_int_attribute(handle,
+                        MV_INFERENCE_INPUT_TENSOR_HEIGHT,
+                        192);
+
+    mv_engine_config_set_int_attribute(handle,
+                        MV_INFERENCE_INPUT_TENSOR_CHANNELS,
+                        3);
+
+    mv_engine_config_set_string_attribute(handle,
+                        MV_INFERENCE_INPUT_NODE_NAME,
+                        inputNodeName);
+
+    mv_engine_config_set_array_string_attribute(handle,
+                        MV_INFERENCE_OUTPUT_NODE_NAMES,
+                        outputNodeName,
+                        1);
+
+    *engine_cfg = handle;
+    return err;
+}
+
+int perform_pose_estimation_detection()
+{
+    int err = MEDIA_VISION_ERROR_NONE;
+
+    int sel_opt = 0;
+    const int options[5] = {1, 2, 3, 4, 5};
+    const *names[5] = { "Configuration",
+                        "ARMNN(CPU) + PoseEstimation",
+                        "Prepare",
+                        "Run",
+                        "Back"};
+
+    mv_engine_config_h engine_cfg = NULL;
+    mv_inference_h infer = NULL;
+    mv_source_h mvSource = NULL;
+
+    while(sel_opt == 0) {
+        sel_opt = show_menu("Select Action:", options, names, 5);
+        switch (sel_opt) {
+        case 1:
+        {
+            //perform configuration
+            if (engine_cfg) {
+                int err2 = mv_destroy_engine_config(engine_cfg);
+                if (err2 != MEDIA_VISION_ERROR_NONE)
+                    printf("Fail to destroy engine_cfg [err:%i]\n", err2);
+            }
+
+            err = perform_configuration(&engine_cfg);
+        }
+            break;
+        case 2:
+        {
+            //perform pose estimation config
+            if (engine_cfg) {
+                int err2 = mv_destroy_engine_config(engine_cfg);
+                if (err2 != MEDIA_VISION_ERROR_NONE)
+                    printf("Fail to destroy engine_cfg [err:%i]\n", err2);
+            }
+            err = perform_armnn_pose_estimation_detection(&engine_cfg);
+        }
+            break;
+        case 3:
+        {
+            // create - configure - prepare
+            if (infer) {
+                int err2 = mv_inference_destroy(infer);
+                if (err2 != MEDIA_VISION_ERROR_NONE) {
+                    printf("Fail to destroy inference handle [err:%i]\n", err2);
+                }
+            }
+
+            // inference
+            // create handle
+            err = mv_inference_create(&infer);
+            if (err != MEDIA_VISION_ERROR_NONE) {
+                printf("Fail to create inference handle [err:%i]\n", err);
+                break;
+            }
+
+            //configure
+            err = mv_inference_configure(infer, engine_cfg);
+            if (err != MEDIA_VISION_ERROR_NONE) {
+                printf("Fail to configure inference handle [err:%i]\n", err);
+                break;
+            }
+
+            //prepare
+            err = mv_inference_prepare(infer);
+            if (err != MEDIA_VISION_ERROR_NONE) {
+                printf("Fail to prepare inference handle");
+                break;
+            }
+        }
+            break;
+        case 4:
+        {
+            if (mvSource) {
+                int err2 = mv_destroy_source(mvSource);
+                if (err2 != MEDIA_VISION_ERROR_NONE);
+                    printf("Fail to destroy mvSource\n");
+            }
+
+            char *in_file_name = NULL;
+            /* Load media source */
+            while (input_string("Input file name to be inferred:", 1024, &(in_file_name)) == -1)
+                printf("Incorrect input! Try again.\n");
+
+            err = mv_create_source(&mvSource);
+            if (err != MEDIA_VISION_ERROR_NONE) {
+                printf("Fail to create mvSource.\n");
+                free(in_file_name);
+                break;
+            }
+
+            err = load_mv_source_from_file(in_file_name, mvSource);
+            if (err != MEDIA_VISION_ERROR_NONE) {
+                int err2 = mv_destroy_source(mvSource);
+                if (err2 != MEDIA_VISION_ERROR_NONE) {
+                    printf("Fail to destroy mvSource.\n", err2);
+                }
+                free(in_file_name);
+                break;
+            }
+            free(in_file_name);
+
+            // Pose estimation
+            err = mv_inference_pose_estimation_detect(mvSource, infer, NULL, _pose_estimation_detected_cb, NULL);
+            break;
+        }
+        case 5:
+        {
+            //perform destroy
+            if (engine_cfg) {
+                err = mv_destroy_engine_config(engine_cfg);
+                if (err != MEDIA_VISION_ERROR_NONE) {
+                    printf("Fail to destroy engine_cfg [err:%i]\n", err);
+                }
+            }
+
+            if (infer) {
+                err = mv_inference_destroy(infer);
+                if (err != MEDIA_VISION_ERROR_NONE) {
+                    printf("Fail to destroy inference handle [err:%i]\n", err);
+                }
+            }
+        }
+            break;
+        default:
+            printf("Invalid option.\n");
+            sel_opt = 0;
+            continue;
+        }
+
+        int do_another = 0;
+        if (err != MEDIA_VISION_ERROR_NONE) {
+            printf("ERROR: Action is finished with error code: %i\n");
+        }
+
+        sel_opt = 0;
+        const int options_last[2] = {1, 2};
+        const char *names_last[2] = { "Yes", "No" };
+
+        while (sel_opt == 0) {
+            sel_opt = show_menu("Run Pose Estimation Detection again?:", options_last, names_last, 2);
+            switch(sel_opt) {
+            case 1:
+                do_another = 1;
+                break;
+            case 2:
+                do_another = 0;
+                break;
+            default:
+                printf("Invalid option.\n");
+                sel_opt = 0;
+            }
+        }
+
+        sel_opt = (do_another == 1) ? 0 : 1;
+    }
+
+    return MEDIA_VISION_ERROR_NONE;
+}
+
 int main()
 {
     int sel_opt = 0;
 
-    const int options[5] = {1, 2, 3, 4, 5};
-    const char *names[5] = { "Image Classification",
+    const int options[6] = {1, 2, 3, 4, 5, 6};
+    const char *names[6] = { "Image Classification",
                              "Object Detection",
                              "Face Detection",
                              "Facial LandmarkDetection",
+                             "Pose Estimation",
                              "Exit"};
 
     int err = MEDIA_VISION_ERROR_NONE;
@@ -1968,6 +2217,14 @@ int main()
         }
         case 5:
         {
+            err = perform_pose_estimation_detection();
+            if (err != MEDIA_VISION_ERROR_NONE) {
+                printf("Fail to perform pose estimation");
+            }
+            break;
+        }
+        case 6:
+        {
             printf("Exit");
         }
             break;