blob: d03dd9b7d27f519d7668fb353d9936c3f8e94254 [file] [log] [blame]
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021 Arm Limited and/or its affiliates <open-source-office@arm.com>
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01003 * 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 {
Liam Barry213a5432022-05-09 17:06:19 +010028namespace 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 Sisodiaaa4bcb12022-05-06 09:13:03 +010035} /* namespace arm */
36
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010037using ViusalWakeWordClassifier = arm::app::Classifier;
38
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010039void main_loop()
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010040{
41 arm::app::VisualWakeWordModel model; /* Model wrapper object. */
42
43 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010044 if (!model.Init(arm::app::tensorArena,
45 sizeof(arm::app::tensorArena),
Liam Barry213a5432022-05-09 17:06:19 +010046 arm::app::vww::GetModelPointer(),
47 arm::app::vww::GetModelLen())) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010048 printf_err("Failed to initialise model\n");
49 return;
50 }
51
52 /* Instantiate application context. */
53 arm::app::ApplicationContext caseContext;
54
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010055 arm::app::Profiler profiler{"vww"};
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010056 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010057 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 Sisodiaaa4bcb12022-05-06 09:13:03 +010072 if (bUseMenu) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010073 DisplayCommonMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010074 menuOption = arm::app::ReadUserInputAsInt();
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010075 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 Gottardi79d41542021-10-20 15:52:32 +010084 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010085 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010086 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}