alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. All rights reserved. |
| 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | #include "hal.h" /* Brings in platform definitions */ |
| 18 | #include "InputFiles.hpp" /* For input data */ |
| 19 | #include "AdModel.hpp" /* Model class for running inference */ |
| 20 | #include "UseCaseCommonUtils.hpp" /* Utils functions */ |
| 21 | #include "UseCaseHandler.hpp" /* Handlers for different user options */ |
| 22 | |
| 23 | enum opcodes |
| 24 | { |
| 25 | MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector */ |
| 26 | MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index */ |
| 27 | MENU_OPT_RUN_INF_ALL, /* Run inference on all */ |
| 28 | MENU_OPT_SHOW_MODEL_INFO, /* Show model info */ |
| 29 | MENU_OPT_LIST_AUDIO_CLIPS /* List the current baked audio signals */ |
| 30 | }; |
| 31 | |
| 32 | static void DisplayMenu() |
| 33 | { |
| 34 | printf("\n\nUser input required\n"); |
| 35 | printf("Enter option number from:\n\n"); |
| 36 | printf(" %u. Classify next audio signal\n", MENU_OPT_RUN_INF_NEXT); |
| 37 | printf(" %u. Classify audio signal at chosen index\n", MENU_OPT_RUN_INF_CHOSEN); |
| 38 | printf(" %u. Run classification on all audio signals\n", MENU_OPT_RUN_INF_ALL); |
| 39 | printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO); |
| 40 | printf(" %u. List audio signals\n\n", MENU_OPT_LIST_AUDIO_CLIPS); |
| 41 | printf(" Choice: "); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | void main_loop(hal_platform& platform) |
| 46 | { |
| 47 | arm::app::AdModel model; /* Model wrapper object. */ |
| 48 | |
| 49 | /* Load the model. */ |
| 50 | if (!model.Init()) |
| 51 | { |
| 52 | printf_err("failed to initialise model\n"); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | /* Instantiate application context. */ |
| 57 | arm::app::ApplicationContext caseContext; |
| 58 | |
| 59 | caseContext.Set<hal_platform&>("platform", platform); |
| 60 | caseContext.Set<arm::app::Model&>("model", model); |
| 61 | caseContext.Set<uint32_t>("clipIndex", 0); |
| 62 | caseContext.Set<int>("frameLength", g_FrameLength); |
| 63 | caseContext.Set<int>("frameStride", g_FrameStride); |
| 64 | caseContext.Set<float>("scoreThreshold", g_ScoreThreshold); |
| 65 | caseContext.Set<float>("trainingMean", g_TrainingMean); |
| 66 | |
| 67 | /* Main program loop. */ |
| 68 | bool executionSuccessful = true; |
| 69 | constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false; |
| 70 | |
| 71 | /* Loop. */ |
| 72 | do { |
| 73 | int menuOption = MENU_OPT_RUN_INF_NEXT; |
| 74 | if (bUseMenu) { |
| 75 | DisplayMenu(); |
| 76 | menuOption = arm::app::ReadUserInputAsInt(platform); |
| 77 | printf("\n"); |
| 78 | } |
| 79 | switch (menuOption) { |
| 80 | case MENU_OPT_RUN_INF_NEXT: |
| 81 | executionSuccessful = ClassifyVibrationHandler( |
| 82 | caseContext, |
| 83 | caseContext.Get<uint32_t>("clipIndex"), |
| 84 | false); |
| 85 | break; |
| 86 | case MENU_OPT_RUN_INF_CHOSEN: { |
| 87 | printf(" Enter the data index [0, %d]: ", |
| 88 | NUMBER_OF_FILES-1); |
| 89 | auto audioIndex = static_cast<uint32_t>( |
| 90 | arm::app::ReadUserInputAsInt(platform)); |
| 91 | executionSuccessful = ClassifyVibrationHandler(caseContext, |
| 92 | audioIndex, |
| 93 | false); |
| 94 | break; |
| 95 | } |
| 96 | case MENU_OPT_RUN_INF_ALL: |
| 97 | executionSuccessful = ClassifyVibrationHandler( |
| 98 | caseContext, |
| 99 | caseContext.Get<uint32_t>("clipIndex"), |
| 100 | true); |
| 101 | break; |
| 102 | case MENU_OPT_SHOW_MODEL_INFO: |
| 103 | executionSuccessful = model.ShowModelInfoHandler(); |
| 104 | break; |
| 105 | case MENU_OPT_LIST_AUDIO_CLIPS: |
| 106 | executionSuccessful = ListFilesHandler(caseContext); |
| 107 | break; |
| 108 | default: |
| 109 | printf("Incorrect choice, try again."); |
| 110 | break; |
| 111 | } |
| 112 | } while (executionSuccessful && bUseMenu); |
| 113 | info("Main loop terminated.\n"); |
| 114 | } |