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