blob: 615f684f8973e12d7d917de2c125d9ab99ea20d2 [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
2 * Copyright (c) 2021 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 "UseCaseCommonUtils.hpp"
18
19#include "InputFiles.hpp"
20
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010021#include <inttypes.h>
22
alexander3c798932021-03-26 21:42:19 +000023namespace arm {
24namespace app {
25
Isabella Gottardi8df12f32021-04-07 17:15:31 +010026 bool RunInference(arm::app::Model& model, Profiler& profiler)
alexander3c798932021-03-26 21:42:19 +000027 {
Isabella Gottardi8df12f32021-04-07 17:15:31 +010028 profiler.StartProfiling("Inference");
alexander3c798932021-03-26 21:42:19 +000029 bool runInf = model.RunInference();
alexander3c798932021-03-26 21:42:19 +000030 profiler.StopProfiling();
alexander3c798932021-03-26 21:42:19 +000031
32 return runInf;
33 }
34
35 int ReadUserInputAsInt(hal_platform& platform)
36 {
37 char chInput[128];
38 memset(chInput, 0, sizeof(chInput));
39
40 platform.data_acq->get_input(chInput, sizeof(chInput));
41 return atoi(chInput);
42 }
43
alexander80eecfb2021-07-06 19:47:59 +010044 void DumpTensorData(const uint8_t* tensorData,
45 size_t size,
46 size_t lineBreakForNumElements)
47 {
48 char strhex[8];
49 std::string strdump;
alexander3c798932021-03-26 21:42:19 +000050
alexander80eecfb2021-07-06 19:47:59 +010051 for (size_t i = 0; i < size; ++i) {
52 if (0 == i % lineBreakForNumElements) {
53 printf("%s\n\t", strdump.c_str());
54 strdump.clear();
55 }
56 snprintf(strhex, sizeof(strhex) - 1,
57 "0x%02x, ", tensorData[i]);
58 strdump += std::string(strhex);
59 }
60
61 if (!strdump.empty()) {
62 printf("%s\n", strdump.c_str());
63 }
64 }
65
66 void DumpTensor(const TfLiteTensor* tensor, const size_t lineBreakForNumElements)
67 {
alexander3c798932021-03-26 21:42:19 +000068 if (!tensor) {
69 printf_err("invalid tensor\n");
70 return;
71 }
72
73 const uint32_t tensorSz = tensor->bytes;
74 const uint8_t* tensorData = tflite::GetTensorData<uint8_t>(tensor);
75
alexander80eecfb2021-07-06 19:47:59 +010076 DumpTensorData(tensorData, tensorSz, lineBreakForNumElements);
alexander3c798932021-03-26 21:42:19 +000077 }
78
79 bool ListFilesHandler(ApplicationContext& ctx)
80 {
81 auto& model = ctx.Get<Model&>("model");
82 auto& platform = ctx.Get<hal_platform&>("platform");
83
84 constexpr uint32_t dataPsnTxtStartX = 20;
85 constexpr uint32_t dataPsnTxtStartY = 40;
86
87 if (!model.IsInited()) {
88 printf_err("Model is not initialised! Terminating processing.\n");
89 return false;
90 }
91
92 /* Clear the LCD */
93 platform.data_psn->clear(COLOR_BLACK);
94
95 /* Show the total number of embedded files. */
96 std::string strNumFiles = std::string{"Total Number of Files: "} +
97 std::to_string(NUMBER_OF_FILES);
98 platform.data_psn->present_data_text(strNumFiles.c_str(),
99 strNumFiles.size(),
100 dataPsnTxtStartX,
101 dataPsnTxtStartY,
alexander80eecfb2021-07-06 19:47:59 +0100102 false);
alexander3c798932021-03-26 21:42:19 +0000103
104#if NUMBER_OF_FILES > 0
105 constexpr uint32_t dataPsnTxtYIncr = 16;
106 info("List of Files:\n");
107 uint32_t yVal = dataPsnTxtStartY + dataPsnTxtYIncr;
108 for (uint32_t i = 0; i < NUMBER_OF_FILES; ++i, yVal += dataPsnTxtYIncr) {
109
110 std::string currentFilename{get_filename(i)};
111 platform.data_psn->present_data_text(currentFilename.c_str(),
112 currentFilename.size(),
alexander80eecfb2021-07-06 19:47:59 +0100113 dataPsnTxtStartX, yVal, false);
alexander3c798932021-03-26 21:42:19 +0000114
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100115 info("\t%" PRIu32 " => %s\n", i, currentFilename.c_str());
alexander3c798932021-03-26 21:42:19 +0000116 }
117#endif /* NUMBER_OF_FILES > 0 */
118
119 return true;
120 }
121
122} /* namespace app */
123} /* namespace arm */