blob: 2161b0a56da0f55e2d946115804f2feff4b203a8 [file] [log] [blame]
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01001/*
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 Sisodiaaa4bcb12022-05-06 09:13:03 +010024#include "log_macros.h" /* Logging functions */
25#include "BufAttributes.hpp" /* Buffer attributes to be applied */
26
27namespace arm {
28 namespace app {
29 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
30 } /* namespace app */
31} /* namespace arm */
32
33extern uint8_t* GetModelPointer();
34extern size_t GetModelLen();
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010035
36using ViusalWakeWordClassifier = arm::app::Classifier;
37
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010038void main_loop()
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010039{
40 arm::app::VisualWakeWordModel model; /* Model wrapper object. */
41
42 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010043 if (!model.Init(arm::app::tensorArena,
44 sizeof(arm::app::tensorArena),
45 GetModelPointer(),
46 GetModelLen())) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010047 printf_err("Failed to initialise model\n");
48 return;
49 }
50
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010051#if !defined(ARM_NPU)
52 /* If it is not a NPU build check if the model contains a NPU operator */
53 if (model.ContainsEthosUOperator()) {
54 printf_err("No driver support for Ethos-U operator found in the model.\n");
55 return;
56 }
57#endif /* ARM_NPU */
58
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010059 /* Instantiate application context. */
60 arm::app::ApplicationContext caseContext;
61
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010062 arm::app::Profiler profiler{"vww"};
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010063 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010064 caseContext.Set<arm::app::Model&>("model", model);
65 caseContext.Set<uint32_t>("imgIndex", 0);
66
67 ViusalWakeWordClassifier classifier; /* Classifier wrapper object. */
68 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
69
70 std::vector <std::string> labels;
71 GetLabelsVector(labels);
72 caseContext.Set<const std::vector <std::string>&>("labels", labels);
73
74 /* Loop. */
75 bool executionSuccessful = true;
76 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
77 do {
78 int menuOption = common::MENU_OPT_RUN_INF_NEXT;
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010079 if (bUseMenu) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010080 DisplayCommonMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010081 menuOption = arm::app::ReadUserInputAsInt();
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010082 printf("\n");
83 }
84
85 switch (menuOption) {
86 case common::MENU_OPT_RUN_INF_NEXT:
87 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false);
88 break;
89 case common::MENU_OPT_RUN_INF_CHOSEN: {
90 printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +010091 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010092 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010093 executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false);
94 break;
95 }
96 case common::MENU_OPT_RUN_INF_ALL:
97 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
98 break;
99 case common::MENU_OPT_SHOW_MODEL_INFO: {
100 executionSuccessful = model.ShowModelInfoHandler();
101 break;
102 }
103 case common::MENU_OPT_LIST_IFM:
104 executionSuccessful = ListFilesHandler(caseContext);
105 break;
106 default:
107 printf("Incorrect choice, try again.");
108 break;
109 }
110 } while (executionSuccessful && bUseMenu);
111 info("Main loop terminated.\n");
112
113}