blob: 469907cb668c8f0285c1e203e219387c29b1cde7 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
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 "MobileNetModel.hpp" /* Model class for running inference. */
22#include "UseCaseHandler.hpp" /* Handlers for different user options. */
23#include "UseCaseCommonUtils.hpp" /* Utils functions. */
24
25using ImgClassClassifier = arm::app::Classifier;
26
27enum opcodes
28{
29 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
30 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */
31 MENU_OPT_RUN_INF_ALL, /* Run inference on all. */
32 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
33 MENU_OPT_LIST_IMAGES /* List the current baked images. */
34};
35
36static void DisplayMenu()
37{
38 printf("\n\nUser input required\n");
39 printf("Enter option number from:\n\n");
40 printf(" %u. Classify next image\n", MENU_OPT_RUN_INF_NEXT);
41 printf(" %u. Classify image at chosen index\n", MENU_OPT_RUN_INF_CHOSEN);
42 printf(" %u. Run classification on all images\n", MENU_OPT_RUN_INF_ALL);
43 printf(" %u. Show NN model info\n", MENU_OPT_SHOW_MODEL_INFO);
44 printf(" %u. List images\n\n", MENU_OPT_LIST_IMAGES);
45 printf(" Choice: ");
46}
47
48void main_loop(hal_platform& platform)
49{
50 arm::app::MobileNetModel model; /* Model wrapper object. */
51
52 /* Load the model. */
53 if (!model.Init()) {
54 printf_err("Failed to initialise model\n");
55 return;
56 }
57
58 /* Instantiate application context. */
59 arm::app::ApplicationContext caseContext;
60
61 caseContext.Set<hal_platform&>("platform", platform);
62 caseContext.Set<arm::app::Model&>("model", model);
63 caseContext.Set<uint32_t>("imgIndex", 0);
64
65 ImgClassClassifier classifier; /* Classifier wrapper object. */
66 caseContext.Set<arm::app::Classifier&>("classifier", classifier);
67
68 std::vector <std::string> labels;
69 GetLabelsVector(labels);
70 caseContext.Set<const std::vector <std::string>&>("labels", labels);
71
72 /* Loop. */
73 bool executionSuccessful = true;
74 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
75
76 /* Loop. */
77 do {
78 int menuOption = MENU_OPT_RUN_INF_NEXT;
79 if (bUseMenu) {
80 DisplayMenu();
81 menuOption = arm::app::ReadUserInputAsInt(platform);
82 printf("\n");
83 }
84 switch (menuOption) {
85 case MENU_OPT_RUN_INF_NEXT:
86 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false);
87 break;
88 case MENU_OPT_RUN_INF_CHOSEN: {
89 printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1);
90 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt(platform));
91 executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false);
92 break;
93 }
94 case MENU_OPT_RUN_INF_ALL:
95 executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
96 break;
97 case MENU_OPT_SHOW_MODEL_INFO:
98 executionSuccessful = model.ShowModelInfoHandler();
99 break;
100 case MENU_OPT_LIST_IMAGES:
101 executionSuccessful = ListFilesHandler(caseContext);
102 break;
103 default:
104 printf("Incorrect choice, try again.");
105 break;
106 }
107 } while (executionSuccessful && bUseMenu);
108 info("Main loop terminated.\n");
109}