blob: 84b5de3e51a8539d3f5e62544e960a1754007037 [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#ifndef USECASE_COMMON_UTILS_HPP
18#define USECASE_COMMON_UTILS_HPP
19
20#include "hal.h"
21#include "Model.hpp"
22#include "AppContext.hpp"
23#include "Profiler.hpp"
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010024#include "UseCaseHandler.hpp" /* Handlers for different user options. */
25#include "Classifier.hpp" /* Classifier. */
26#include "InputFiles.hpp"
27#include <inttypes.h>
28
alexander3c798932021-03-26 21:42:19 +000029
30/* Helper macro to convert RGB888 to RGB565 format. */
31#define RGB888_TO_RGB565(R8,G8,B8) ((((R8>>3) & 0x1F) << 11) | \
32 (((G8>>2) & 0x3F) << 5) | \
33 ((B8>>3) & 0x1F))
34
35constexpr uint16_t COLOR_BLACK = 0;
36constexpr uint16_t COLOR_GREEN = RGB888_TO_RGB565( 0, 255, 0); // 2016;
37constexpr uint16_t COLOR_YELLOW = RGB888_TO_RGB565(255, 255, 0); // 65504;
38
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010039
40void DisplayCommonMenu();
41
42namespace image{
43
44 /**
45 * @brief Helper function to convert a UINT8 image to INT8 format.
46 * @param[in,out] data Pointer to the data start.
47 * @param[in] kMaxImageSize Total number of pixels in the image.
48 **/
49 void ConvertImgToInt8(void * data, size_t kMaxImageSize);
50
51 /**
52 * @brief Presents inference results using the data presentation
53 * object.
54 * @param[in] platform Reference to the hal platform object.
55 * @param[in] results Vector of classification results to be displayed.
56 * @return true if successful, false otherwise.
57 **/
58 bool PresentInferenceResult(hal_platform & platform,
59 const std::vector < arm::app::ClassificationResult > & results);
Isabella Gottardi3107aa22022-01-27 16:39:37 +000060
61 /**
62 * @brief Converts RGB image to grayscale.
63 * @param[in] srcPtr Pointer to RGB source image.
64 * @param[out] dstPtr Pointer to grayscale destination image.
65 * @param[in] imgSz Destination image size.
66 **/
67 void RgbToGrayscale(const uint8_t *srcPtr, uint8_t *dstPtr, const size_t dstImgSz);
68}
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010069
70/**
71 * @brief Helper function to increment current input feature vector index.
72 * @param[in,out] ctx Pointer to the application context object.
73 * @param[in] useCase Use case name
74 **/
75void IncrementAppCtxIfmIdx(arm::app::ApplicationContext& ctx, std::string useCase);
76
77/**
78 * @brief Helper function to set the input feature map index.
79 * @param[in,out] ctx Pointer to the application context object.
80 * @param[in] idx Value to be set.
81 * @param[in] ctxIfmName Input Feature Map name
82 * @return true if index is set, false otherwise.
83 **/
84bool SetAppCtxIfmIdx(arm::app::ApplicationContext& ctx, uint32_t idx, std::string ctxIfmName);
85
86
87namespace common {
88
89 enum OPCODES {
90 MENU_OPT_RUN_INF_NEXT = 1, /* Run on next vector. */
91 MENU_OPT_RUN_INF_CHOSEN, /* Run on a user provided vector index. */
92 MENU_OPT_RUN_INF_ALL, /* Run inference on all. */
93 MENU_OPT_SHOW_MODEL_INFO, /* Show model info. */
94 MENU_OPT_LIST_IFM /* List the current IFM. */
95 };
96
97}
98
alexander3c798932021-03-26 21:42:19 +000099namespace arm {
100namespace app {
alexander3c798932021-03-26 21:42:19 +0000101 /**
102 * @brief Run inference using given model
103 * object. If profiling is enabled, it will log the
104 * statistics too.
alexander3c798932021-03-26 21:42:19 +0000105 * @param[in] model Reference to the initialised model.
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100106 * @param[in] profiler Reference to the initialised profiler.
alexander3c798932021-03-26 21:42:19 +0000107 * @return true if inference succeeds, false otherwise.
108 **/
Isabella Gottardi56ee6202021-05-12 08:27:15 +0100109 bool RunInference(arm::app::Model& model, Profiler& profiler);
alexander3c798932021-03-26 21:42:19 +0000110
111 /**
112 * @brief Read input and return as an integer.
113 * @param[in] platform Reference to the hal platform object.
alexander3c798932021-03-26 21:42:19 +0000114 * @return Integer value corresponding to the user input.
115 **/
116 int ReadUserInputAsInt(hal_platform& platform);
117
118#if VERIFY_TEST_OUTPUT
119 /**
120 * @brief Helper function to dump a tensor to stdout
121 * @param[in] tensor tensor to be dumped
122 * @param[in] lineBreakForNumElements number of elements
123 * after which line break will be added.
124 **/
alexander80eecfb2021-07-06 19:47:59 +0100125 void DumpTensor(const TfLiteTensor* tensor,
126 size_t lineBreakForNumElements = 16);
127
128
129 void DumpTensorData(const uint8_t* tensorData,
130 size_t size,
131 size_t lineBreakForNumElements = 16);
alexander3c798932021-03-26 21:42:19 +0000132#endif /* VERIFY_TEST_OUTPUT */
133
134 /**
135 * @brief List the files baked in the application.
136 * @param[in] ctx Reference to the application context.
137 * @return true or false based on event being handled.
138 **/
139 bool ListFilesHandler(ApplicationContext& ctx);
140
141} /* namespace app */
142} /* namespace arm */
143
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100144
145#endif /* USECASE_COMMON_UTILS_HPP */