blob: 7b67a194aa09ea0b7bcbc02faecdb430cb7f0bd7 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Isabella Gottardi3107aa22022-01-27 16:39:37 +00002 * Copyright (c) 2021 - 2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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 "MobileNetModel.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"
alexander3c798932021-03-26 21:42:19 +000025
26using ImgClassClassifier = arm::app::Classifier;
27
alexander3c798932021-03-26 21:42:19 +000028void main_loop(hal_platform& platform)
29{
30 arm::app::MobileNetModel 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
Isabella Gottardi8df12f32021-04-07 17:15:31 +010041 arm::app::Profiler profiler{&platform, "img_class"};
42 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
alexander3c798932021-03-26 21:42:19 +000043 caseContext.Set<hal_platform&>("platform", platform);
44 caseContext.Set<arm::app::Model&>("model", model);
45 caseContext.Set<uint32_t>("imgIndex", 0);
46
47 ImgClassClassifier classifier; /* Classifier wrapper object. */
48 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
49
Isabella Gottardi3107aa22022-01-27 16:39:37 +000050 std::vector<std::string> labels;
alexander3c798932021-03-26 21:42:19 +000051 GetLabelsVector(labels);
52 caseContext.Set<const std::vector <std::string>&>("labels", labels);
53
54 /* Loop. */
55 bool executionSuccessful = true;
56 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
57
58 /* Loop. */
59 do {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010060 int menuOption = common::MENU_OPT_RUN_INF_NEXT;
alexander3c798932021-03-26 21:42:19 +000061 if (bUseMenu) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010062 DisplayCommonMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010063 menuOption = arm::app::ReadUserInputAsInt();
alexander3c798932021-03-26 21:42:19 +000064 printf("\n");
65 }
66 switch (menuOption) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010067 case common::MENU_OPT_RUN_INF_NEXT:
alexander3c798932021-03-26 21:42:19 +000068 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false);
69 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010070 case common::MENU_OPT_RUN_INF_CHOSEN: {
alexander3c798932021-03-26 21:42:19 +000071 printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1);
Isabella Gottardi79d41542021-10-20 15:52:32 +010072 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010073 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
alexander3c798932021-03-26 21:42:19 +000074 executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false);
75 break;
76 }
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010077 case common::MENU_OPT_RUN_INF_ALL:
alexander3c798932021-03-26 21:42:19 +000078 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
79 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010080 case common::MENU_OPT_SHOW_MODEL_INFO:
alexander3c798932021-03-26 21:42:19 +000081 executionSuccessful = model.ShowModelInfoHandler();
82 break;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010083 case common::MENU_OPT_LIST_IFM:
alexander3c798932021-03-26 21:42:19 +000084 executionSuccessful = ListFilesHandler(caseContext);
85 break;
86 default:
87 printf("Incorrect choice, try again.");
88 break;
89 }
90 } while (executionSuccessful && bUseMenu);
91 info("Main loop terminated.\n");
Isabella Gottardi3107aa22022-01-27 16:39:37 +000092}