alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Liam Barry | e958850 | 2022-01-25 14:31:15 +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 | #ifndef USECASE_COMMON_UTILS_HPP |
| 18 | #define USECASE_COMMON_UTILS_HPP |
| 19 | |
| 20 | #include "hal.h" |
| 21 | #include "Model.hpp" |
| 22 | #include "AppContext.hpp" |
| 23 | #include "Profiler.hpp" |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 24 | #include "UseCaseHandler.hpp" /* Handlers for different user options. */ |
| 25 | #include "Classifier.hpp" /* Classifier. */ |
| 26 | #include "InputFiles.hpp" |
| 27 | #include <inttypes.h> |
| 28 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 29 | |
| 30 | /* Helper macro to convert RGB888 to RGB565 format. */ |
| 31 | #define RGB888_TO_RGB565(R8,G8,B8) ((((R8>>3) & 0x1F) << 11) | \ |
| 32 | (((G8>>2) & 0x3F) << 5) | \ |
| 33 | ((B8>>3) & 0x1F)) |
| 34 | |
| 35 | constexpr uint16_t COLOR_BLACK = 0; |
| 36 | constexpr uint16_t COLOR_GREEN = RGB888_TO_RGB565( 0, 255, 0); // 2016; |
| 37 | constexpr uint16_t COLOR_YELLOW = RGB888_TO_RGB565(255, 255, 0); // 65504; |
| 38 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 39 | |
| 40 | void DisplayCommonMenu(); |
| 41 | |
| 42 | namespace image{ |
| 43 | |
| 44 | /** |
| 45 | * @brief Helper function to convert a UINT8 image to INT8 format. |
| 46 | * @param[in,out] data Pointer to the data start. |
| 47 | * @param[in] kMaxImageSize Total number of pixels in the image. |
| 48 | **/ |
| 49 | void ConvertImgToInt8(void * data, size_t kMaxImageSize); |
| 50 | |
| 51 | /** |
| 52 | * @brief Presents inference results using the data presentation |
| 53 | * object. |
| 54 | * @param[in] platform Reference to the hal platform object. |
| 55 | * @param[in] results Vector of classification results to be displayed. |
| 56 | * @return true if successful, false otherwise. |
| 57 | **/ |
| 58 | bool PresentInferenceResult(hal_platform & platform, |
| 59 | const std::vector < arm::app::ClassificationResult > & results); |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 60 | |
| 61 | /** |
| 62 | * @brief Converts RGB image to grayscale. |
| 63 | * @param[in] srcPtr Pointer to RGB source image. |
| 64 | * @param[out] dstPtr Pointer to grayscale destination image. |
| 65 | * @param[in] imgSz Destination image size. |
| 66 | **/ |
| 67 | void RgbToGrayscale(const uint8_t *srcPtr, uint8_t *dstPtr, const size_t dstImgSz); |
| 68 | } |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * @brief Helper function to increment current input feature vector index. |
| 72 | * @param[in,out] ctx Pointer to the application context object. |
| 73 | * @param[in] useCase Use case name |
| 74 | **/ |
| 75 | void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, std::string useCase); |
| 76 | |
| 77 | /** |
| 78 | * @brief Helper function to set the input feature map index. |
| 79 | * @param[in,out] ctx Pointer to the application context object. |
| 80 | * @param[in] idx Value to be set. |
| 81 | * @param[in] ctxIfmName Input Feature Map name |
| 82 | * @return true if index is set, false otherwise. |
| 83 | **/ |
| 84 | bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, std::string ctxIfmName); |
| 85 | |
| 86 | |
| 87 | namespace common { |
| 88 | |
| 89 | enum OPCODES { |
| 90 | MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */ |
| 91 | MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */ |
| 92 | MENU_OPT_RUN_INF_ALL, /* Run inference on all. */ |
| 93 | MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */ |
| 94 | MENU_OPT_LIST_IFM /* List the current IFM. */ |
| 95 | }; |
| 96 | |
| 97 | } |
| 98 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 99 | namespace arm { |
| 100 | namespace app { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 101 | /** |
| 102 | * @brief Run inference using given model |
| 103 | * object. If profiling is enabled, it will log the |
| 104 | * statistics too. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 105 | * @param[in] model Reference to the initialised model. |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 106 | * @param[in] profiler Reference to the initialised profiler. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 107 | * @return true if inference succeeds, false otherwise. |
| 108 | **/ |
Isabella Gottardi | 56ee620 | 2021-05-12 08:27:15 +0100 | [diff] [blame] | 109 | bool RunInference(arm::app::Model& model, Profiler& profiler); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * @brief Read input and return as an integer. |
| 113 | * @param[in] platform Reference to the hal platform object. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 114 | * @return Integer value corresponding to the user input. |
| 115 | **/ |
| 116 | int ReadUserInputAsInt(hal_platform& platform); |
| 117 | |
| 118 | #if VERIFY_TEST_OUTPUT |
| 119 | /** |
| 120 | * @brief Helper function to dump a tensor to stdout |
| 121 | * @param[in] tensor tensor to be dumped |
| 122 | * @param[in] lineBreakForNumElements number of elements |
| 123 | * after which line break will be added. |
| 124 | **/ |
alexander | 80eecfb | 2021-07-06 19:47:59 +0100 | [diff] [blame] | 125 | void DumpTensor(const TfLiteTensor* tensor, |
| 126 | size_t lineBreakForNumElements = 16); |
| 127 | |
| 128 | |
| 129 | void DumpTensorData(const uint8_t* tensorData, |
| 130 | size_t size, |
| 131 | size_t lineBreakForNumElements = 16); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 132 | #endif /* VERIFY_TEST_OUTPUT */ |
| 133 | |
| 134 | /** |
| 135 | * @brief List the files baked in the application. |
| 136 | * @param[in] ctx Reference to the application context. |
| 137 | * @return true or false based on event being handled. |
| 138 | **/ |
| 139 | bool ListFilesHandler(ApplicationContext& ctx); |
| 140 | |
| 141 | } /* namespace app */ |
| 142 | } /* namespace arm */ |
| 143 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 144 | |
| 145 | #endif /* USECASE_COMMON_UTILS_HPP */ |