blob: 084059ea9218e04c3cbd18ccf010fbeef860f3ad [file] [log] [blame]
Michael Levit06fcf752022-01-12 11:53:46 +02001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
Michael Levit06fcf752022-01-12 11:53:46 +02003 * 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 Levit06fcf752022-01-12 11:53:46 +020021#include "DetectorPostProcessing.hpp"
Richard Burtonef904972022-04-27 17:24:36 +010022#include "DetectorPreProcessing.hpp"
Michael Levit06fcf752022-01-12 11:53:46 +020023#include "hal.h"
alexander31ae9f02022-02-10 16:15:54 +000024#include "log_macros.h"
Michael Levit06fcf752022-01-12 11:53:46 +020025
alexander31ae9f02022-02-10 16:15:54 +000026#include <cinttypes>
Michael Levit06fcf752022-01-12 11:53:46 +020027
Michael Levit06fcf752022-01-12 11:53:46 +020028namespace arm {
29namespace app {
30
Isabella Gottardi3107aa22022-01-27 16:39:37 +000031 /**
32 * @brief Presents inference results along using the data presentation
33 * object.
Isabella Gottardi3107aa22022-01-27 16:39:37 +000034 * @param[in] results Vector of detection results to be displayed.
35 * @return true if successful, false otherwise.
36 **/
Richard Burtonef904972022-04-27 17:24:36 +010037 static bool PresentInferenceResult(const std::vector<object_detection::DetectionResult>& results);
Richard Burton9c549902022-02-15 16:39:18 +000038
39 /**
40 * @brief Draw boxes directly on the LCD for all detected objects.
Richard Burton9c549902022-02-15 16:39:18 +000041 * @param[in] results Vector of detection results to be displayed.
42 * @param[in] imageStartX X coordinate where the image starts on the LCD.
43 * @param[in] imageStartY Y coordinate where the image starts on the LCD.
44 * @param[in] imgDownscaleFactor How much image has been downscaled on LCD.
45 **/
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010046 static void DrawDetectionBoxes(
Richard Burtonef904972022-04-27 17:24:36 +010047 const std::vector<object_detection::DetectionResult>& results,
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010048 uint32_t imgStartX,
49 uint32_t imgStartY,
50 uint32_t imgDownscaleFactor);
Michael Levit06fcf752022-01-12 11:53:46 +020051
Richard Burtonef904972022-04-27 17:24:36 +010052 /* Object detection inference handler. */
Michael Levit06fcf752022-01-12 11:53:46 +020053 bool ObjectDetectionHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll)
54 {
Michael Levit06fcf752022-01-12 11:53:46 +020055 auto& profiler = ctx.Get<Profiler&>("profiler");
56
57 constexpr uint32_t dataPsnImgDownscaleFactor = 1;
58 constexpr uint32_t dataPsnImgStartX = 10;
59 constexpr uint32_t dataPsnImgStartY = 35;
60
Richard Burton71f282e2022-12-01 12:31:23 +000061 constexpr uint32_t dataPsnTxtInfStartX = 20;
62 constexpr uint32_t dataPsnTxtInfStartY = 28;
Michael Levit06fcf752022-01-12 11:53:46 +020063
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010064 hal_lcd_clear(COLOR_BLACK);
Michael Levit06fcf752022-01-12 11:53:46 +020065
66 auto& model = ctx.Get<Model&>("model");
Isabella Gottardi3107aa22022-01-27 16:39:37 +000067
Michael Levit06fcf752022-01-12 11:53:46 +020068 /* 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 Burtonef904972022-04-27 17:24:36 +010079 auto initialImgIdx = ctx.Get<uint32_t>("imgIndex");
Michael Levit06fcf752022-01-12 11:53:46 +020080
81 TfLiteTensor* inputTensor = model.GetInputTensor(0);
Richard Burtonef904972022-04-27 17:24:36 +010082 TfLiteTensor* outputTensor0 = model.GetOutputTensor(0);
83 TfLiteTensor* outputTensor1 = model.GetOutputTensor(1);
Michael Levit06fcf752022-01-12 11:53:46 +020084
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 Burtonef904972022-04-27 17:24:36 +010095 const int inputImgCols = inputShape->data[YoloFastestModel::ms_inputColsIdx];
96 const int inputImgRows = inputShape->data[YoloFastestModel::ms_inputRowsIdx];
Michael Levit06fcf752022-01-12 11:53:46 +020097
Richard Burtonef904972022-04-27 17:24:36 +010098 /* Set up pre and post-processing. */
99 DetectorPreProcess preProcess = DetectorPreProcess(inputTensor, true, model.IsDataSigned());
Michael Levit06fcf752022-01-12 11:53:46 +0200100
Richard Burtonef904972022-04-27 17:24:36 +0100101 std::vector<object_detection::DetectionResult> results;
Richard Burton6f6df092022-05-17 12:52:50 +0100102 const object_detection::PostProcessParams postProcessParams {
103 inputImgRows, inputImgCols, object_detection::originalImageSize,
104 object_detection::anchor1, object_detection::anchor2
105 };
Richard Burtonef904972022-04-27 17:24:36 +0100106 DetectorPostProcess postProcess = DetectorPostProcess(outputTensor0, outputTensor1,
Richard Burton6f6df092022-05-17 12:52:50 +0100107 results, postProcessParams);
Michael Levit06fcf752022-01-12 11:53:46 +0200108 do {
Matthew Sloyan0bc74e92022-05-10 13:21:01 +0100109 /* Ensure there are no results leftover from previous inference when running all. */
110 results.clear();
111
Michael Levit06fcf752022-01-12 11:53:46 +0200112 /* Strings for presentation/logging. */
113 std::string str_inf{"Running inference... "};
114
Richard Burtonef904972022-04-27 17:24:36 +0100115 const uint8_t* currImage = get_img_array(ctx.Get<uint32_t>("imgIndex"));
Michael Levit06fcf752022-01-12 11:53:46 +0200116
Richard Burtonef904972022-04-27 17:24:36 +0100117 auto dstPtr = static_cast<uint8_t*>(inputTensor->data.uint8);
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000118 const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ?
119 inputTensor->bytes : IMAGE_DATA_SIZE;
Michael Levit06fcf752022-01-12 11:53:46 +0200120
Richard Burtonef904972022-04-27 17:24:36 +0100121 /* Run the pre-processing, inference and post-processing. */
122 if (!preProcess.DoPreProcess(currImage, copySz)) {
123 printf_err("Pre-processing failed.");
124 return false;
125 }
Michael Levit06fcf752022-01-12 11:53:46 +0200126
Isabella Gottardie76a6912022-02-16 10:42:32 +0000127 /* Display image on the LCD. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100128 hal_lcd_display_image(
Liam Barry213a5432022-05-09 17:06:19 +0100129 (arm::app::object_detection::channelsImageDisplayed == 3) ? currImage : dstPtr,
130 inputImgCols,
131 inputImgRows,
132 arm::app::object_detection::channelsImageDisplayed,
133 dataPsnImgStartX,
134 dataPsnImgStartY,
135 dataPsnImgDownscaleFactor);
Michael Levit06fcf752022-01-12 11:53:46 +0200136
Michael Levit06fcf752022-01-12 11:53:46 +0200137 /* Display message on the LCD - inference running. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100138 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
Richard Burtonef904972022-04-27 17:24:36 +0100139 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
Michael Levit06fcf752022-01-12 11:53:46 +0200140
141 /* Run inference over this image. */
142 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
143 get_filename(ctx.Get<uint32_t>("imgIndex")));
144
145 if (!RunInference(model, profiler)) {
Richard Burtonef904972022-04-27 17:24:36 +0100146 printf_err("Inference failed.");
147 return false;
148 }
149
150 if (!postProcess.DoPostProcess()) {
151 printf_err("Post-processing failed.");
Michael Levit06fcf752022-01-12 11:53:46 +0200152 return false;
153 }
154
155 /* Erase. */
156 str_inf = std::string(str_inf.size(), ' ');
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100157 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
Richard Burtonef904972022-04-27 17:24:36 +0100158 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
Michael Levit06fcf752022-01-12 11:53:46 +0200159
Richard Burton9c549902022-02-15 16:39:18 +0000160 /* Draw boxes. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100161 DrawDetectionBoxes(results, dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
Michael Levit06fcf752022-01-12 11:53:46 +0200162
Michael Levit06fcf752022-01-12 11:53:46 +0200163#if VERIFY_TEST_OUTPUT
Richard Burtonef904972022-04-27 17:24:36 +0100164 DumpTensor(modelOutput0);
165 DumpTensor(modelOutput1);
Michael Levit06fcf752022-01-12 11:53:46 +0200166#endif /* VERIFY_TEST_OUTPUT */
167
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100168 if (!PresentInferenceResult(results)) {
Michael Levit06fcf752022-01-12 11:53:46 +0200169 return false;
170 }
171
172 profiler.PrintProfilingResult();
173
174 IncrementAppCtxIfmIdx(ctx,"imgIndex");
175
Richard Burtonef904972022-04-27 17:24:36 +0100176 } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImgIdx);
Michael Levit06fcf752022-01-12 11:53:46 +0200177
178 return true;
179 }
180
Richard Burtonef904972022-04-27 17:24:36 +0100181 static bool PresentInferenceResult(const std::vector<object_detection::DetectionResult>& results)
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000182 {
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100183 hal_lcd_set_text_color(COLOR_GREEN);
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000184
185 /* If profiling is enabled, and the time is valid. */
186 info("Final results:\n");
187 info("Total number of inferences: 1\n");
188
189 for (uint32_t i = 0; i < results.size(); ++i) {
190 info("%" PRIu32 ") (%f) -> %s {x=%d,y=%d,w=%d,h=%d}\n", i,
191 results[i].m_normalisedVal, "Detection box:",
192 results[i].m_x0, results[i].m_y0, results[i].m_w, results[i].m_h );
193 }
194
195 return true;
196 }
197
Richard Burtonef904972022-04-27 17:24:36 +0100198 static void DrawDetectionBoxes(const std::vector<object_detection::DetectionResult>& results,
Richard Burton9c549902022-02-15 16:39:18 +0000199 uint32_t imgStartX,
200 uint32_t imgStartY,
201 uint32_t imgDownscaleFactor)
202 {
203 uint32_t lineThickness = 1;
204
205 for (const auto& result: results) {
206 /* Top line. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100207 hal_lcd_display_box(imgStartX + result.m_x0/imgDownscaleFactor,
Richard Burton9c549902022-02-15 16:39:18 +0000208 imgStartY + result.m_y0/imgDownscaleFactor,
209 result.m_w/imgDownscaleFactor, lineThickness, COLOR_GREEN);
210 /* Bot line. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100211 hal_lcd_display_box(imgStartX + result.m_x0/imgDownscaleFactor,
Richard Burton9c549902022-02-15 16:39:18 +0000212 imgStartY + (result.m_y0 + result.m_h)/imgDownscaleFactor - lineThickness,
213 result.m_w/imgDownscaleFactor, lineThickness, COLOR_GREEN);
214
215 /* Left line. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100216 hal_lcd_display_box(imgStartX + result.m_x0/imgDownscaleFactor,
Richard Burton9c549902022-02-15 16:39:18 +0000217 imgStartY + result.m_y0/imgDownscaleFactor,
218 lineThickness, result.m_h/imgDownscaleFactor, COLOR_GREEN);
219 /* Right line. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100220 hal_lcd_display_box(imgStartX + (result.m_x0 + result.m_w)/imgDownscaleFactor - lineThickness,
Richard Burton9c549902022-02-15 16:39:18 +0000221 imgStartY + result.m_y0/imgDownscaleFactor,
222 lineThickness, result.m_h/imgDownscaleFactor, COLOR_GREEN);
223 }
224 }
225
Michael Levit06fcf752022-01-12 11:53:46 +0200226} /* namespace app */
227} /* namespace arm */