alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
| 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 Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame^] | 21 | #include <inttypes.h> |
| 22 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 23 | namespace arm { |
| 24 | namespace app { |
| 25 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 26 | bool RunInference(arm::app::Model& model, Profiler& profiler) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 27 | { |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 28 | profiler.StartProfiling("Inference"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 29 | bool runInf = model.RunInference(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 30 | profiler.StopProfiling(); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 31 | |
| 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 | |
| 44 | void DumpTensor(TfLiteTensor* tensor, const size_t lineBreakForNumElements) |
| 45 | { |
| 46 | char strhex[8]; |
| 47 | std::string strdump; |
| 48 | |
| 49 | if (!tensor) { |
| 50 | printf_err("invalid tensor\n"); |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | const uint32_t tensorSz = tensor->bytes; |
| 55 | const uint8_t* tensorData = tflite::GetTensorData<uint8_t>(tensor); |
| 56 | |
| 57 | for (size_t i = 0; i < tensorSz; ++i) { |
| 58 | if (0 == i % lineBreakForNumElements) { |
| 59 | printf("%s\n\t", strdump.c_str()); |
| 60 | strdump.clear(); |
| 61 | } |
| 62 | snprintf(strhex, sizeof(strhex) - 1, |
| 63 | "0x%02x, ", tensorData[i]); |
| 64 | strdump += std::string(strhex); |
| 65 | } |
| 66 | |
| 67 | if (strdump.size()) { |
| 68 | printf("%s\n", strdump.c_str()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | bool ListFilesHandler(ApplicationContext& ctx) |
| 73 | { |
| 74 | auto& model = ctx.Get<Model&>("model"); |
| 75 | auto& platform = ctx.Get<hal_platform&>("platform"); |
| 76 | |
| 77 | constexpr uint32_t dataPsnTxtStartX = 20; |
| 78 | constexpr uint32_t dataPsnTxtStartY = 40; |
| 79 | |
| 80 | if (!model.IsInited()) { |
| 81 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | /* Clear the LCD */ |
| 86 | platform.data_psn->clear(COLOR_BLACK); |
| 87 | |
| 88 | /* Show the total number of embedded files. */ |
| 89 | std::string strNumFiles = std::string{"Total Number of Files: "} + |
| 90 | std::to_string(NUMBER_OF_FILES); |
| 91 | platform.data_psn->present_data_text(strNumFiles.c_str(), |
| 92 | strNumFiles.size(), |
| 93 | dataPsnTxtStartX, |
| 94 | dataPsnTxtStartY, |
| 95 | 0); |
| 96 | |
| 97 | #if NUMBER_OF_FILES > 0 |
| 98 | constexpr uint32_t dataPsnTxtYIncr = 16; |
| 99 | info("List of Files:\n"); |
| 100 | uint32_t yVal = dataPsnTxtStartY + dataPsnTxtYIncr; |
| 101 | for (uint32_t i = 0; i < NUMBER_OF_FILES; ++i, yVal += dataPsnTxtYIncr) { |
| 102 | |
| 103 | std::string currentFilename{get_filename(i)}; |
| 104 | platform.data_psn->present_data_text(currentFilename.c_str(), |
| 105 | currentFilename.size(), |
| 106 | dataPsnTxtStartX, yVal, 0); |
| 107 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame^] | 108 | info("\t%" PRIu32 " => %s\n", i, currentFilename.c_str()); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 109 | } |
| 110 | #endif /* NUMBER_OF_FILES > 0 */ |
| 111 | |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | } /* namespace app */ |
| 116 | } /* namespace arm */ |