blob: 620ce6c6a43222f59a4fa312521239f6c6771b3e [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,
38 const std::vector<arm::app::object_detection::DetectionResult>& results);
Michael Levit06fcf752022-01-12 11:53:46 +020039
40 /* Object detection classification handler. */
41 bool ObjectDetectionHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll)
42 {
43 auto& platform = ctx.Get<hal_platform&>("platform");
44 auto& profiler = ctx.Get<Profiler&>("profiler");
45
46 constexpr uint32_t dataPsnImgDownscaleFactor = 1;
47 constexpr uint32_t dataPsnImgStartX = 10;
48 constexpr uint32_t dataPsnImgStartY = 35;
49
50 constexpr uint32_t dataPsnTxtInfStartX = 150;
51 constexpr uint32_t dataPsnTxtInfStartY = 40;
52
53 platform.data_psn->clear(COLOR_BLACK);
54
55 auto& model = ctx.Get<Model&>("model");
Isabella Gottardi3107aa22022-01-27 16:39:37 +000056
Michael Levit06fcf752022-01-12 11:53:46 +020057 /* If the request has a valid size, set the image index. */
58 if (imgIndex < NUMBER_OF_FILES) {
59 if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) {
60 return false;
61 }
62 }
63 if (!model.IsInited()) {
64 printf_err("Model is not initialised! Terminating processing.\n");
65 return false;
66 }
67
68 auto curImIdx = ctx.Get<uint32_t>("imgIndex");
69
70 TfLiteTensor* inputTensor = model.GetInputTensor(0);
71
72 if (!inputTensor->dims) {
73 printf_err("Invalid input tensor dims\n");
74 return false;
75 } else if (inputTensor->dims->size < 3) {
76 printf_err("Input tensor dimension should be >= 3\n");
77 return false;
78 }
79
80 TfLiteIntArray* inputShape = model.GetInputShape(0);
81
82 const uint32_t nCols = inputShape->data[arm::app::YoloFastestModel::ms_inputColsIdx];
83 const uint32_t nRows = inputShape->data[arm::app::YoloFastestModel::ms_inputRowsIdx];
Isabella Gottardi3107aa22022-01-27 16:39:37 +000084 const uint32_t nPresentationChannels = channelsImageDisplayed;
Michael Levit06fcf752022-01-12 11:53:46 +020085
Isabella Gottardi3107aa22022-01-27 16:39:37 +000086 /* Get pre/post-processing objects. */
87 auto& postp = ctx.Get<object_detection::DetectorPostprocessing&>("postprocess");
Michael Levit06fcf752022-01-12 11:53:46 +020088
89 do {
90 /* Strings for presentation/logging. */
91 std::string str_inf{"Running inference... "};
92
93 const uint8_t* curr_image = get_img_array(ctx.Get<uint32_t>("imgIndex"));
94
Isabella Gottardi3107aa22022-01-27 16:39:37 +000095 /* Copy over the data and convert to grayscale */
96 auto* dstPtr = static_cast<uint8_t*>(inputTensor->data.uint8);
97 const size_t copySz = inputTensor->bytes < IMAGE_DATA_SIZE ?
98 inputTensor->bytes : IMAGE_DATA_SIZE;
Michael Levit06fcf752022-01-12 11:53:46 +020099
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000100 /* Copy of the image used for presentation, original images are read-only */
101 std::vector<uint8_t> g_image_buffer(nCols*nRows*channelsImageDisplayed);
102 if (nPresentationChannels == 3) {
103 memcpy(g_image_buffer.data(),curr_image, nCols * nRows * channelsImageDisplayed);
104 } else {
105 image::RgbToGrayscale(curr_image, g_image_buffer.data(), nCols * nRows);
106 }
107 image::RgbToGrayscale(curr_image, dstPtr, copySz);
Michael Levit06fcf752022-01-12 11:53:46 +0200108
109 /* Display this image on the LCD. */
110 platform.data_psn->present_data_image(
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000111 g_image_buffer.data(),
Michael Levit06fcf752022-01-12 11:53:46 +0200112 nCols, nRows, nPresentationChannels,
113 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
114
115 /* If the data is signed. */
116 if (model.IsDataSigned()) {
117 image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes);
118 }
119
120 /* Display message on the LCD - inference running. */
121 platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(),
122 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
123
124 /* Run inference over this image. */
125 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
126 get_filename(ctx.Get<uint32_t>("imgIndex")));
127
128 if (!RunInference(model, profiler)) {
129 return false;
130 }
131
132 /* Erase. */
133 str_inf = std::string(str_inf.size(), ' ');
134 platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(),
135 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
136
137 /* Detector post-processing*/
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000138 std::vector<object_detection::DetectionResult> results;
139 TfLiteTensor* modelOutput0 = model.GetOutputTensor(0);
140 TfLiteTensor* modelOutput1 = model.GetOutputTensor(1);
141 postp.RunPostProcessing(
142 g_image_buffer.data(),
143 nRows,
144 nCols,
145 modelOutput0,
146 modelOutput1,
147 results);
Michael Levit06fcf752022-01-12 11:53:46 +0200148
149 platform.data_psn->present_data_image(
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000150 g_image_buffer.data(),
Michael Levit06fcf752022-01-12 11:53:46 +0200151 nCols, nRows, nPresentationChannels,
152 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
153
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
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000159 if (!PresentInferenceResult(platform, 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
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000172
173 static bool PresentInferenceResult(hal_platform& platform,
174 const std::vector<arm::app::object_detection::DetectionResult>& results)
175 {
176 platform.data_psn->set_text_color(COLOR_GREEN);
177
178 /* If profiling is enabled, and the time is valid. */
179 info("Final results:\n");
180 info("Total number of inferences: 1\n");
181
182 for (uint32_t i = 0; i < results.size(); ++i) {
183 info("%" PRIu32 ") (%f) -> %s {x=%d,y=%d,w=%d,h=%d}\n", i,
184 results[i].m_normalisedVal, "Detection box:",
185 results[i].m_x0, results[i].m_y0, results[i].m_w, results[i].m_h );
186 }
187
188 return true;
189 }
190
Michael Levit06fcf752022-01-12 11:53:46 +0200191} /* namespace app */
192} /* namespace arm */