alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021 - 2022 Arm Limited. All rights reserved. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [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 | */ |
| 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. */ |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 24 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 25 | |
| 26 | using ImgClassClassifier = arm::app::Classifier; |
| 27 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 28 | void 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 Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 41 | arm::app::Profiler profiler{&platform, "img_class"}; |
| 42 | caseContext.Set<arm::app::Profiler&>("profiler", profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 43 | 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 Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 50 | std::vector<std::string> labels; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 51 | 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áin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 60 | int menuOption = common::MENU_OPT_RUN_INF_NEXT; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 61 | if (bUseMenu) { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 62 | DisplayCommonMenu(); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame^] | 63 | menuOption = arm::app::ReadUserInputAsInt(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 64 | printf("\n"); |
| 65 | } |
| 66 | switch (menuOption) { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 67 | case common::MENU_OPT_RUN_INF_NEXT: |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 68 | executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), false); |
| 69 | break; |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 70 | case common::MENU_OPT_RUN_INF_CHOSEN: { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 71 | printf(" Enter the image index [0, %d]: ", NUMBER_OF_FILES-1); |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 72 | fflush(stdout); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame^] | 73 | auto imgIndex = static_cast<uint32_t>(arm::app::ReadUserInputAsInt()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 74 | executionSuccessful = ClassifyImageHandler(caseContext, imgIndex, false); |
| 75 | break; |
| 76 | } |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 77 | case common::MENU_OPT_RUN_INF_ALL: |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 78 | executionSuccessful = ClassifyImageHandler(caseContext, caseContext.Get<uint32_t>("imgIndex"), true); |
| 79 | break; |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 80 | case common::MENU_OPT_SHOW_MODEL_INFO: |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 81 | executionSuccessful = model.ShowModelInfoHandler(); |
| 82 | break; |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 83 | case common::MENU_OPT_LIST_IFM: |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 84 | 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 Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 92 | } |