Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [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 "Classifier.hpp" /* Classifier. */ |
| 19 | #include "InputFiles.hpp" /* For input images. */ |
| 20 | #include "Labels.hpp" /* For label strings. */ |
| 21 | #include "VisualWakeWordModel.hpp" /* Model class for running inference. */ |
| 22 | #include "UseCaseHandler.hpp" /* Handlers for different user options. */ |
| 23 | #include "UseCaseCommonUtils.hpp" /* Utils functions. */ |
| 24 | |
| 25 | using ViusalWakeWordClassifier = arm::app::Classifier; |
| 26 | |
| 27 | void main_loop(hal_platform &platform) |
| 28 | { |
| 29 | arm::app::VisualWakeWordModel model; /* Model wrapper object. */ |
| 30 | |
| 31 | /* Load the model. */ |
| 32 | if (!model.Init()) { |
| 33 | printf_err("Failed to initialise model\n"); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | /* Instantiate application context. */ |
| 38 | arm::app::ApplicationContext caseContext; |
| 39 | |
| 40 | arm::app::Profiler profiler{&platform, "vww"}; |
| 41 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
| 42 | caseContext.Set<hal_platform&>("platform", platform); |
| 43 | caseContext.Set<arm::app::Model&>("model", model); |
| 44 | caseContext.Set<uint32_t>("imgIndex", 0); |
| 45 | |
| 46 | ViusalWakeWordClassifier classifier; /* Classifier wrapper object. */ |
| 47 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 48 | |
| 49 | std::vector <std::string> labels; |
| 50 | GetLabelsVector(labels); |
| 51 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 52 | |
| 53 | /* Loop. */ |
| 54 | bool executionSuccessful = true; |
| 55 | constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false; |
| 56 | do { |
| 57 | int menuOption = common::MENU_OPT_RUN_INF_NEXT; |
| 58 | if (bUseMenu) { |
| 59 | DisplayCommonMenu(); |
| 60 | menuOption = arm::app::ReadUserInputAsInt(platform); |
| 61 | printf("\n"); |
| 62 | } |
| 63 | |
| 64 | switch (menuOption) { |
| 65 | case common::MENU_OPT_RUN_INF_NEXT: |
| 66 | executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false); |
| 67 | break; |
| 68 | case common::MENU_OPT_RUN_INF_CHOSEN: { |
| 69 | printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1); |
| 70 | auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt(platform)); |
| 71 | executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false); |
| 72 | break; |
| 73 | } |
| 74 | case common::MENU_OPT_RUN_INF_ALL: |
| 75 | executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true); |
| 76 | break; |
| 77 | case common::MENU_OPT_SHOW_MODEL_INFO: { |
| 78 | executionSuccessful = model.ShowModelInfoHandler(); |
| 79 | break; |
| 80 | } |
| 81 | case common::MENU_OPT_LIST_IFM: |
| 82 | executionSuccessful = ListFilesHandler(caseContext); |
| 83 | break; |
| 84 | default: |
| 85 | printf("Incorrect choice, try again."); |
| 86 | break; |
| 87 | } |
| 88 | } while (executionSuccessful && bUseMenu); |
| 89 | info("Main loop terminated.\n"); |
| 90 | |
| 91 | } |