blob: 257da4f4e23ba9a070e4554c742ce5f37dfc4a7f [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"
22#include "hal.h"
alexander31ae9f02022-02-10 16:15:54 +000023#include "log_macros.h"
Michael Levit06fcf752022-01-12 11:53:46 +020024
alexander31ae9f02022-02-10 16:15:54 +000025#include <cinttypes>
Michael Levit06fcf752022-01-12 11:53:46 +020026
Michael Levit06fcf752022-01-12 11:53:46 +020027namespace arm {
28namespace app {
29
Isabella Gottardi3107aa22022-01-27 16:39:37 +000030 /**
31 * @brief Presents inference results along using the data presentation
32 * object.
33 * @param[in] platform Reference to the hal platform object.
34 * @param[in] results Vector of detection results to be displayed.
35 * @return true if successful, false otherwise.
36 **/
37 static bool PresentInferenceResult(hal_platform& platform,
Richard Burton9c549902022-02-15 16:39:18 +000038 const std::vector<arm::app::object_detection::DetectionResult>& results);
39
40 /**
41 * @brief Draw boxes directly on the LCD for all detected objects.
42 * @param[in] platform Reference to the hal platform object.
43 * @param[in] results Vector of detection results to be displayed.
44 * @param[in] imageStartX X coordinate where the image starts on the LCD.
45 * @param[in] imageStartY Y coordinate where the image starts on the LCD.
46 * @param[in] imgDownscaleFactor How much image has been downscaled on LCD.
47 **/
48 static void DrawDetectionBoxes(hal_platform& platform,
49 const std::vector<arm::app::object_detection::DetectionResult>& results,
50 uint32_t imgStartX,
51 uint32_t imgStartY,
52 uint32_t imgDownscaleFactor);
Michael Levit06fcf752022-01-12 11:53:46 +020053
54 /* Object detection classification handler. */
55 bool ObjectDetectionHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll)
56 {
57 auto& platform = ctx.Get<hal_platform&>("platform");
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
67 platform.data_psn->clear(COLOR_BLACK);
68
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
82 auto curImIdx = ctx.Get<uint32_t>("imgIndex");
83
84 TfLiteTensor* inputTensor = model.GetInputTensor(0);
85
86 if (!inputTensor->dims) {
87 printf_err("Invalid input tensor dims\n");
88 return false;
89 } else if (inputTensor->dims->size < 3) {
90 printf_err("Input tensor dimension should be >= 3\n");
91 return false;
92 }
93
94 TfLiteIntArray* inputShape = model.GetInputShape(0);
95
96 const uint32_t nCols = inputShape->data[arm::app::YoloFastestModel::ms_inputColsIdx];
97 const uint32_t nRows = inputShape->data[arm::app::YoloFastestModel::ms_inputRowsIdx];
Isabella Gottardi3107aa22022-01-27 16:39:37 +000098 const uint32_t nPresentationChannels = channelsImageDisplayed;
Michael Levit06fcf752022-01-12 11:53:46 +020099
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000100 /* Get pre/post-processing objects. */
101 auto& postp = ctx.Get<object_detection::DetectorPostprocessing&>("postprocess");
Michael Levit06fcf752022-01-12 11:53:46 +0200102
103 do {
104 /* Strings for presentation/logging. */
105 std::string str_inf{"Running inference... "};
106
107 const uint8_t* curr_image = get_img_array(ctx.Get<uint32_t>("imgIndex"));
108
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000109 /* Copy over the data and convert to grayscale */
110 auto* dstPtr = static_cast<uint8_t*>(inputTensor->data.uint8);
111 const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ?
112 inputTensor->bytes : IMAGE_DATA_SIZE;
Michael Levit06fcf752022-01-12 11:53:46 +0200113
Richard Burton9c549902022-02-15 16:39:18 +0000114 /* Convert to gray scale and populate input tensor. */
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000115 image::RgbToGrayscale(curr_image, dstPtr, copySz);
Michael Levit06fcf752022-01-12 11:53:46 +0200116
Richard Burton9c549902022-02-15 16:39:18 +0000117 /* Display original image on the LCD. */
Michael Levit06fcf752022-01-12 11:53:46 +0200118 platform.data_psn->present_data_image(
Richard Burton9c549902022-02-15 16:39:18 +0000119 curr_image,
Michael Levit06fcf752022-01-12 11:53:46 +0200120 nCols, nRows, nPresentationChannels,
121 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
122
123 /* If the data is signed. */
124 if (model.IsDataSigned()) {
125 image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes);
126 }
127
128 /* Display message on the LCD - inference running. */
129 platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(),
Richard Burton9c549902022-02-15 16:39:18 +0000130 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
Michael Levit06fcf752022-01-12 11:53:46 +0200131
132 /* Run inference over this image. */
133 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
134 get_filename(ctx.Get<uint32_t>("imgIndex")));
135
136 if (!RunInference(model, profiler)) {
137 return false;
138 }
139
140 /* Erase. */
141 str_inf = std::string(str_inf.size(), ' ');
142 platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(),
Richard Burton9c549902022-02-15 16:39:18 +0000143 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
Michael Levit06fcf752022-01-12 11:53:46 +0200144
145 /* Detector post-processing*/
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000146 std::vector<object_detection::DetectionResult> results;
147 TfLiteTensor* modelOutput0 = model.GetOutputTensor(0);
148 TfLiteTensor* modelOutput1 = model.GetOutputTensor(1);
149 postp.RunPostProcessing(
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000150 nRows,
151 nCols,
152 modelOutput0,
153 modelOutput1,
154 results);
Michael Levit06fcf752022-01-12 11:53:46 +0200155
Richard Burton9c549902022-02-15 16:39:18 +0000156 /* Draw boxes. */
157 DrawDetectionBoxes(platform, results, dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
Michael Levit06fcf752022-01-12 11:53:46 +0200158
Michael Levit06fcf752022-01-12 11:53:46 +0200159#if VERIFY_TEST_OUTPUT
alexander31ae9f02022-02-10 16:15:54 +0000160 arm::app::DumpTensor(modelOutput0);
161 arm::app::DumpTensor(modelOutput1);
Michael Levit06fcf752022-01-12 11:53:46 +0200162#endif /* VERIFY_TEST_OUTPUT */
163
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000164 if (!PresentInferenceResult(platform, results)) {
Michael Levit06fcf752022-01-12 11:53:46 +0200165 return false;
166 }
167
168 profiler.PrintProfilingResult();
169
170 IncrementAppCtxIfmIdx(ctx,"imgIndex");
171
172 } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx);
173
174 return true;
175 }
176
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000177
178 static bool PresentInferenceResult(hal_platform& platform,
179 const std::vector<arm::app::object_detection::DetectionResult>& results)
180 {
181 platform.data_psn->set_text_color(COLOR_GREEN);
182
183 /* If profiling is enabled, and the time is valid. */
184 info("Final results:\n");
185 info("Total number of inferences: 1\n");
186
187 for (uint32_t i = 0; i < results.size(); ++i) {
188 info("%" PRIu32 ") (%f) -> %s {x=%d,y=%d,w=%d,h=%d}\n", i,
189 results[i].m_normalisedVal, "Detection box:",
190 results[i].m_x0, results[i].m_y0, results[i].m_w, results[i].m_h );
191 }
192
193 return true;
194 }
195
Richard Burton9c549902022-02-15 16:39:18 +0000196 static void DrawDetectionBoxes(hal_platform& platform,
197 const std::vector<arm::app::object_detection::DetectionResult>& results,
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. */
206 platform.data_psn->present_box(imgStartX + result.m_x0/imgDownscaleFactor,
207 imgStartY + result.m_y0/imgDownscaleFactor,
208 result.m_w/imgDownscaleFactor, lineThickness, COLOR_GREEN);
209 /* Bot line. */
210 platform.data_psn->present_box(imgStartX + result.m_x0/imgDownscaleFactor,
211 imgStartY + (result.m_y0 + result.m_h)/imgDownscaleFactor - lineThickness,
212 result.m_w/imgDownscaleFactor, lineThickness, COLOR_GREEN);
213
214 /* Left line. */
215 platform.data_psn->present_box(imgStartX + result.m_x0/imgDownscaleFactor,
216 imgStartY + result.m_y0/imgDownscaleFactor,
217 lineThickness, result.m_h/imgDownscaleFactor, COLOR_GREEN);
218 /* Right line. */
219 platform.data_psn->present_box(imgStartX + (result.m_x0 + result.m_w)/imgDownscaleFactor - lineThickness,
220 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 */