blob: 041ea18bf45b10c98383a8c729c40e485d94f52c [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. */
alexander31ae9f02022-02-10 16:15:54 +000024#include "log_macros.h"
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010025
26using ViusalWakeWordClassifier = arm::app::Classifier;
27
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010028void main_loop()
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010029{
30 arm::app::VisualWakeWordModel model; /* Model wrapper object. */
31
32 /* Load the model. */
33 if (!model.Init()) {
34 printf_err("Failed to initialise model\n");
35 return;
36 }
37
38 /* Instantiate application context. */
39 arm::app::ApplicationContext caseContext;
40
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010041 arm::app::Profiler profiler{"vww"};
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010042 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010043 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();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010060 menuOption = arm::app::ReadUserInputAsInt();
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010061 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);
Isabella Gottardi79d41542021-10-20 15:52:32 +010070 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010071 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010072 executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false);
73 break;
74 }
75 case common::MENU_OPT_RUN_INF_ALL:
76 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
77 break;
78 case common::MENU_OPT_SHOW_MODEL_INFO: {
79 executionSuccessful = model.ShowModelInfoHandler();
80 break;
81 }
82 case common::MENU_OPT_LIST_IFM:
83 executionSuccessful = ListFilesHandler(caseContext);
84 break;
85 default:
86 printf("Incorrect choice, try again.");
87 break;
88 }
89 } while (executionSuccessful && bUseMenu);
90 info("Main loop terminated.\n");
91
92}