blob: 4735bcb405781a2957595446ef5969afb14ec42e [file] [log] [blame]
Michael Levit06fcf752022-01-12 11:53:46 +02001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
Michael Levit06fcf752022-01-12 11:53:46 +02003 * 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 {
Liam Barry213a5432022-05-09 17:06:19 +010026namespace app {
27 static uint8_t tensorArena[ACTIVATION_BUF_SZ] ACTIVATION_BUF_ATTRIBUTE;
28 namespace object_detection {
29 extern uint8_t* GetModelPointer();
30 extern size_t GetModelLen();
31 } /* namespace object_detection */
32} /* namespace app */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010033} /* namespace arm */
34
Isabella Gottardi3107aa22022-01-27 16:39:37 +000035static void DisplayDetectionMenu()
36{
37 printf("\n\n");
38 printf("User input required\n");
39 printf("Enter option number from:\n\n");
40 printf(" %u. Run detection on next ifm\n", common::MENU_OPT_RUN_INF_NEXT);
41 printf(" %u. Run detection ifm at chosen index\n", common::MENU_OPT_RUN_INF_CHOSEN);
42 printf(" %u. Run detection on all ifm\n", common::MENU_OPT_RUN_INF_ALL);
43 printf(" %u. Show NN model info\n", common::MENU_OPT_SHOW_MODEL_INFO);
44 printf(" %u. List ifm\n\n", common::MENU_OPT_LIST_IFM);
45 printf(" Choice: ");
46 fflush(stdout);
47}
48
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010049void main_loop()
Michael Levit06fcf752022-01-12 11:53:46 +020050{
51 arm::app::YoloFastestModel model; /* Model wrapper object. */
52
53 /* Load the model. */
Kshitij Sisodiaaa4bcb12022-05-06 09:13:03 +010054 if (!model.Init(arm::app::tensorArena,
55 sizeof(arm::app::tensorArena),
Liam Barry213a5432022-05-09 17:06:19 +010056 arm::app::object_detection::GetModelPointer(),
57 arm::app::object_detection::GetModelLen())) {
Michael Levit06fcf752022-01-12 11:53:46 +020058 printf_err("Failed to initialise model\n");
59 return;
60 }
61
62 /* Instantiate application context. */
63 arm::app::ApplicationContext caseContext;
64
Kshitij Sisodia4cc40212022-04-08 09:54:53 +010065 arm::app::Profiler profiler{"object_detection"};
Michael Levit06fcf752022-01-12 11:53:46 +020066 caseContext.Set<arm::app::Profiler&>("profiler", profiler);
Michael Levit06fcf752022-01-12 11:53:46 +020067 caseContext.Set<arm::app::Model&>("model", model);
68 caseContext.Set<uint32_t>("imgIndex", 0);
Isabella Gottardi3107aa22022-01-27 16:39:37 +000069
Michael Levit06fcf752022-01-12 11:53:46 +020070 /* Loop. */
71 bool executionSuccessful = true;
72 constexpr bool bUseMenu = NUMBER_OF_FILES > 1 ? true : false;
73
74 /* Loop. */
75 do {
76 int menuOption = common::MENU_OPT_RUN_INF_NEXT;
77 if (bUseMenu) {
78 DisplayDetectionMenu();
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010079 menuOption = arm::app::ReadUserInputAsInt();
Michael Levit06fcf752022-01-12 11:53:46 +020080 printf("\n");
81 }
82 switch (menuOption) {
83 case common::MENU_OPT_RUN_INF_NEXT:
84 executionSuccessful = ObjectDetectionHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false);
85 break;
86 case common::MENU_OPT_RUN_INF_CHOSEN: {
87 printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1);
88 fflush(stdout);
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010089 auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt());
Michael Levit06fcf752022-01-12 11:53:46 +020090 executionSuccessful = ObjectDetectionHandler(caseContext, imgIndex, false);
91 break;
92 }
93 case common::MENU_OPT_RUN_INF_ALL:
94 executionSuccessful = ObjectDetectionHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true);
95 break;
96 case common::MENU_OPT_SHOW_MODEL_INFO:
97 executionSuccessful = model.ShowModelInfoHandler();
98 break;
99 case common::MENU_OPT_LIST_IFM:
100 executionSuccessful = ListFilesHandler(caseContext);
101 break;
102 default:
103 printf("Incorrect choice, try again.");
104 break;
105 }
106 } while (executionSuccessful && bUseMenu);
107 info("Main loop terminated.\n");
108}