blob: d119501a980156c3db38d9fa12d4cc6499096ec8 [file] [log] [blame]
Michael Levit06fcf752022-01-12 11:53:46 +02001/*
2 * Copyright (c) 2022 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 */
Isabella Gottardi3107aa22022-01-27 16:39:37 +000017#include "hal.h" /* Brings in platform definitions. */
18#include "InputFiles.hpp" /* For input images. */
19#include "YoloFastestModel.hpp" /* Model class for running inference. */
20#include "UseCaseHandler.hpp" /* Handlers for different user options. */
21#include "UseCaseCommonUtils.hpp" /* Utils functions. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010022#include "log_macros.h" /* Logging functions */
23#include "BufAttributes.hpp" /* Buffer attributes to be applied */
24
25namespace arm {
26 namespace app {
27 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
28 } /* namespace app */
29} /* namespace arm */
30
31extern uint8_t* GetModelPointer();
32extern size_t GetModelLen();
Michael Levit06fcf752022-01-12 11:53:46 +020033
Isabella Gottardi3107aa22022-01-27 16:39:37 +000034static void DisplayDetectionMenu()
35{
36 printf("\n\n");
37 printf("User input required\n");
38 printf("Enter option number from:\n\n");
39 printf(" %u. Run detection on next ifm\n", common::MENU_OPT_RUN_INF_NEXT);
40 printf(" %u. Run detection ifm at chosen index\n", common::MENU_OPT_RUN_INF_CHOSEN);
41 printf(" %u. Run detection on all ifm\n", common::MENU_OPT_RUN_INF_ALL);
42 printf(" %u. Show NN model info\n", common::MENU_OPT_SHOW_MODEL_INFO);
43 printf(" %u. List ifm\n\n", common::MENU_OPT_LIST_IFM);
44 printf(" Choice: ");
45 fflush(stdout);
46}
47
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010048void main_loop()
Michael Levit06fcf752022-01-12 11:53:46 +020049{
50 arm::app::YoloFastestModel model; /* Model wrapper object. */
51
52 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010053 if (!model.Init(arm::app::tensorArena,
54 sizeof(arm::app::tensorArena),
55 GetModelPointer(),
56 GetModelLen())) {
Michael Levit06fcf752022-01-12 11:53:46 +020057 printf_err("Failed to initialise model\n");
58 return;
59 }
60
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010061#if !defined(ARM_NPU)
62 /* If it is not a NPU build check if the model contains a NPU operator */
63 if (model.ContainsEthosUOperator()) {
64 printf_err("No driver support for Ethos-U operator found in the model.\n");
65 return;
66 }
67#endif /* ARM_NPU */
68
Michael Levit06fcf752022-01-12 11:53:46 +020069 /* Instantiate application context. */
70 arm::app::ApplicationContext caseContext;
71
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010072 arm::app::Profiler profiler{"object_detection"};
Michael Levit06fcf752022-01-12 11:53:46 +020073 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
Michael Levit06fcf752022-01-12 11:53:46 +020074 caseContext.Set<arm::app::Model&>("model", model);
75 caseContext.Set<uint32_t>("imgIndex", 0);
Isabella Gottardi3107aa22022-01-27 16:39:37 +000076
Michael Levit06fcf752022-01-12 11:53:46 +020077 /* Loop. */
78 bool executionSuccessful = true;
79 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
80
81 /* Loop. */
82 do {
83 int menuOption = common::MENU_OPT_RUN_INF_NEXT;
84 if (bUseMenu) {
85 DisplayDetectionMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010086 menuOption = arm::app::ReadUserInputAsInt();
Michael Levit06fcf752022-01-12 11:53:46 +020087 printf("\n");
88 }
89 switch (menuOption) {
90 case common::MENU_OPT_RUN_INF_NEXT:
91 executionSuccessful = ObjectDetectionHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false);
92 break;
93 case common::MENU_OPT_RUN_INF_CHOSEN: {
94 printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1);
95 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010096 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
Michael Levit06fcf752022-01-12 11:53:46 +020097 executionSuccessful = ObjectDetectionHandler(caseContext, imgIndex, false);
98 break;
99 }
100 case common::MENU_OPT_RUN_INF_ALL:
101 executionSuccessful = ObjectDetectionHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
102 break;
103 case common::MENU_OPT_SHOW_MODEL_INFO:
104 executionSuccessful = model.ShowModelInfoHandler();
105 break;
106 case common::MENU_OPT_LIST_IFM:
107 executionSuccessful = ListFilesHandler(caseContext);
108 break;
109 default:
110 printf("Incorrect choice, try again.");
111 break;
112 }
113 } while (executionSuccessful && bUseMenu);
114 info("Main loop terminated.\n");
115}