Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates |
| 3 | * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0 |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 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 "UseCaseHandler.hpp" |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 18 | #include "DetectorPostProcessing.hpp" |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 19 | #include "DetectorPreProcessing.hpp" |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 20 | #include "InputFiles.hpp" |
| 21 | #include "UseCaseCommonUtils.hpp" |
| 22 | #include "YoloFastestModel.hpp" |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 23 | #include "hal.h" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 24 | #include "log_macros.h" |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 25 | |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 26 | #include <cinttypes> |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 27 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 28 | namespace arm { |
| 29 | namespace app { |
| 30 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 31 | /** |
| 32 | * @brief Presents inference results along using the data presentation |
| 33 | * object. |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 34 | * @param[in] results Vector of detection results to be displayed. |
| 35 | * @return true if successful, false otherwise. |
| 36 | **/ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 37 | static bool |
| 38 | PresentInferenceResult(const std::vector<object_detection::DetectionResult>& results); |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * @brief Draw boxes directly on the LCD for all detected objects. |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 42 | * @param[in] results Vector of detection results to be displayed. |
| 43 | * @param[in] imageStartX X coordinate where the image starts on the LCD. |
| 44 | * @param[in] imageStartY Y coordinate where the image starts on the LCD. |
| 45 | * @param[in] imgDownscaleFactor How much image has been downscaled on LCD. |
| 46 | **/ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 47 | static void DrawDetectionBoxes(const std::vector<object_detection::DetectionResult>& results, |
| 48 | uint32_t imgStartX, |
| 49 | uint32_t imgStartY, |
| 50 | uint32_t imgDownscaleFactor); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 51 | |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 52 | /* Object detection inference handler. */ |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 53 | bool ObjectDetectionHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll) |
| 54 | { |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 55 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
| 56 | |
| 57 | constexpr uint32_t dataPsnImgDownscaleFactor = 1; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 58 | constexpr uint32_t dataPsnImgStartX = 10; |
| 59 | constexpr uint32_t dataPsnImgStartY = 35; |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 60 | |
Richard Burton | 71f282e | 2022-12-01 12:31:23 +0000 | [diff] [blame] | 61 | constexpr uint32_t dataPsnTxtInfStartX = 20; |
| 62 | constexpr uint32_t dataPsnTxtInfStartY = 28; |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 63 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 64 | hal_lcd_clear(COLOR_BLACK); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 65 | |
| 66 | auto& model = ctx.Get<Model&>("model"); |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 67 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 68 | /* If the request has a valid size, set the image index. */ |
| 69 | if (imgIndex < NUMBER_OF_FILES) { |
| 70 | if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) { |
| 71 | return false; |
| 72 | } |
| 73 | } |
| 74 | if (!model.IsInited()) { |
| 75 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 76 | return false; |
| 77 | } |
| 78 | |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 79 | auto initialImgIdx = ctx.Get<uint32_t>("imgIndex"); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 80 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 81 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 82 | TfLiteTensor* outputTensor0 = model.GetOutputTensor(0); |
| 83 | TfLiteTensor* outputTensor1 = model.GetOutputTensor(1); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 84 | |
| 85 | if (!inputTensor->dims) { |
| 86 | printf_err("Invalid input tensor dims\n"); |
| 87 | return false; |
| 88 | } else if (inputTensor->dims->size < 3) { |
| 89 | printf_err("Input tensor dimension should be >= 3\n"); |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | TfLiteIntArray* inputShape = model.GetInputShape(0); |
| 94 | |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 95 | const int inputImgCols = inputShape->data[YoloFastestModel::ms_inputColsIdx]; |
| 96 | const int inputImgRows = inputShape->data[YoloFastestModel::ms_inputRowsIdx]; |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 97 | |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 98 | /* Set up pre and post-processing. */ |
| 99 | DetectorPreProcess preProcess = DetectorPreProcess(inputTensor, true, model.IsDataSigned()); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 100 | |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 101 | std::vector<object_detection::DetectionResult> results; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 102 | const object_detection::PostProcessParams postProcessParams{ |
| 103 | inputImgRows, |
| 104 | inputImgCols, |
| 105 | object_detection::originalImageSize, |
| 106 | object_detection::anchor1, |
| 107 | object_detection::anchor2}; |
| 108 | DetectorPostProcess postProcess = |
| 109 | DetectorPostProcess(outputTensor0, outputTensor1, results, postProcessParams); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 110 | do { |
Matthew Sloyan | 0bc74e9 | 2022-05-10 13:21:01 +0100 | [diff] [blame] | 111 | /* Ensure there are no results leftover from previous inference when running all. */ |
| 112 | results.clear(); |
| 113 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 114 | /* Strings for presentation/logging. */ |
| 115 | std::string str_inf{"Running inference... "}; |
| 116 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 117 | const uint8_t* currImage = GetImgArray(ctx.Get<uint32_t>("imgIndex")); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 118 | |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 119 | auto dstPtr = static_cast<uint8_t*>(inputTensor->data.uint8); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 120 | const size_t copySz = |
| 121 | inputTensor->bytes < IMAGE_DATA_SIZE ? inputTensor->bytes : IMAGE_DATA_SIZE; |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 122 | |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 123 | /* Run the pre-processing, inference and post-processing. */ |
| 124 | if (!preProcess.DoPreProcess(currImage, copySz)) { |
| 125 | printf_err("Pre-processing failed."); |
| 126 | return false; |
| 127 | } |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 128 | |
Isabella Gottardi | e76a691 | 2022-02-16 10:42:32 +0000 | [diff] [blame] | 129 | /* Display image on the LCD. */ |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 130 | hal_lcd_display_image( |
Liam Barry | 213a543 | 2022-05-09 17:06:19 +0100 | [diff] [blame] | 131 | (arm::app::object_detection::channelsImageDisplayed == 3) ? currImage : dstPtr, |
| 132 | inputImgCols, |
| 133 | inputImgRows, |
| 134 | arm::app::object_detection::channelsImageDisplayed, |
| 135 | dataPsnImgStartX, |
| 136 | dataPsnImgStartY, |
| 137 | dataPsnImgDownscaleFactor); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 138 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 139 | /* Display message on the LCD - inference running. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 140 | hal_lcd_display_text( |
| 141 | str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 142 | |
| 143 | /* Run inference over this image. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 144 | info("Running inference on image %" PRIu32 " => %s\n", |
| 145 | ctx.Get<uint32_t>("imgIndex"), |
| 146 | GetFilename(ctx.Get<uint32_t>("imgIndex"))); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 147 | |
| 148 | if (!RunInference(model, profiler)) { |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 149 | printf_err("Inference failed."); |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | if (!postProcess.DoPostProcess()) { |
| 154 | printf_err("Post-processing failed."); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | |
| 158 | /* Erase. */ |
| 159 | str_inf = std::string(str_inf.size(), ' '); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 160 | hal_lcd_display_text( |
| 161 | str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 162 | |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 163 | /* Draw boxes. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 164 | DrawDetectionBoxes( |
| 165 | results, dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 166 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 167 | #if VERIFY_TEST_OUTPUT |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 168 | DumpTensor(modelOutput0); |
| 169 | DumpTensor(modelOutput1); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 170 | #endif /* VERIFY_TEST_OUTPUT */ |
| 171 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 172 | if (!PresentInferenceResult(results)) { |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
| 176 | profiler.PrintProfilingResult(); |
| 177 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 178 | IncrementAppCtxIfmIdx(ctx, "imgIndex"); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 179 | |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 180 | } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImgIdx); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 181 | |
| 182 | return true; |
| 183 | } |
| 184 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 185 | static bool |
| 186 | PresentInferenceResult(const std::vector<object_detection::DetectionResult>& results) |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 187 | { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 188 | hal_lcd_set_text_color(COLOR_GREEN); |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 189 | |
| 190 | /* If profiling is enabled, and the time is valid. */ |
| 191 | info("Final results:\n"); |
| 192 | info("Total number of inferences: 1\n"); |
| 193 | |
| 194 | for (uint32_t i = 0; i < results.size(); ++i) { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 195 | info("%" PRIu32 ") (%f) -> %s {x=%d,y=%d,w=%d,h=%d}\n", |
| 196 | i, |
| 197 | results[i].m_normalisedVal, |
| 198 | "Detection box:", |
| 199 | results[i].m_x0, |
| 200 | results[i].m_y0, |
| 201 | results[i].m_w, |
| 202 | results[i].m_h); |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | return true; |
| 206 | } |
| 207 | |
Richard Burton | ef90497 | 2022-04-27 17:24:36 +0100 | [diff] [blame] | 208 | static void DrawDetectionBoxes(const std::vector<object_detection::DetectionResult>& results, |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 209 | uint32_t imgStartX, |
| 210 | uint32_t imgStartY, |
| 211 | uint32_t imgDownscaleFactor) |
| 212 | { |
| 213 | uint32_t lineThickness = 1; |
| 214 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 215 | for (const auto& result : results) { |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 216 | /* Top line. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 217 | hal_lcd_display_box(imgStartX + result.m_x0 / imgDownscaleFactor, |
| 218 | imgStartY + result.m_y0 / imgDownscaleFactor, |
| 219 | result.m_w / imgDownscaleFactor, |
| 220 | lineThickness, |
| 221 | COLOR_GREEN); |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 222 | /* Bot line. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 223 | hal_lcd_display_box(imgStartX + result.m_x0 / imgDownscaleFactor, |
| 224 | imgStartY + (result.m_y0 + result.m_h) / imgDownscaleFactor - |
| 225 | lineThickness, |
| 226 | result.m_w / imgDownscaleFactor, |
| 227 | lineThickness, |
| 228 | COLOR_GREEN); |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 229 | |
| 230 | /* Left line. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 231 | hal_lcd_display_box(imgStartX + result.m_x0 / imgDownscaleFactor, |
| 232 | imgStartY + result.m_y0 / imgDownscaleFactor, |
| 233 | lineThickness, |
| 234 | result.m_h / imgDownscaleFactor, |
| 235 | COLOR_GREEN); |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 236 | /* Right line. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 237 | hal_lcd_display_box(imgStartX + (result.m_x0 + result.m_w) / imgDownscaleFactor - |
| 238 | lineThickness, |
| 239 | imgStartY + result.m_y0 / imgDownscaleFactor, |
| 240 | lineThickness, |
| 241 | result.m_h / imgDownscaleFactor, |
| 242 | COLOR_GREEN); |
Richard Burton | 9c54990 | 2022-02-15 16:39:18 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 246 | } /* namespace app */ |
| 247 | } /* namespace arm */ |