blob: fae753071d491aadf4927cd89c51d8a6a58afb96 [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
51 /* Instantiate application context. */
52 arm::app::ApplicationContext caseContext;
53
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010054 arm::app::Profiler profiler{"vww"};
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010055 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010056 caseContext.Set<arm::app::Model&>("model", model);
57 caseContext.Set<uint32_t>("imgIndex", 0);
58
59 ViusalWakeWordClassifier classifier; /* Classifier wrapper object. */
60 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
61
62 std::vector <std::string> labels;
63 GetLabelsVector(labels);
64 caseContext.Set<const std::vector <std::string>&>("labels", labels);
65
66 /* Loop. */
67 bool executionSuccessful = true;
68 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
69 do {
70 int menuOption = common::MENU_OPT_RUN_INF_NEXT;
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010071 if (bUseMenu) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010072 DisplayCommonMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010073 menuOption = arm::app::ReadUserInputAsInt();
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010074 printf("\n");
75 }
76
77 switch (menuOption) {
78 case common::MENU_OPT_RUN_INF_NEXT:
79 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false);
80 break;
81 case common::MENU_OPT_RUN_INF_CHOSEN: {
82 printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +010083 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010084 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010085 executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false);
86 break;
87 }
88 case common::MENU_OPT_RUN_INF_ALL:
89 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
90 break;
91 case common::MENU_OPT_SHOW_MODEL_INFO: {
92 executionSuccessful = model.ShowModelInfoHandler();
93 break;
94 }
95 case common::MENU_OPT_LIST_IFM:
96 executionSuccessful = ListFilesHandler(caseContext);
97 break;
98 default:
99 printf("Incorrect choice, try again.");
100 break;
101 }
102 } while (executionSuccessful && bUseMenu);
103 info("Main loop terminated.\n");
104
105}