blob: dd9a32dac7607cd6c8e7ddb81975e1c7ae4e2260 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Liam Barrye9588502022-01-25 14:31:15 +00002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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 "UseCaseCommonUtils.hpp"
alexander3c798932021-03-26 21:42:19 +000018#include "InputFiles.hpp"
alexander31ae9f02022-02-10 16:15:54 +000019#include "log_macros.h"
20
21#include <cinttypes>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010022
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010023
24void DisplayCommonMenu()
25{
26 printf("\n\n");
27 printf("User input required\n");
28 printf("Enter option number from:\n\n");
29 printf(" %u. Classify next ifm\n", common::MENU_OPT_RUN_INF_NEXT);
30 printf(" %u. Classify ifm at chosen index\n", common::MENU_OPT_RUN_INF_CHOSEN);
31 printf(" %u. Run classification on all ifm\n", common::MENU_OPT_RUN_INF_ALL);
32 printf(" %u. Show NN model info\n", common::MENU_OPT_SHOW_MODEL_INFO);
33 printf(" %u. List ifm\n\n", common::MENU_OPT_LIST_IFM);
34 printf(" Choice: ");
35 fflush(stdout);
36}
37
38void image::ConvertImgToInt8(void* data, const size_t kMaxImageSize)
39{
Isabella Gottardi79d41542021-10-20 15:52:32 +010040 auto* tmp_req_data = static_cast<uint8_t *>(data);
41 auto* tmp_signed_req_data = static_cast<int8_t *>(data);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010042
43 for (size_t i = 0; i < kMaxImageSize; i++) {
44 tmp_signed_req_data[i] = (int8_t) (
45 (int32_t) (tmp_req_data[i]) - 128);
46 }
47}
48
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010049
Kshitij Sisodiab48b5b62021-12-29 14:32:58 +000050bool image::PresentInferenceResult(
51 hal_platform &platform,
Liam Barrye9588502022-01-25 14:31:15 +000052 const std::vector<arm::app::ClassificationResult> &results)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010053{
54 constexpr uint32_t dataPsnTxtStartX1 = 150;
55 constexpr uint32_t dataPsnTxtStartY1 = 30;
56
57 constexpr uint32_t dataPsnTxtStartX2 = 10;
58 constexpr uint32_t dataPsnTxtStartY2 = 150;
59
60 constexpr uint32_t dataPsnTxtYIncr = 16; /* Row index increment. */
61
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010062 platform.data_psn->set_text_color(COLOR_GREEN);
63
64 /* Display each result. */
65 uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr;
66 uint32_t rowIdx2 = dataPsnTxtStartY2;
67
Liam Barrye9588502022-01-25 14:31:15 +000068 info("Final results:\n");
69 info("Total number of inferences: 1\n");
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010070
71 for (uint32_t i = 0; i < results.size(); ++i) {
72 std::string resultStr =
73 std::to_string(i + 1) + ") " +
74 std::to_string(results[i].m_labelIdx) +
75 " (" + std::to_string(results[i].m_normalisedVal) + ")";
76
77 platform.data_psn->present_data_text(
78 resultStr.c_str(), resultStr.size(),
alexander31ae9f02022-02-10 16:15:54 +000079 dataPsnTxtStartX1, rowIdx1, false);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010080 rowIdx1 += dataPsnTxtYIncr;
81
82 resultStr = std::to_string(i + 1) + ") " + results[i].m_label;
83 platform.data_psn->present_data_text(
84 resultStr.c_str(), resultStr.size(),
85 dataPsnTxtStartX2, rowIdx2, 0);
86 rowIdx2 += dataPsnTxtYIncr;
87
Richard Burton9b8d67a2021-12-10 12:32:51 +000088 info("%" PRIu32 ") %" PRIu32 " (%f) -> %s\n", i,
89 results[i].m_labelIdx, results[i].m_normalisedVal,
90 results[i].m_label.c_str());
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010091 }
92
93 return true;
94}
95
Isabella Gottardi3107aa22022-01-27 16:39:37 +000096void image::RgbToGrayscale(const uint8_t *srcPtr, uint8_t *dstPtr, const size_t dstImgSz)
97{
98 float R=0.299;
99 float G=0.587;
100 float B=0.114;
101 for (size_t i = 0; i < dstImgSz; ++i, srcPtr += 3) {
102 uint32_t int_gray = R * (*srcPtr) +
103 G * (*(srcPtr + 1)) +
104 B * (*(srcPtr + 2));
105 *dstPtr++ = int_gray <= std::numeric_limits<uint8_t>::max() ?
106 int_gray : std::numeric_limits<uint8_t>::max();
107 }
108}
109
alexander31ae9f02022-02-10 16:15:54 +0000110void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, const std::string& useCase)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100111{
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100112#if NUMBER_OF_FILES > 0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100113 auto curImIdx = ctx.Get<uint32_t>(useCase);
114
115 if (curImIdx + 1 >= NUMBER_OF_FILES) {
116 ctx.Set<uint32_t>(useCase, 0);
117 return;
118 }
119 ++curImIdx;
120 ctx.Set<uint32_t>(useCase, curImIdx);
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100121#else /* NUMBER_OF_FILES > 0 */
122 UNUSED(ctx);
123 UNUSED(useCase);
124#endif /* NUMBER_OF_FILES > 0 */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100125}
126
alexander31ae9f02022-02-10 16:15:54 +0000127bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, const std::string& ctxIfmName)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100128{
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100129#if NUMBER_OF_FILES > 0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100130 if (idx >= NUMBER_OF_FILES) {
131 printf_err("Invalid idx %" PRIu32 " (expected less than %u)\n",
132 idx, NUMBER_OF_FILES);
133 return false;
134 }
135 ctx.Set<uint32_t>(ctxIfmName, idx);
136 return true;
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100137#else /* NUMBER_OF_FILES > 0 */
138 UNUSED(ctx);
139 UNUSED(idx);
140 UNUSED(ctxIfmName);
141 return false;
142#endif /* NUMBER_OF_FILES > 0 */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100143}
144
alexander3c798932021-03-26 21:42:19 +0000145namespace arm {
146namespace app {
147
alexander3c798932021-03-26 21:42:19 +0000148
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100149bool RunInference(arm::app::Model& model, Profiler& profiler)
150{
151 profiler.StartProfiling("Inference");
152 bool runInf = model.RunInference();
153 profiler.StopProfiling();
154
155 return runInf;
156}
157
158int ReadUserInputAsInt(hal_platform& platform)
159{
160 char chInput[128];
161 memset(chInput, 0, sizeof(chInput));
162
163 platform.data_acq->get_input(chInput, sizeof(chInput));
164 return atoi(chInput);
165}
166
167void DumpTensorData(const uint8_t* tensorData,
168 size_t size,
169 size_t lineBreakForNumElements)
170{
171 char strhex[8];
172 std::string strdump;
173
174 for (size_t i = 0; i < size; ++i) {
175 if (0 == i % lineBreakForNumElements) {
176 printf("%s\n\t", strdump.c_str());
177 strdump.clear();
178 }
179 snprintf(strhex, sizeof(strhex) - 1,
180 "0x%02x, ", tensorData[i]);
181 strdump += std::string(strhex);
alexander3c798932021-03-26 21:42:19 +0000182 }
183
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100184 if (!strdump.empty()) {
185 printf("%s\n", strdump.c_str());
186 }
187}
alexander3c798932021-03-26 21:42:19 +0000188
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100189void DumpTensor(const TfLiteTensor* tensor, const size_t lineBreakForNumElements)
190{
191 if (!tensor) {
192 printf_err("invalid tensor\n");
193 return;
alexander3c798932021-03-26 21:42:19 +0000194 }
195
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100196 const uint32_t tensorSz = tensor->bytes;
alexander31ae9f02022-02-10 16:15:54 +0000197 const auto* tensorData = tflite::GetTensorData<uint8_t>(tensor);
alexander3c798932021-03-26 21:42:19 +0000198
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100199 DumpTensorData(tensorData, tensorSz, lineBreakForNumElements);
200}
alexander80eecfb2021-07-06 19:47:59 +0100201
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100202bool ListFilesHandler(ApplicationContext& ctx)
203{
204 auto& model = ctx.Get<Model&>("model");
205 auto& platform = ctx.Get<hal_platform&>("platform");
alexander80eecfb2021-07-06 19:47:59 +0100206
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100207 constexpr uint32_t dataPsnTxtStartX = 20;
208 constexpr uint32_t dataPsnTxtStartY = 40;
alexander3c798932021-03-26 21:42:19 +0000209
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100210 if (!model.IsInited()) {
211 printf_err("Model is not initialised! Terminating processing.\n");
212 return false;
alexander3c798932021-03-26 21:42:19 +0000213 }
214
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100215 /* Clear the LCD */
216 platform.data_psn->clear(COLOR_BLACK);
alexander3c798932021-03-26 21:42:19 +0000217
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100218 /* Show the total number of embedded files. */
219 std::string strNumFiles = std::string{"Total Number of Files: "} +
220 std::to_string(NUMBER_OF_FILES);
221 platform.data_psn->present_data_text(strNumFiles.c_str(),
222 strNumFiles.size(),
223 dataPsnTxtStartX,
224 dataPsnTxtStartY,
225 false);
alexander3c798932021-03-26 21:42:19 +0000226
227#if NUMBER_OF_FILES > 0
228 constexpr uint32_t dataPsnTxtYIncr = 16;
229 info("List of Files:\n");
230 uint32_t yVal = dataPsnTxtStartY + dataPsnTxtYIncr;
231 for (uint32_t i = 0; i < NUMBER_OF_FILES; ++i, yVal += dataPsnTxtYIncr) {
232
233 std::string currentFilename{get_filename(i)};
234 platform.data_psn->present_data_text(currentFilename.c_str(),
235 currentFilename.size(),
alexander80eecfb2021-07-06 19:47:59 +0100236 dataPsnTxtStartX, yVal, false);
alexander3c798932021-03-26 21:42:19 +0000237
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100238 info("\t%" PRIu32 " => %s\n", i, currentFilename.c_str());
alexander3c798932021-03-26 21:42:19 +0000239 }
240#endif /* NUMBER_OF_FILES > 0 */
241
242 return true;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100243}
alexander3c798932021-03-26 21:42:19 +0000244
245} /* namespace app */
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000246} /* namespace arm */