blob: 7681f89448f45623be5cbb157df752d8e5ca9fcd [file] [log] [blame]
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01001/*
Liam Barrye9588502022-01-25 14:31:15 +00002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01003 * 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 "UseCaseHandler.hpp"
18#include "VisualWakeWordModel.hpp"
19#include "Classifier.hpp"
20#include "InputFiles.hpp"
Richard Burtoned35a6f2022-02-14 11:55:35 +000021#include "ImageUtils.hpp"
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010022#include "UseCaseCommonUtils.hpp"
23#include "hal.h"
alexander31ae9f02022-02-10 16:15:54 +000024#include "log_macros.h"
Richard Burtonc20be972022-04-19 17:01:08 +010025#include "VisualWakeWordProcessing.hpp"
Isabella Gottardi79d41542021-10-20 15:52:32 +010026
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010027namespace arm {
28namespace app {
29
Richard Burtonc20be972022-04-19 17:01:08 +010030 /* Visual Wake Word inference handler. */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010031 bool ClassifyImageHandler(ApplicationContext &ctx, uint32_t imgIndex, bool runAll)
32 {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010033 auto& profiler = ctx.Get<Profiler&>("profiler");
Richard Burtonc20be972022-04-19 17:01:08 +010034 auto& model = ctx.Get<Model&>("model");
35 /* If the request has a valid size, set the image index. */
36 if (imgIndex < NUMBER_OF_FILES) {
37 if (!SetAppCtxIfmIdx(ctx, imgIndex,"imgIndex")) {
38 return false;
39 }
40 }
41 auto initialImgIdx = ctx.Get<uint32_t>("imgIndex");
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010042
43 constexpr uint32_t dataPsnImgDownscaleFactor = 1;
44 constexpr uint32_t dataPsnImgStartX = 10;
45 constexpr uint32_t dataPsnImgStartY = 35;
46
47 constexpr uint32_t dataPsnTxtInfStartX = 150;
48 constexpr uint32_t dataPsnTxtInfStartY = 70;
49
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010050 if (!model.IsInited()) {
51 printf_err("Model is not initialised! Terminating processing.\n");
52 return false;
53 }
54
Richard Burtonc20be972022-04-19 17:01:08 +010055 TfLiteTensor* inputTensor = model.GetInputTensor(0);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010056
57 if (!inputTensor->dims) {
58 printf_err("Invalid input tensor dims\n");
59 return false;
Richard Burtonc20be972022-04-19 17:01:08 +010060 } else if (inputTensor->dims->size < 4) {
61 printf_err("Input tensor dimension should be = 4\n");
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010062 return false;
63 }
Richard Burtonc20be972022-04-19 17:01:08 +010064
65 /* Get input shape for displaying the image. */
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010066 TfLiteIntArray* inputShape = model.GetInputShape(0);
Isabella Gottardi3107aa22022-01-27 16:39:37 +000067 const uint32_t nCols = inputShape->data[arm::app::VisualWakeWordModel::ms_inputColsIdx];
68 const uint32_t nRows = inputShape->data[arm::app::VisualWakeWordModel::ms_inputRowsIdx];
69 if (arm::app::VisualWakeWordModel::ms_inputChannelsIdx >= static_cast<uint32_t>(inputShape->size)) {
70 printf_err("Invalid channel index.\n");
71 return false;
72 }
Richard Burtonc20be972022-04-19 17:01:08 +010073
74 /* We expect RGB images to be provided. */
75 const uint32_t displayChannels = 3;
76
77 /* Set up pre and post-processing. */
78 VisualWakeWordPreProcess preprocess = VisualWakeWordPreProcess(&model);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010079
80 std::vector<ClassificationResult> results;
Richard Burtonc20be972022-04-19 17:01:08 +010081 VisualWakeWordPostProcess postprocess = VisualWakeWordPostProcess(
82 ctx.Get<Classifier&>("classifier"), &model,
83 ctx.Get<std::vector<std::string>&>("labels"), results);
84
85 UseCaseRunner runner = UseCaseRunner(&preprocess, &postprocess, &model);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010086
87 do {
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010088 hal_lcd_clear(COLOR_BLACK);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010089
90 /* Strings for presentation/logging. */
91 std::string str_inf{"Running inference... "};
92
Richard Burtonc20be972022-04-19 17:01:08 +010093 const uint8_t* imgSrc = get_img_array(ctx.Get<uint32_t>("imgIndex"));
94 if (nullptr == imgSrc) {
95 printf_err("Failed to get image index %" PRIu32 " (max: %u)\n", ctx.Get<uint32_t>("imgIndex"),
96 NUMBER_OF_FILES - 1);
97 return false;
98 }
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010099
100 /* Display this image on the LCD. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100101 hal_lcd_display_image(
Richard Burtonc20be972022-04-19 17:01:08 +0100102 imgSrc,
103 nCols, nRows, displayChannels,
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100104 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
105
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100106 /* Display message on the LCD - inference running. */
Richard Burtonc20be972022-04-19 17:01:08 +0100107 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100108 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
109
110 /* Run inference over this image. */
111 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
112 get_filename(ctx.Get<uint32_t>("imgIndex")));
113
Richard Burtonc20be972022-04-19 17:01:08 +0100114 const size_t imgSz = inputTensor->bytes < IMAGE_DATA_SIZE ?
115 inputTensor->bytes : IMAGE_DATA_SIZE;
116
117 /* Run the pre-processing, inference and post-processing. */
118 if (!runner.PreProcess(imgSrc, imgSz)) {
119 return false;
120 }
121
122 profiler.StartProfiling("Inference");
123 if (!runner.RunInference()) {
124 return false;
125 }
126 profiler.StopProfiling();
127
128 if (!runner.PostProcess()) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100129 return false;
130 }
131
132 /* Erase. */
133 str_inf = std::string(str_inf.size(), ' ');
Richard Burtonc20be972022-04-19 17:01:08 +0100134 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
135 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100136
137 /* Add results to context for access outside handler. */
138 ctx.Set<std::vector<ClassificationResult>>("results", results);
139
140#if VERIFY_TEST_OUTPUT
Richard Burtonc20be972022-04-19 17:01:08 +0100141 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100142 arm::app::DumpTensor(outputTensor);
143#endif /* VERIFY_TEST_OUTPUT */
144
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100145 if (!PresentInferenceResult(results)) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100146 return false;
147 }
148
149 profiler.PrintProfilingResult();
Richard Burtonc20be972022-04-19 17:01:08 +0100150
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100151 IncrementAppCtxIfmIdx(ctx,"imgIndex");
152
Richard Burtonc20be972022-04-19 17:01:08 +0100153 } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImgIdx);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100154
155 return true;
156 }
157
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100158} /* namespace app */
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000159} /* namespace arm */