Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 1 | /* |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 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 Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 17 | #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 Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 22 | #include "log_macros.h" /* Logging functions */ |
| 23 | #include "BufAttributes.hpp" /* Buffer attributes to be applied */ |
| 24 | |
| 25 | namespace arm { |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 26 | namespace 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 Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 33 | } /* namespace arm */ |
| 34 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 35 | static 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 Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 49 | void main_loop() |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 50 | { |
| 51 | arm::app::YoloFastestModel model; /* Model wrapper object. */ |
| 52 | |
| 53 | /* Load the model. */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 54 | if (!model.Init(arm::app::tensorArena, |
| 55 | sizeof(arm::app::tensorArena), |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 56 | arm::app::object_detection::GetModelPointer(), |
| 57 | arm::app::object_detection::GetModelLen())) { |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 58 | printf_err("Failed to initialise model\n"); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | /* Instantiate application context. */ |
| 63 | arm::app::ApplicationContext caseContext; |
| 64 | |
Kshitij Sisodia | 4cc4021 | 2022-04-08 09:54:53 +0100 | [diff] [blame] | 65 | arm::app::Profiler profiler{"object_detection"}; |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 66 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 67 | caseContext.Set<arm::app::Model&>("model", model); |
| 68 | caseContext.Set<uint32_t>("imgIndex", 0); |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 69 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 70 | /* 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 Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 79 | menuOption = arm::app::ReadUserInputAsInt(); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 80 | 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 Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 89 | auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt()); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 90 | 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 | } |