blob: 411b8680d36adaa702128e8cb24b8925606c7e8a [file] [log] [blame]
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
É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);
Richard Burtonb40ecf82022-04-22 16:14:57 +010056 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010057 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. */
Richard Burtonb40ecf82022-04-22 16:14:57 +010078 VisualWakeWordPreProcess preProcess = VisualWakeWordPreProcess(inputTensor);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010079
80 std::vector<ClassificationResult> results;
Richard Burtonb40ecf82022-04-22 16:14:57 +010081 VisualWakeWordPostProcess postProcess = VisualWakeWordPostProcess(outputTensor,
82 ctx.Get<Classifier&>("classifier"),
Richard Burtonc20be972022-04-19 17:01:08 +010083 ctx.Get<std::vector<std::string>&>("labels"), results);
84
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010085 do {
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010086 hal_lcd_clear(COLOR_BLACK);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010087
88 /* Strings for presentation/logging. */
89 std::string str_inf{"Running inference... "};
90
Richard Burtonc20be972022-04-19 17:01:08 +010091 const uint8_t* imgSrc = get_img_array(ctx.Get<uint32_t>("imgIndex"));
92 if (nullptr == imgSrc) {
93 printf_err("Failed to get image index %" PRIu32 " (max: %u)\n", ctx.Get<uint32_t>("imgIndex"),
94 NUMBER_OF_FILES - 1);
95 return false;
96 }
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010097
98 /* Display this image on the LCD. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010099 hal_lcd_display_image(
Richard Burtonc20be972022-04-19 17:01:08 +0100100 imgSrc,
101 nCols, nRows, displayChannels,
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100102 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
103
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100104 /* Display message on the LCD - inference running. */
Richard Burtonc20be972022-04-19 17:01:08 +0100105 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100106 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
107
108 /* Run inference over this image. */
109 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
110 get_filename(ctx.Get<uint32_t>("imgIndex")));
111
Richard Burtonc20be972022-04-19 17:01:08 +0100112 const size_t imgSz = inputTensor->bytes < IMAGE_DATA_SIZE ?
113 inputTensor->bytes : IMAGE_DATA_SIZE;
114
115 /* Run the pre-processing, inference and post-processing. */
Richard Burtonb40ecf82022-04-22 16:14:57 +0100116 if (!preProcess.DoPreProcess(imgSrc, imgSz)) {
117 printf_err("Pre-processing failed.");
Richard Burtonc20be972022-04-19 17:01:08 +0100118 return false;
119 }
120
Richard Burtonb40ecf82022-04-22 16:14:57 +0100121 if (!RunInference(model, profiler)) {
122 printf_err("Inference failed.");
Richard Burtonc20be972022-04-19 17:01:08 +0100123 return false;
124 }
Richard Burtonc20be972022-04-19 17:01:08 +0100125
Richard Burtonb40ecf82022-04-22 16:14:57 +0100126 if (!postProcess.DoPostProcess()) {
127 printf_err("Post-processing failed.");
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100128 return false;
129 }
130
131 /* Erase. */
132 str_inf = std::string(str_inf.size(), ' ');
Richard Burtonc20be972022-04-19 17:01:08 +0100133 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
134 dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100135
136 /* Add results to context for access outside handler. */
137 ctx.Set<std::vector<ClassificationResult>>("results", results);
138
139#if VERIFY_TEST_OUTPUT
140 arm::app::DumpTensor(outputTensor);
141#endif /* VERIFY_TEST_OUTPUT */
142
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100143 if (!PresentInferenceResult(results)) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100144 return false;
145 }
146
147 profiler.PrintProfilingResult();
Richard Burtonc20be972022-04-19 17:01:08 +0100148
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100149 IncrementAppCtxIfmIdx(ctx,"imgIndex");
150
Richard Burtonc20be972022-04-19 17:01:08 +0100151 } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImgIdx);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100152
153 return true;
154 }
155
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100156} /* namespace app */
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000157} /* namespace arm */