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" |
| 22 | #include "hal.h" |
| 23 | |
| 24 | #include <inttypes.h> |
| 25 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 26 | namespace arm { |
| 27 | namespace app { |
| 28 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 29 | /** |
| 30 | * @brief Presents inference results along using the data presentation |
| 31 | * object. |
| 32 | * @param[in] platform Reference to the hal platform object. |
| 33 | * @param[in] results Vector of detection results to be displayed. |
| 34 | * @return true if successful, false otherwise. |
| 35 | **/ |
| 36 | static bool PresentInferenceResult(hal_platform& platform, |
| 37 | const std::vector<arm::app::object_detection::DetectionResult>& results); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 38 | |
| 39 | /* Object detection classification handler. */ |
| 40 | bool ObjectDetectionHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll) |
| 41 | { |
| 42 | auto& platform = ctx.Get<hal_platform&>("platform"); |
| 43 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
| 44 | |
| 45 | constexpr uint32_t dataPsnImgDownscaleFactor = 1; |
| 46 | constexpr uint32_t dataPsnImgStartX = 10; |
| 47 | constexpr uint32_t dataPsnImgStartY = 35; |
| 48 | |
| 49 | constexpr uint32_t dataPsnTxtInfStartX = 150; |
| 50 | constexpr uint32_t dataPsnTxtInfStartY = 40; |
| 51 | |
| 52 | platform.data_psn->clear(COLOR_BLACK); |
| 53 | |
| 54 | auto& model = ctx.Get<Model&>("model"); |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 55 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 56 | /* If the request has a valid size, set the image index. */ |
| 57 | if (imgIndex < NUMBER_OF_FILES) { |
| 58 | if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | if (!model.IsInited()) { |
| 63 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | auto curImIdx = ctx.Get<uint32_t>("imgIndex"); |
| 68 | |
| 69 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 70 | |
| 71 | if (!inputTensor->dims) { |
| 72 | printf_err("Invalid input tensor dims\n"); |
| 73 | return false; |
| 74 | } else if (inputTensor->dims->size < 3) { |
| 75 | printf_err("Input tensor dimension should be >= 3\n"); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | TfLiteIntArray* inputShape = model.GetInputShape(0); |
| 80 | |
| 81 | const uint32_t nCols = inputShape->data[arm::app::YoloFastestModel::ms_inputColsIdx]; |
| 82 | const uint32_t nRows = inputShape->data[arm::app::YoloFastestModel::ms_inputRowsIdx]; |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 83 | const uint32_t nPresentationChannels = channelsImageDisplayed; |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 84 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 85 | /* Get pre/post-processing objects. */ |
| 86 | auto& postp = ctx.Get<object_detection::DetectorPostprocessing&>("postprocess"); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 87 | |
| 88 | do { |
| 89 | /* Strings for presentation/logging. */ |
| 90 | std::string str_inf{"Running inference... "}; |
| 91 | |
| 92 | const uint8_t* curr_image = get_img_array(ctx.Get<uint32_t>("imgIndex")); |
| 93 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 94 | /* Copy over the data and convert to grayscale */ |
| 95 | auto* dstPtr = static_cast<uint8_t*>(inputTensor->data.uint8); |
| 96 | const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ? |
| 97 | inputTensor->bytes : IMAGE_DATA_SIZE; |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 98 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 99 | /* Copy of the image used for presentation, original images are read-only */ |
| 100 | std::vector<uint8_t> g_image_buffer(nCols*nRows*channelsImageDisplayed); |
| 101 | if (nPresentationChannels == 3) { |
| 102 | memcpy(g_image_buffer.data(),curr_image, nCols * nRows * channelsImageDisplayed); |
| 103 | } else { |
| 104 | image::RgbToGrayscale(curr_image, g_image_buffer.data(), nCols * nRows); |
| 105 | } |
| 106 | image::RgbToGrayscale(curr_image, dstPtr, copySz); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 107 | |
| 108 | /* Display this image on the LCD. */ |
| 109 | platform.data_psn->present_data_image( |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 110 | g_image_buffer.data(), |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 111 | nCols, nRows, nPresentationChannels, |
| 112 | dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor); |
| 113 | |
| 114 | /* If the data is signed. */ |
| 115 | if (model.IsDataSigned()) { |
| 116 | image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes); |
| 117 | } |
| 118 | |
| 119 | /* Display message on the LCD - inference running. */ |
| 120 | platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(), |
| 121 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
| 122 | |
| 123 | /* Run inference over this image. */ |
| 124 | info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"), |
| 125 | get_filename(ctx.Get<uint32_t>("imgIndex"))); |
| 126 | |
| 127 | if (!RunInference(model, profiler)) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | /* Erase. */ |
| 132 | str_inf = std::string(str_inf.size(), ' '); |
| 133 | platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(), |
| 134 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
| 135 | |
| 136 | /* Detector post-processing*/ |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 137 | std::vector<object_detection::DetectionResult> results; |
| 138 | TfLiteTensor* modelOutput0 = model.GetOutputTensor(0); |
| 139 | TfLiteTensor* modelOutput1 = model.GetOutputTensor(1); |
| 140 | postp.RunPostProcessing( |
| 141 | g_image_buffer.data(), |
| 142 | nRows, |
| 143 | nCols, |
| 144 | modelOutput0, |
| 145 | modelOutput1, |
| 146 | results); |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 147 | |
| 148 | platform.data_psn->present_data_image( |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 149 | g_image_buffer.data(), |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 150 | nCols, nRows, nPresentationChannels, |
| 151 | dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor); |
| 152 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 153 | #if VERIFY_TEST_OUTPUT |
| 154 | arm::app::DumpTensor(outputTensor); |
| 155 | #endif /* VERIFY_TEST_OUTPUT */ |
| 156 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 157 | if (!PresentInferenceResult(platform, results)) { |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 158 | return false; |
| 159 | } |
| 160 | |
| 161 | profiler.PrintProfilingResult(); |
| 162 | |
| 163 | IncrementAppCtxIfmIdx(ctx,"imgIndex"); |
| 164 | |
| 165 | } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx); |
| 166 | |
| 167 | return true; |
| 168 | } |
| 169 | |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame^] | 170 | |
| 171 | static bool PresentInferenceResult(hal_platform& platform, |
| 172 | const std::vector<arm::app::object_detection::DetectionResult>& results) |
| 173 | { |
| 174 | platform.data_psn->set_text_color(COLOR_GREEN); |
| 175 | |
| 176 | /* If profiling is enabled, and the time is valid. */ |
| 177 | info("Final results:\n"); |
| 178 | info("Total number of inferences: 1\n"); |
| 179 | |
| 180 | for (uint32_t i = 0; i < results.size(); ++i) { |
| 181 | info("%" PRIu32 ") (%f) -> %s {x=%d,y=%d,w=%d,h=%d}\n", i, |
| 182 | results[i].m_normalisedVal, "Detection box:", |
| 183 | results[i].m_x0, results[i].m_y0, results[i].m_w, results[i].m_h ); |
| 184 | } |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame] | 189 | } /* namespace app */ |
| 190 | } /* namespace arm */ |