blob: f3b317e0e2072ed63a31c4f65541bae521b0c8aa [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.
Isabella Gottardi3107aa22022-01-27 16:39:37 +000033 * @param[in] results Vector of detection results to be displayed.
34 * @return true if successful, false otherwise.
35 **/
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010036 static bool PresentInferenceResult(const std::vector<arm::app::object_detection::DetectionResult>& results);
Richard Burton9c549902022-02-15 16:39:18 +000037
38 /**
39 * @brief Draw boxes directly on the LCD for all detected objects.
Richard Burton9c549902022-02-15 16:39:18 +000040 * @param[in] results Vector of detection results to be displayed.
41 * @param[in] imageStartX X coordinate where the image starts on the LCD.
42 * @param[in] imageStartY Y coordinate where the image starts on the LCD.
43 * @param[in] imgDownscaleFactor How much image has been downscaled on LCD.
44 **/
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010045 static void DrawDetectionBoxes(
46 const std::vector<arm::app::object_detection::DetectionResult>& results,
47 uint32_t imgStartX,
48 uint32_t imgStartY,
49 uint32_t imgDownscaleFactor);
Michael Levit06fcf752022-01-12 11:53:46 +020050
51 /* Object detection classification handler. */
52 bool ObjectDetectionHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll)
53 {
Michael Levit06fcf752022-01-12 11:53:46 +020054 auto& profiler = ctx.Get<Profiler&>("profiler");
55
56 constexpr uint32_t dataPsnImgDownscaleFactor = 1;
57 constexpr uint32_t dataPsnImgStartX = 10;
58 constexpr uint32_t dataPsnImgStartY = 35;
59
60 constexpr uint32_t dataPsnTxtInfStartX = 150;
61 constexpr uint32_t dataPsnTxtInfStartY = 40;
62
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010063 hal_lcd_clear(COLOR_BLACK);
Michael Levit06fcf752022-01-12 11:53:46 +020064
65 auto& model = ctx.Get<Model&>("model");
Isabella Gottardi3107aa22022-01-27 16:39:37 +000066
Michael Levit06fcf752022-01-12 11:53:46 +020067 /* If the request has a valid size, set the image index. */
68 if (imgIndex < NUMBER_OF_FILES) {
69 if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) {
70 return false;
71 }
72 }
73 if (!model.IsInited()) {
74 printf_err("Model is not initialised! Terminating processing.\n");
75 return false;
76 }
77
78 auto curImIdx = ctx.Get<uint32_t>("imgIndex");
79
80 TfLiteTensor* inputTensor = model.GetInputTensor(0);
81
82 if (!inputTensor->dims) {
83 printf_err("Invalid input tensor dims\n");
84 return false;
85 } else if (inputTensor->dims->size < 3) {
86 printf_err("Input tensor dimension should be >= 3\n");
87 return false;
88 }
89
90 TfLiteIntArray* inputShape = model.GetInputShape(0);
91
92 const uint32_t nCols = inputShape->data[arm::app::YoloFastestModel::ms_inputColsIdx];
93 const uint32_t nRows = inputShape->data[arm::app::YoloFastestModel::ms_inputRowsIdx];
Michael Levit06fcf752022-01-12 11:53:46 +020094
Isabella Gottardi3107aa22022-01-27 16:39:37 +000095 /* Get pre/post-processing objects. */
96 auto& postp = ctx.Get<object_detection::DetectorPostprocessing&>("postprocess");
Michael Levit06fcf752022-01-12 11:53:46 +020097
98 do {
99 /* Strings for presentation/logging. */
100 std::string str_inf{"Running inference... "};
101
102 const uint8_t* curr_image = get_img_array(ctx.Get<uint32_t>("imgIndex"));
103
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000104 /* Copy over the data and convert to grayscale */
105 auto* dstPtr = static_cast<uint8_t*>(inputTensor->data.uint8);
106 const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ?
107 inputTensor->bytes : IMAGE_DATA_SIZE;
Michael Levit06fcf752022-01-12 11:53:46 +0200108
Richard Burton9c549902022-02-15 16:39:18 +0000109 /* Convert to gray scale and populate input tensor. */
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000110 image::RgbToGrayscale(curr_image, dstPtr, copySz);
Michael Levit06fcf752022-01-12 11:53:46 +0200111
Isabella Gottardie76a6912022-02-16 10:42:32 +0000112 /* Display image on the LCD. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100113 hal_lcd_display_image(
Isabella Gottardie76a6912022-02-16 10:42:32 +0000114 (channelsImageDisplayed == 3) ? curr_image : dstPtr,
115 nCols, nRows, channelsImageDisplayed,
Michael Levit06fcf752022-01-12 11:53:46 +0200116 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
117
118 /* If the data is signed. */
119 if (model.IsDataSigned()) {
120 image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes);
121 }
122
123 /* Display message on the LCD - inference running. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100124 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
Richard Burton9c549902022-02-15 16:39:18 +0000125 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
Michael Levit06fcf752022-01-12 11:53:46 +0200126
127 /* Run inference over this image. */
128 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
129 get_filename(ctx.Get<uint32_t>("imgIndex")));
130
131 if (!RunInference(model, profiler)) {
132 return false;
133 }
134
135 /* Erase. */
136 str_inf = std::string(str_inf.size(), ' ');
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100137 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
Richard Burton9c549902022-02-15 16:39:18 +0000138 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
Michael Levit06fcf752022-01-12 11:53:46 +0200139
140 /* Detector post-processing*/
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000141 std::vector<object_detection::DetectionResult> results;
142 TfLiteTensor* modelOutput0 = model.GetOutputTensor(0);
143 TfLiteTensor* modelOutput1 = model.GetOutputTensor(1);
144 postp.RunPostProcessing(
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000145 nRows,
146 nCols,
147 modelOutput0,
148 modelOutput1,
149 results);
Michael Levit06fcf752022-01-12 11:53:46 +0200150
Richard Burton9c549902022-02-15 16:39:18 +0000151 /* Draw boxes. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100152 DrawDetectionBoxes(results, dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
Michael Levit06fcf752022-01-12 11:53:46 +0200153
Michael Levit06fcf752022-01-12 11:53:46 +0200154#if VERIFY_TEST_OUTPUT
alexander31ae9f02022-02-10 16:15:54 +0000155 arm::app::DumpTensor(modelOutput0);
156 arm::app::DumpTensor(modelOutput1);
Michael Levit06fcf752022-01-12 11:53:46 +0200157#endif /* VERIFY_TEST_OUTPUT */
158
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100159 if (!PresentInferenceResult(results)) {
Michael Levit06fcf752022-01-12 11:53:46 +0200160 return false;
161 }
162
163 profiler.PrintProfilingResult();
164
165 IncrementAppCtxIfmIdx(ctx,"imgIndex");
166
167 } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx);
168
169 return true;
170 }
171
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100172 static bool PresentInferenceResult(const std::vector<arm::app::object_detection::DetectionResult>& results)
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000173 {
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100174 hal_lcd_set_text_color(COLOR_GREEN);
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000175
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
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100189 static void DrawDetectionBoxes(const std::vector<arm::app::object_detection::DetectionResult>& results,
Richard Burton9c549902022-02-15 16:39:18 +0000190 uint32_t imgStartX,
191 uint32_t imgStartY,
192 uint32_t imgDownscaleFactor)
193 {
194 uint32_t lineThickness = 1;
195
196 for (const auto& result: results) {
197 /* Top line. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100198 hal_lcd_display_box(imgStartX + result.m_x0/imgDownscaleFactor,
Richard Burton9c549902022-02-15 16:39:18 +0000199 imgStartY + result.m_y0/imgDownscaleFactor,
200 result.m_w/imgDownscaleFactor, lineThickness, COLOR_GREEN);
201 /* Bot line. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100202 hal_lcd_display_box(imgStartX + result.m_x0/imgDownscaleFactor,
Richard Burton9c549902022-02-15 16:39:18 +0000203 imgStartY + (result.m_y0 + result.m_h)/imgDownscaleFactor - lineThickness,
204 result.m_w/imgDownscaleFactor, lineThickness, COLOR_GREEN);
205
206 /* Left 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 lineThickness, result.m_h/imgDownscaleFactor, COLOR_GREEN);
210 /* Right line. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100211 hal_lcd_display_box(imgStartX + (result.m_x0 + result.m_w)/imgDownscaleFactor - lineThickness,
Richard Burton9c549902022-02-15 16:39:18 +0000212 imgStartY + result.m_y0/imgDownscaleFactor,
213 lineThickness, result.m_h/imgDownscaleFactor, COLOR_GREEN);
214 }
215 }
216
Michael Levit06fcf752022-01-12 11:53:46 +0200217} /* namespace app */
218} /* namespace arm */