blob: e9bcd4ad1efb83c11697a9ab81c01282a577df53 [file] [log] [blame]
Michael Levit06fcf752022-01-12 11:53:46 +02001/*
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 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 {
Liam Barry213a5432022-05-09 17:06:19 +010030 namespace object_detection {
31 extern const int channelsImageDisplayed;
32 } /* namespace object_detection */
Michael Levit06fcf752022-01-12 11:53:46 +020033
Isabella Gottardi3107aa22022-01-27 16:39:37 +000034 /**
35 * @brief Presents inference results along using the data presentation
36 * object.
Isabella Gottardi3107aa22022-01-27 16:39:37 +000037 * @param[in] results Vector of detection results to be displayed.
38 * @return true if successful, false otherwise.
39 **/
Richard Burtonef904972022-04-27 17:24:36 +010040 static bool PresentInferenceResult(const std::vector<object_detection::DetectionResult>& results);
Richard Burton9c549902022-02-15 16:39:18 +000041
42 /**
43 * @brief Draw boxes directly on the LCD for all detected objects.
Richard Burton9c549902022-02-15 16:39:18 +000044 * @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 Sisodia68fdd112022-04-06 13:03:20 +010049 static void DrawDetectionBoxes(
Richard Burtonef904972022-04-27 17:24:36 +010050 const std::vector<object_detection::DetectionResult>& results,
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010051 uint32_t imgStartX,
52 uint32_t imgStartY,
53 uint32_t imgDownscaleFactor);
Michael Levit06fcf752022-01-12 11:53:46 +020054
Richard Burtonef904972022-04-27 17:24:36 +010055 /* Object detection inference handler. */
Michael Levit06fcf752022-01-12 11:53:46 +020056 bool ObjectDetectionHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll)
57 {
Michael Levit06fcf752022-01-12 11:53:46 +020058 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 Sisodia68fdd112022-04-06 13:03:20 +010067 hal_lcd_clear(COLOR_BLACK);
Michael Levit06fcf752022-01-12 11:53:46 +020068
69 auto& model = ctx.Get<Model&>("model");
Isabella Gottardi3107aa22022-01-27 16:39:37 +000070
Michael Levit06fcf752022-01-12 11:53:46 +020071 /* 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 Burtonef904972022-04-27 17:24:36 +010082 auto initialImgIdx = ctx.Get<uint32_t>("imgIndex");
Michael Levit06fcf752022-01-12 11:53:46 +020083
84 TfLiteTensor* inputTensor = model.GetInputTensor(0);
Richard Burtonef904972022-04-27 17:24:36 +010085 TfLiteTensor* outputTensor0 = model.GetOutputTensor(0);
86 TfLiteTensor* outputTensor1 = model.GetOutputTensor(1);
Michael Levit06fcf752022-01-12 11:53:46 +020087
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 Burtonef904972022-04-27 17:24:36 +010098 const int inputImgCols = inputShape->data[YoloFastestModel::ms_inputColsIdx];
99 const int inputImgRows = inputShape->data[YoloFastestModel::ms_inputRowsIdx];
Michael Levit06fcf752022-01-12 11:53:46 +0200100
Richard Burtonef904972022-04-27 17:24:36 +0100101 /* Set up pre and post-processing. */
102 DetectorPreProcess preProcess = DetectorPreProcess(inputTensor, true, model.IsDataSigned());
Michael Levit06fcf752022-01-12 11:53:46 +0200103
Richard Burtonef904972022-04-27 17:24:36 +0100104 std::vector<object_detection::DetectionResult> results;
105 DetectorPostProcess postProcess = DetectorPostProcess(outputTensor0, outputTensor1,
106 results, inputImgRows, inputImgCols);
Michael Levit06fcf752022-01-12 11:53:46 +0200107 do {
Matthew Sloyan0bc74e92022-05-10 13:21:01 +0100108 /* Ensure there are no results leftover from previous inference when running all. */
109 results.clear();
110
Michael Levit06fcf752022-01-12 11:53:46 +0200111 /* Strings for presentation/logging. */
112 std::string str_inf{"Running inference... "};
113
Richard Burtonef904972022-04-27 17:24:36 +0100114 const uint8_t* currImage = get_img_array(ctx.Get<uint32_t>("imgIndex"));
Michael Levit06fcf752022-01-12 11:53:46 +0200115
Richard Burtonef904972022-04-27 17:24:36 +0100116 auto dstPtr = static_cast<uint8_t*>(inputTensor->data.uint8);
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000117 const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ?
118 inputTensor->bytes : IMAGE_DATA_SIZE;
Michael Levit06fcf752022-01-12 11:53:46 +0200119
Richard Burtonef904972022-04-27 17:24:36 +0100120 /* 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 Levit06fcf752022-01-12 11:53:46 +0200125
Isabella Gottardie76a6912022-02-16 10:42:32 +0000126 /* Display image on the LCD. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100127 hal_lcd_display_image(
Liam Barry213a5432022-05-09 17:06:19 +0100128 (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 Levit06fcf752022-01-12 11:53:46 +0200135
Michael Levit06fcf752022-01-12 11:53:46 +0200136 /* Display message on the LCD - inference running. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100137 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
Richard Burtonef904972022-04-27 17:24:36 +0100138 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
Michael Levit06fcf752022-01-12 11:53:46 +0200139
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 Burtonef904972022-04-27 17:24:36 +0100145 printf_err("Inference failed.");
146 return false;
147 }
148
149 if (!postProcess.DoPostProcess()) {
150 printf_err("Post-processing failed.");
Michael Levit06fcf752022-01-12 11:53:46 +0200151 return false;
152 }
153
154 /* Erase. */
155 str_inf = std::string(str_inf.size(), ' ');
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100156 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
Richard Burtonef904972022-04-27 17:24:36 +0100157 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
Michael Levit06fcf752022-01-12 11:53:46 +0200158
Richard Burton9c549902022-02-15 16:39:18 +0000159 /* Draw boxes. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100160 DrawDetectionBoxes(results, dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
Michael Levit06fcf752022-01-12 11:53:46 +0200161
Michael Levit06fcf752022-01-12 11:53:46 +0200162#if VERIFY_TEST_OUTPUT
Richard Burtonef904972022-04-27 17:24:36 +0100163 DumpTensor(modelOutput0);
164 DumpTensor(modelOutput1);
Michael Levit06fcf752022-01-12 11:53:46 +0200165#endif /* VERIFY_TEST_OUTPUT */
166
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100167 if (!PresentInferenceResult(results)) {
Michael Levit06fcf752022-01-12 11:53:46 +0200168 return false;
169 }
170
171 profiler.PrintProfilingResult();
172
173 IncrementAppCtxIfmIdx(ctx,"imgIndex");
174
Richard Burtonef904972022-04-27 17:24:36 +0100175 } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImgIdx);
Michael Levit06fcf752022-01-12 11:53:46 +0200176
177 return true;
178 }
179
Richard Burtonef904972022-04-27 17:24:36 +0100180 static bool PresentInferenceResult(const std::vector<object_detection::DetectionResult>& results)
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000181 {
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100182 hal_lcd_set_text_color(COLOR_GREEN);
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000183
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 Burtonef904972022-04-27 17:24:36 +0100197 static void DrawDetectionBoxes(const std::vector<object_detection::DetectionResult>& results,
Richard Burton9c549902022-02-15 16:39:18 +0000198 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 Sisodia68fdd112022-04-06 13:03:20 +0100206 hal_lcd_display_box(imgStartX + result.m_x0/imgDownscaleFactor,
Richard Burton9c549902022-02-15 16:39:18 +0000207 imgStartY + result.m_y0/imgDownscaleFactor,
208 result.m_w/imgDownscaleFactor, lineThickness, COLOR_GREEN);
209 /* Bot line. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100210 hal_lcd_display_box(imgStartX + result.m_x0/imgDownscaleFactor,
Richard Burton9c549902022-02-15 16:39:18 +0000211 imgStartY + (result.m_y0 + result.m_h)/imgDownscaleFactor - lineThickness,
212 result.m_w/imgDownscaleFactor, lineThickness, COLOR_GREEN);
213
214 /* Left line. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100215 hal_lcd_display_box(imgStartX + result.m_x0/imgDownscaleFactor,
Richard Burton9c549902022-02-15 16:39:18 +0000216 imgStartY + result.m_y0/imgDownscaleFactor,
217 lineThickness, result.m_h/imgDownscaleFactor, COLOR_GREEN);
218 /* Right line. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100219 hal_lcd_display_box(imgStartX + (result.m_x0 + result.m_w)/imgDownscaleFactor - lineThickness,
Richard Burton9c549902022-02-15 16:39:18 +0000220 imgStartY + result.m_y0/imgDownscaleFactor,
221 lineThickness, result.m_h/imgDownscaleFactor, COLOR_GREEN);
222 }
223 }
224
Michael Levit06fcf752022-01-12 11:53:46 +0200225} /* namespace app */
226} /* namespace arm */