É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. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 24 | #include "log_macros.h" /* Logging functions */ |
| 25 | #include "BufAttributes.hpp" /* Buffer attributes to be applied */ |
| 26 | |
| 27 | namespace arm { |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 28 | namespace app { |
| 29 | static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE; |
| 30 | namespace vww { |
| 31 | extern uint8_t* GetModelPointer(); |
| 32 | extern size_t GetModelLen(); |
| 33 | } /* namespace vww */ |
| 34 | } /* namespace app */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 35 | } /* namespace arm */ |
| 36 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 37 | using ViusalWakeWordClassifier = arm::app::Classifier; |
| 38 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 39 | void main_loop() |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 40 | { |
| 41 | arm::app::VisualWakeWordModel model; /* Model wrapper object. */ |
| 42 | |
| 43 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 44 | if (!model.Init(arm::app::tensorArena, |
| 45 | sizeof(arm::app::tensorArena), |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 46 | arm::app::vww::GetModelPointer(), |
| 47 | arm::app::vww::GetModelLen())) { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 48 | printf_err("Failed to initialise model\n"); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | /* Instantiate application context. */ |
| 53 | arm::app::ApplicationContext caseContext; |
| 54 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 55 | arm::app::Profiler profiler{"vww"}; |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 56 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 57 | caseContext.Set<arm::app::Model&>("model", model); |
| 58 | caseContext.Set<uint32_t>("imgIndex", 0); |
| 59 | |
| 60 | ViusalWakeWordClassifier classifier; /* Classifier wrapper object. */ |
| 61 | caseContext.Set<arm::app::Classifier&>("classifier", classifier); |
| 62 | |
| 63 | std::vector <std::string> labels; |
| 64 | GetLabelsVector(labels); |
| 65 | caseContext.Set<const std::vector <std::string>&>("labels", labels); |
| 66 | |
| 67 | /* Loop. */ |
| 68 | bool executionSuccessful = true; |
| 69 | constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false; |
| 70 | do { |
| 71 | int menuOption = common::MENU_OPT_RUN_INF_NEXT; |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 72 | if (bUseMenu) { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 73 | DisplayCommonMenu(); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 74 | menuOption = arm::app::ReadUserInputAsInt(); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 75 | printf("\n"); |
| 76 | } |
| 77 | |
| 78 | switch (menuOption) { |
| 79 | case common::MENU_OPT_RUN_INF_NEXT: |
| 80 | executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false); |
| 81 | break; |
| 82 | case common::MENU_OPT_RUN_INF_CHOSEN: { |
| 83 | printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1); |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 84 | fflush(stdout); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 85 | auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt()); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 86 | executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false); |
| 87 | break; |
| 88 | } |
| 89 | case common::MENU_OPT_RUN_INF_ALL: |
| 90 | executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true); |
| 91 | break; |
| 92 | case common::MENU_OPT_SHOW_MODEL_INFO: { |
| 93 | executionSuccessful = model.ShowModelInfoHandler(); |
| 94 | break; |
| 95 | } |
| 96 | case common::MENU_OPT_LIST_IFM: |
| 97 | executionSuccessful = ListFilesHandler(caseContext); |
| 98 | break; |
| 99 | default: |
| 100 | printf("Incorrect choice, try again."); |
| 101 | break; |
| 102 | } |
| 103 | } while (executionSuccessful && bUseMenu); |
| 104 | info("Main loop terminated.\n"); |
| 105 | |
| 106 | } |