blob: 7d0a2ca559fab076dc268ece34770ac211ce1174 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Kshitij Sisodia2ea46232022-12-19 16:37:33 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates
3 * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0
alexander3c798932021-03-26 21:42:19 +00004 *
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"
Richard Burtoned35a6f2022-02-14 11:55:35 +000018#include "ImageUtils.hpp"
alexander3c798932021-03-26 21:42:19 +000019#include "InputFiles.hpp"
alexander31ae9f02022-02-10 16:15:54 +000020#include "log_macros.h"
21
22#include <cinttypes>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010023
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010024void 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
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000038bool PresentInferenceResult(const std::vector<arm::app::ClassificationResult>& results)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010039{
40 constexpr uint32_t dataPsnTxtStartX1 = 150;
41 constexpr uint32_t dataPsnTxtStartY1 = 30;
42
43 constexpr uint32_t dataPsnTxtStartX2 = 10;
44 constexpr uint32_t dataPsnTxtStartY2 = 150;
45
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000046 constexpr uint32_t dataPsnTxtYIncr = 16; /* Row index increment. */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010047
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010048 hal_lcd_set_text_color(COLOR_GREEN);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010049
50 /* Display each result. */
51 uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr;
52 uint32_t rowIdx2 = dataPsnTxtStartY2;
53
Liam Barrye9588502022-01-25 14:31:15 +000054 info("Final results:\n");
55 info("Total number of inferences: 1\n");
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010056
57 for (uint32_t i = 0; i < results.size(); ++i) {
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000058 std::string resultStr = std::to_string(i + 1) + ") " +
59 std::to_string(results[i].m_labelIdx) + " (" +
60 std::to_string(results[i].m_normalisedVal) + ")";
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010061
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010062 hal_lcd_display_text(
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000063 resultStr.c_str(), resultStr.size(), dataPsnTxtStartX1, rowIdx1, false);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010064 rowIdx1 += dataPsnTxtYIncr;
65
66 resultStr = std::to_string(i + 1) + ") " + results[i].m_label;
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000067 hal_lcd_display_text(resultStr.c_str(), resultStr.size(), dataPsnTxtStartX2, rowIdx2, 0);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010068 rowIdx2 += dataPsnTxtYIncr;
69
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000070 info("%" PRIu32 ") %" PRIu32 " (%f) -> %s\n",
71 i,
72 results[i].m_labelIdx,
73 results[i].m_normalisedVal,
74 results[i].m_label.c_str());
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010075 }
76
77 return true;
78}
79
alexander31ae9f02022-02-10 16:15:54 +000080void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, const std::string& useCase)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010081{
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010082#if NUMBER_OF_FILES > 0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010083 auto curImIdx = ctx.Get<uint32_t>(useCase);
84
85 if (curImIdx + 1 >= NUMBER_OF_FILES) {
86 ctx.Set<uint32_t>(useCase, 0);
87 return;
88 }
89 ++curImIdx;
90 ctx.Set<uint32_t>(useCase, curImIdx);
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000091#else /* NUMBER_OF_FILES > 0 */
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010092 UNUSED(ctx);
93 UNUSED(useCase);
94#endif /* NUMBER_OF_FILES > 0 */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010095}
96
alexander31ae9f02022-02-10 16:15:54 +000097bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, const std::string& ctxIfmName)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010098{
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +010099#if NUMBER_OF_FILES > 0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100100 if (idx >= NUMBER_OF_FILES) {
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000101 printf_err("Invalid idx %" PRIu32 " (expected less than %u)\n", idx, NUMBER_OF_FILES);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100102 return false;
103 }
104 ctx.Set<uint32_t>(ctxIfmName, idx);
105 return true;
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000106#else /* NUMBER_OF_FILES > 0 */
Kshitij Sisodiaaa5e1f62021-09-24 14:42:08 +0100107 UNUSED(ctx);
108 UNUSED(idx);
109 UNUSED(ctxIfmName);
110 return false;
111#endif /* NUMBER_OF_FILES > 0 */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100112}
113
alexander3c798932021-03-26 21:42:19 +0000114namespace arm {
115namespace app {
116
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000117 bool RunInference(arm::app::Model& model, Profiler& profiler)
118 {
119 profiler.StartProfiling("Inference");
120 bool runInf = model.RunInference();
121 profiler.StopProfiling();
alexander3c798932021-03-26 21:42:19 +0000122
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000123 return runInf;
124 }
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100125
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000126 int ReadUserInputAsInt()
127 {
128 char chInput[128];
129 memset(chInput, 0, sizeof(chInput));
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100130
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000131 hal_get_user_input(chInput, sizeof(chInput));
132 return atoi(chInput);
133 }
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100134
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000135 void DumpTensorData(const uint8_t* tensorData, size_t size, size_t lineBreakForNumElements)
136 {
137 char strhex[8];
138 std::string strdump;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100139
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000140 for (size_t i = 0; i < size; ++i) {
141 if (0 == i % lineBreakForNumElements) {
142 printf("%s\n\t", strdump.c_str());
143 strdump.clear();
144 }
145 snprintf(strhex, sizeof(strhex) - 1, "0x%02x, ", tensorData[i]);
146 strdump += std::string(strhex);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100147 }
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000148
149 if (!strdump.empty()) {
150 printf("%s\n", strdump.c_str());
151 }
alexander3c798932021-03-26 21:42:19 +0000152 }
153
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000154 void DumpTensor(const TfLiteTensor* tensor, const size_t lineBreakForNumElements)
155 {
156 if (!tensor) {
157 printf_err("invalid tensor\n");
158 return;
159 }
alexander3c798932021-03-26 21:42:19 +0000160
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000161 const uint32_t tensorSz = tensor->bytes;
162 const auto* tensorData = tflite::GetTensorData<uint8_t>(tensor);
163
164 DumpTensorData(tensorData, tensorSz, lineBreakForNumElements);
alexander3c798932021-03-26 21:42:19 +0000165 }
166
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000167 bool ListFilesHandler(ApplicationContext& ctx)
168 {
169 auto& model = ctx.Get<Model&>("model");
alexander3c798932021-03-26 21:42:19 +0000170
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000171 constexpr uint32_t dataPsnTxtStartX = 20;
172 constexpr uint32_t dataPsnTxtStartY = 40;
alexander80eecfb2021-07-06 19:47:59 +0100173
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000174 if (!model.IsInited()) {
175 printf_err("Model is not initialised! Terminating processing.\n");
176 return false;
177 }
alexander80eecfb2021-07-06 19:47:59 +0100178
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000179 /* Clear the LCD */
180 hal_lcd_clear(COLOR_BLACK);
alexander3c798932021-03-26 21:42:19 +0000181
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000182 /* Show the total number of embedded files. */
183 std::string strNumFiles =
184 std::string{"Total Number of Files: "} + std::to_string(NUMBER_OF_FILES);
185 hal_lcd_display_text(
186 strNumFiles.c_str(), strNumFiles.size(), dataPsnTxtStartX, dataPsnTxtStartY, false);
alexander3c798932021-03-26 21:42:19 +0000187
188#if NUMBER_OF_FILES > 0
189 constexpr uint32_t dataPsnTxtYIncr = 16;
190 info("List of Files:\n");
191 uint32_t yVal = dataPsnTxtStartY + dataPsnTxtYIncr;
192 for (uint32_t i = 0; i < NUMBER_OF_FILES; ++i, yVal += dataPsnTxtYIncr) {
193
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000194 std::string currentFilename{GetFilename(i)};
195 hal_lcd_display_text(
196 currentFilename.c_str(), currentFilename.size(), dataPsnTxtStartX, yVal, false);
alexander3c798932021-03-26 21:42:19 +0000197
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100198 info("\t%" PRIu32 " => %s\n", i, currentFilename.c_str());
alexander3c798932021-03-26 21:42:19 +0000199 }
200#endif /* NUMBER_OF_FILES > 0 */
201
202 return true;
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000203 }
alexander3c798932021-03-26 21:42:19 +0000204
205} /* namespace app */
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000206} /* namespace arm */