From 9b045d0c27ad4dd8b5a51e90dd1bdefb90b1b4c1 Mon Sep 17 00:00:00 2001 From: Kwang Son Date: Wed, 4 Aug 2021 03:06:26 -0400 Subject: [PATCH] test: Cleanup perform_face_detection Base on commit 0a7ab1f50e641c4937d2ca87ab0950067688860b alloactate mv_source, mv_engine, mv_infer in a function so makes hard to deallocate resource and error handling. Change-Id: Iaf1124821e39c7785955bc9b06a49b5997bb0430 Signed-off-by: Kwang Son --- .../inference/inference_test_suite.c | 741 +++++---------------- 1 file changed, 175 insertions(+), 566 deletions(-) diff --git a/test/testsuites/machine_learning/inference/inference_test_suite.c b/test/testsuites/machine_learning/inference/inference_test_suite.c index 5fa02f2..b47547e 100644 --- a/test/testsuites/machine_learning/inference/inference_test_suite.c +++ b/test/testsuites/machine_learning/inference/inference_test_suite.c @@ -2386,503 +2386,159 @@ int perform_object_detection() return MEDIA_VISION_ERROR_NONE; } -int perform_tflite_mobilenetv1ssd_face(mv_engine_config_h *engine_cfg) +int perform_tflite_mobilenetv1ssd_face(mv_engine_config_h handle) { - 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 configuration.\n"); - } - } - return err; - } - - const char *inputNodeName = "normalized_input_image_tensor"; - const char *outputNodeName[] = { "TFLite_Detection_PostProcess", - "TFLite_Detection_PostProcess:1", - "TFLite_Detection_PostProcess:2", - "TFLite_Detection_PostProcess:3" }; - - mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, FD_TFLITE_WEIGHT_PATH); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_DATA_TYPE, - MV_INFERENCE_DATA_FLOAT32); - - mv_engine_config_set_double_attribute(handle, MV_INFERENCE_MODEL_MEAN_VALUE, - 127.5); - - mv_engine_config_set_double_attribute(handle, MV_INFERENCE_MODEL_STD_VALUE, - 127.5); - - mv_engine_config_set_double_attribute( - handle, MV_INFERENCE_CONFIDENCE_THRESHOLD, 0.3); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_BACKEND_TYPE, - MV_INFERENCE_BACKEND_TFLITE); - - 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, - 300); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, - 300); - - 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, 4); - - *engine_cfg = handle; - return err; -} - -int perform_opencv_resnet10ssd_face(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 configuration.\n"); - } - } - return err; - } - - const char *inputNodeName = "data"; - const char *outputNodeName[] = { "detection_out" }; - - mv_engine_config_set_string_attribute(handle, - MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, - FD_OPENCV_WEIGHT_CAFFE_PATH); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_DATA_TYPE, - MV_INFERENCE_DATA_FLOAT32); - - mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_CONFIGURATION_FILE_PATH, - FD_OPENCV_CONFIG_CAFFE_PATH); - - mv_engine_config_set_double_attribute(handle, MV_INFERENCE_MODEL_MEAN_VALUE, - 135.7); - - mv_engine_config_set_double_attribute(handle, MV_INFERENCE_MODEL_STD_VALUE, - 1.0); - - mv_engine_config_set_double_attribute( - handle, MV_INFERENCE_CONFIDENCE_THRESHOLD, 0.3); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_BACKEND_TYPE, - MV_INFERENCE_BACKEND_OPENCV); - - 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, - 300); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, - 300); - - 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_armnn_mobilenetv1ssd_face(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 configuration.\n"); - } - } - return err; - } - const char *inputNodeName = "normalized_input_image_tensor"; const char *outputNodeName[] = { "TFLite_Detection_PostProcess", - "TFLite_Detection_PostProcess:1", - "TFLite_Detection_PostProcess:2", - "TFLite_Detection_PostProcess:3" }; - - mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, FD_TFLITE_WEIGHT_PATH); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_DATA_TYPE, - MV_INFERENCE_DATA_FLOAT32); - - mv_engine_config_set_double_attribute(handle, MV_INFERENCE_MODEL_MEAN_VALUE, - 127.5); - - mv_engine_config_set_double_attribute(handle, MV_INFERENCE_MODEL_STD_VALUE, - 127.5); - - mv_engine_config_set_double_attribute( - handle, MV_INFERENCE_CONFIDENCE_THRESHOLD, 0.3); - - 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, - 300); - - mv_engine_config_set_int_attribute(handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, - 300); - - 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, 4); - - *engine_cfg = handle; - return err; -} - -int perform_hosted_tflite_mobilenetv1ssd_300_config_face(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 configuration.\n"); - } - } - return err; - } - - mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, - FD_TFLITE_WEIGHT_MOBILENET_V1_SSD_300_PATH); - - mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_BACKEND_TYPE, - MV_INFERENCE_BACKEND_TFLITE); - - mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_TARGET_TYPE, - MV_INFERENCE_TARGET_CPU); - - *engine_cfg = handle; - return err; -} - -int perform_hosted_tflite_blazeface_128_config_face(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 configuration.\n"); - } - } - return err; - } - - mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, - FD_TFLITE_WEIGHT_BLAZEFACE_128_PATH); - - mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_BACKEND_TYPE, - MV_INFERENCE_BACKEND_TFLITE); - - mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_TARGET_TYPE, - MV_INFERENCE_TARGET_CPU); - - *engine_cfg = handle; - return err; -} - -int perform_face_detection() -{ - int err = MEDIA_VISION_ERROR_NONE; - - int sel_opt = 0; - const char *names[] = { "Configuration", - "TFLite(CPU) + MobileNetV1 + SSD", - "OPENCV(CPU) + Resnet10 + SSD", - "ARMNN(CPU) + MobileNetV1 + SSD", - "Hosted: TFLite(cpu + MobilenetV1+SSD)", - "Hosted: TFLite(cpu + BlazeFace)", - "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_linear("Select Action:", names, ARRAY_SIZE(names)); - 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); - engine_cfg = NULL; - } - - err = perform_configuration(&engine_cfg); - } break; - case 2: { - //perform TFlit Mobilenetv1ssd 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); - engine_cfg = NULL; - } - - err = perform_tflite_mobilenetv1ssd_face(&engine_cfg); - } break; - case 3: { - //perform Opencv resenet10ssd 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_opencv_resnet10ssd_face(&engine_cfg); - } break; - case 4: { - //perform Armnn Mobilenetv1ssd 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_mobilenetv1ssd_face(&engine_cfg); - } break; - case 5: { - //perform hosted TFlite Mobilenetv1ssd 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); - engine_cfg = NULL; - } - - err = perform_hosted_tflite_mobilenetv1ssd_300_config_face(&engine_cfg); - } break; - case 6: { - //perform hosted TFlite blazeface 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); - engine_cfg = NULL; - } - - err = perform_hosted_tflite_blazeface_128_config_face(&engine_cfg); - } break; - case 7: { - // 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); - infer = NULL; - } - - // 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; - } + "TFLite_Detection_PostProcess:1", + "TFLite_Detection_PostProcess:2", + "TFLite_Detection_PostProcess:3" }; - //prepare - err = mv_inference_prepare(infer); - if (err != MEDIA_VISION_ERROR_NONE) { - printf("Fail to prepare inference handle"); - break; - } - } break; - case 8: { - if (mvSource) { - int err2 = mv_destroy_source(mvSource); - if (err2 != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy mvSource\n"); - mvSource = NULL; - } - - 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. error code:%i\n", err2); - mvSource = NULL; - free(in_file_name); - break; - } - free(in_file_name); - - struct timespec s_tspec; - struct timespec e_tspec; - - clock_gettime(CLOCK_MONOTONIC, &s_tspec); - - // Object Detect - err = mv_inference_face_detect(mvSource, infer, _face_detected_cb, - NULL); - - clock_gettime(CLOCK_MONOTONIC, &e_tspec); + RET_IF_FAIL(engine_config_hosted_tflite_cpu(handle, FD_TFLITE_WEIGHT_PATH)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_DATA_TYPE, MV_INFERENCE_DATA_FLOAT32)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_MEAN_VALUE, 127.5)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_STD_VALUE, 127.5)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_CONFIDENCE_THRESHOLD, 0.3)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_WIDTH, 300)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, 300)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_CHANNELS, 3)); + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_INPUT_NODE_NAME, inputNodeName)); + RET_IF_FAIL(mv_engine_config_set_array_string_attribute( + handle, MV_INFERENCE_OUTPUT_NODE_NAMES, outputNodeName, 4)); + return MEDIA_VISION_ERROR_NONE; +} - struct timespec diffspec = diff(s_tspec, e_tspec); - unsigned long timeDiff = gettotalmillisec(diffspec); - printf("elapsed time : %lu(ms)\n", timeDiff); - } break; - case 9: { - //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); - engine_cfg = NULL; - } +int perform_opencv_resnet10ssd_face(mv_engine_config_h handle) +{ + const char *inputNodeName = "data"; + const char *outputNodeName[] = { "detection_out" }; - if (infer) { - err = mv_inference_destroy(infer); - if (err != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy inference handle [err:%i]\n", err); - infer = NULL; - } + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, + FD_OPENCV_WEIGHT_CAFFE_PATH)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_DATA_TYPE, MV_INFERENCE_DATA_FLOAT32)); + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_MODEL_CONFIGURATION_FILE_PATH, + FD_OPENCV_CONFIG_CAFFE_PATH)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_MEAN_VALUE, 135.7)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_STD_VALUE, 1.0)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_CONFIDENCE_THRESHOLD, 0.3)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_BACKEND_TYPE, MV_INFERENCE_BACKEND_OPENCV)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_TARGET_TYPE, MV_INFERENCE_TARGET_CPU)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_WIDTH, 300)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, 300)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_CHANNELS, 3)); + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_INPUT_NODE_NAME, inputNodeName)); + RET_IF_FAIL(mv_engine_config_set_array_string_attribute( + handle, MV_INFERENCE_OUTPUT_NODE_NAMES, outputNodeName, 1)); + return MEDIA_VISION_ERROR_NONE; +} - if (mvSource) { - err = mv_destroy_source(mvSource); - if (err != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy mvSource [err:%i]\n", err); - mvSource = NULL; - } - } break; - default: - printf("Invalid option.\n"); - sel_opt = 0; - continue; - } +int perform_armnn_mobilenetv1ssd_face(mv_engine_config_h handle) +{ + const char *inputNodeName = "normalized_input_image_tensor"; + const char *outputNodeName[] = { "TFLite_Detection_PostProcess", + "TFLite_Detection_PostProcess:1", + "TFLite_Detection_PostProcess:2", + "TFLite_Detection_PostProcess:3" }; - int do_another = 0; - if (err != MEDIA_VISION_ERROR_NONE) { - printf("ERROR: Action is finished with error code:%i\n", err); - } + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, + FD_TFLITE_WEIGHT_PATH)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_DATA_TYPE, MV_INFERENCE_DATA_FLOAT32)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_MEAN_VALUE, 127.5)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_STD_VALUE, 127.5)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_CONFIDENCE_THRESHOLD, 0.3)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_BACKEND_TYPE, MV_INFERENCE_BACKEND_ARMNN)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_TARGET_TYPE, MV_INFERENCE_TARGET_CPU)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_WIDTH, 300)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, 300)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_CHANNELS, 3)); + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_INPUT_NODE_NAME, inputNodeName)); + RET_IF_FAIL(mv_engine_config_set_array_string_attribute( + handle, MV_INFERENCE_OUTPUT_NODE_NAMES, outputNodeName, 4)); + return MEDIA_VISION_ERROR_NONE; +} - sel_opt = 0; - while (sel_opt == 0) { - sel_opt = show_menu_yes_or_no("Run Face Detection again?:"); - switch (sel_opt) { - case 1: - do_another = 1; - break; - case 2: - do_another = 0; - break; - default: - printf("Invalid option.\n"); - sel_opt = 0; - } - } +int perform_face_detection() +{ + int err = MEDIA_VISION_ERROR_NONE; + mv_engine_config_h engine_cfg = NULL; + const char *names[] = { + "TFLite(CPU) + MobileNetV1 + SSD", + "OPENCV(CPU) + Resnet10 + SSD", + "ARMNN(CPU) + MobileNetV1 + SSD", + "Hosted: TFLite(cpu + MobilenetV1+SSD)", + "Hosted: TFLite(cpu + BlazeFace)", + }; - sel_opt = (do_another == 1) ? 0 : 1; + int sel_opt = show_menu_linear("Select Action:", names, ARRAY_SIZE(names)); + if (sel_opt <= 0 || sel_opt > ARRAY_SIZE(names)) { + printf("Invalid option"); + return -1; } - 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); - engine_cfg = NULL; - } + RET_IF_FAIL(mv_create_engine_config(&engine_cfg)); - if (infer) { - err = mv_inference_destroy(infer); - if (err != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy inference handle [err:%i]\n", err); - infer = NULL; + switch (sel_opt) { + case 1: { + err = perform_tflite_mobilenetv1ssd_face(engine_cfg); + } break; + case 2: { + err = perform_opencv_resnet10ssd_face(engine_cfg); + } break; + case 3: { + err = perform_armnn_mobilenetv1ssd_face(engine_cfg); + } break; + case 4: { + err = engine_config_hosted_tflite_cpu( + engine_cfg, FD_TFLITE_WEIGHT_MOBILENET_V1_SSD_300_PATH); + } break; + case 5: { + err = engine_config_hosted_tflite_cpu( + engine_cfg, FD_TFLITE_WEIGHT_BLAZEFACE_128_PATH); + } break; } - - if (mvSource) { - err = mv_destroy_source(mvSource); - if (err != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy mvSource [err:%i]\n", err); - mvSource = NULL; + if (err != MEDIA_VISION_ERROR_NONE) { + printf("Fail to perform config [err:%i]\n", err); + goto clean_face_engine; } - return MEDIA_VISION_ERROR_NONE; + err = mv_inference_task_helper(engine_cfg, TASK_FD); + if (err != MEDIA_VISION_ERROR_NONE) + printf("Fail to detect with engine [err:%i]\n", err); + +clean_face_engine: + RET_IF_FAIL(mv_destroy_engine_config(engine_cfg)); + return err; } int perform_tflite_TweakCNN(mv_engine_config_h handle) @@ -2985,7 +2641,9 @@ int perform_facial_landmark_detection() goto clean_facial_landmark_engine; } - RET_IF_FAIL(mv_inference_task_helper(engine_cfg, TASK_FLD)); + err = mv_inference_task_helper(engine_cfg, TASK_FLD); + if (err != MEDIA_VISION_ERROR_NONE) + printf("Fail to detect with engine [err:%i]\n", err); clean_facial_landmark_engine: RET_IF_FAIL(mv_destroy_engine_config(engine_cfg)); @@ -2995,21 +2653,13 @@ clean_facial_landmark_engine: int engine_config_hosted_tflite_cpu(mv_engine_config_h handle, const char *tf_weight) { - int err = mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, tf_weight); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_int_attribute(handle, MV_INFERENCE_BACKEND_TYPE, - MV_INFERENCE_BACKEND_TFLITE); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_int_attribute(handle, MV_INFERENCE_TARGET_TYPE, - MV_INFERENCE_TARGET_CPU); - return err; + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_MODEL_WEIGHT_FILE_PATH, tf_weight)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_BACKEND_TYPE, MV_INFERENCE_BACKEND_TFLITE)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_TARGET_TYPE, MV_INFERENCE_TARGET_CPU)); + return MEDIA_VISION_ERROR_NONE; } int perform_armnn_cpm_config(mv_engine_config_h handle) @@ -3017,68 +2667,29 @@ int perform_armnn_cpm_config(mv_engine_config_h handle) const char *inputNodeName = "image"; const char *outputNodeName[] = { "Convolutional_Pose_Machine/stage_5_out" }; - int err = engine_config_hosted_tflite_cpu(handle, PLD_TFLITE_WEIGHT_PATH); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_INPUT_DATA_TYPE, MV_INFERENCE_DATA_FLOAT32); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_MODEL_USER_FILE_PATH, PLD_POSE_LABEL_PATH); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_double_attribute( - handle, MV_INFERENCE_MODEL_MEAN_VALUE, 0.0); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_double_attribute( - handle, MV_INFERENCE_MODEL_STD_VALUE, 1.0); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_double_attribute( - handle, MV_INFERENCE_CONFIDENCE_THRESHOLD, 0.3); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_INPUT_TENSOR_WIDTH, 192); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, 192); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_int_attribute( - handle, MV_INFERENCE_INPUT_TENSOR_CHANNELS, 3); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_string_attribute( - handle, MV_INFERENCE_INPUT_NODE_NAME, inputNodeName); - if (err != MEDIA_VISION_ERROR_NONE) { - return err; - } - - err = mv_engine_config_set_array_string_attribute( - handle, MV_INFERENCE_OUTPUT_NODE_NAMES, outputNodeName, 1); - return err; + RET_IF_FAIL( + engine_config_hosted_tflite_cpu(handle, PLD_TFLITE_WEIGHT_PATH)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_DATA_TYPE, MV_INFERENCE_DATA_FLOAT32)); + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_MODEL_USER_FILE_PATH, PLD_POSE_LABEL_PATH)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_MEAN_VALUE, 0.0)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_MODEL_STD_VALUE, 1.0)); + RET_IF_FAIL(mv_engine_config_set_double_attribute( + handle, MV_INFERENCE_CONFIDENCE_THRESHOLD, 0.3)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_WIDTH, 192)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_HEIGHT, 192)); + RET_IF_FAIL(mv_engine_config_set_int_attribute( + handle, MV_INFERENCE_INPUT_TENSOR_CHANNELS, 3)); + RET_IF_FAIL(mv_engine_config_set_string_attribute( + handle, MV_INFERENCE_INPUT_NODE_NAME, inputNodeName)); + RET_IF_FAIL(mv_engine_config_set_array_string_attribute( + handle, MV_INFERENCE_OUTPUT_NODE_NAMES, outputNodeName, 1)); + return MEDIA_VISION_ERROR_NONE; } int perform_pose_landmark_detection() @@ -3105,18 +2716,18 @@ int perform_pose_landmark_detection() } switch (sel_opt) { - case 1: { //perform TweakCNN config + case 1: { err = perform_armnn_cpm_config(engine_cfg); } break; - case 2: { //perform cpm config + case 2: { err = engine_config_hosted_tflite_cpu(engine_cfg, PLD_TFLITE_WEIGHT_CPM_192_PATH); } break; - case 3: { //perform mobilenet-v1 posenet config + case 3: { err = engine_config_hosted_tflite_cpu( engine_cfg, PLD_TFLITE_WEIGHT_MOBILENET_V1_POSENET_257_PATH); } break; - case 4: { //perform int8 movenet + case 4: { err = engine_config_hosted_tflite_cpu( engine_cfg, PLD_TFLITE_WEIGHT_INT8_MOVENET_PATH); } break; @@ -3131,9 +2742,7 @@ int perform_pose_landmark_detection() printf("Fail to detect with engine [err:%i]\n", err); clean_pose_engine: - err = mv_destroy_engine_config(engine_cfg); - if (err != MEDIA_VISION_ERROR_NONE) - printf("Fail to destroy engine_cfg [err:%i]\n", err); + RET_IF_FAIL(mv_destroy_engine_config(engine_cfg)); return err; } -- 2.7.4