blob: 4704c976eb7521e5ee2afa74e0c51eb098e536d2 [file] [log] [blame]
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01001/*
Kshitij Sisodia2ea46232022-12-19 16:37:33 +00002 * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates
3 * <open-source-office@arm.com> SPDX-License-Identifier: Apache-2.0
Éanna Ó Catháin8f958872021-09-15 09:32:30 +01004 *
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"
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010018#include "Classifier.hpp"
Richard Burtoned35a6f2022-02-14 11:55:35 +000019#include "ImageUtils.hpp"
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000020#include "InputFiles.hpp"
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010021#include "UseCaseCommonUtils.hpp"
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000022#include "VisualWakeWordModel.hpp"
23#include "VisualWakeWordProcessing.hpp"
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010024#include "hal.h"
alexander31ae9f02022-02-10 16:15:54 +000025#include "log_macros.h"
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. */
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000031 bool ClassifyImageHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll)
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010032 {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010033 auto& profiler = ctx.Get<Profiler&>("profiler");
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000034 auto& model = ctx.Get<Model&>("model");
Richard Burtonc20be972022-04-19 17:01:08 +010035 /* If the request has a valid size, set the image index. */
36 if (imgIndex < NUMBER_OF_FILES) {
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000037 if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) {
Richard Burtonc20be972022-04-19 17:01:08 +010038 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;
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000044 constexpr uint32_t dataPsnImgStartX = 10;
45 constexpr uint32_t dataPsnImgStartY = 35;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010046
47 constexpr uint32_t dataPsnTxtInfStartX = 150;
Richard Burton71f282e2022-12-01 12:31:23 +000048 constexpr uint32_t dataPsnTxtInfStartY = 40;
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010049
É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
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000055 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];
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000069 if (arm::app::VisualWakeWordModel::ms_inputChannelsIdx >=
70 static_cast<uint32_t>(inputShape->size)) {
Isabella Gottardi3107aa22022-01-27 16:39:37 +000071 printf_err("Invalid channel index.\n");
72 return false;
73 }
Richard Burtonc20be972022-04-19 17:01:08 +010074
75 /* We expect RGB images to be provided. */
76 const uint32_t displayChannels = 3;
77
78 /* Set up pre and post-processing. */
Richard Burtonb40ecf82022-04-22 16:14:57 +010079 VisualWakeWordPreProcess preProcess = VisualWakeWordPreProcess(inputTensor);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010080
81 std::vector<ClassificationResult> results;
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000082 VisualWakeWordPostProcess postProcess =
83 VisualWakeWordPostProcess(outputTensor,
84 ctx.Get<Classifier&>("classifier"),
85 ctx.Get<std::vector<std::string>&>("labels"),
86 results);
Richard Burtonc20be972022-04-19 17:01:08 +010087
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010088 do {
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010089 hal_lcd_clear(COLOR_BLACK);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010090
91 /* Strings for presentation/logging. */
92 std::string str_inf{"Running inference... "};
93
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000094 const uint8_t* imgSrc = GetImgArray(ctx.Get<uint32_t>("imgIndex"));
Richard Burtonc20be972022-04-19 17:01:08 +010095 if (nullptr == imgSrc) {
Kshitij Sisodia2ea46232022-12-19 16:37:33 +000096 printf_err("Failed to get image index %" PRIu32 " (max: %u)\n",
97 ctx.Get<uint32_t>("imgIndex"),
Richard Burtonc20be972022-04-19 17:01:08 +010098 NUMBER_OF_FILES - 1);
99 return false;
100 }
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100101
102 /* Display this image on the LCD. */
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000103 hal_lcd_display_image(imgSrc,
104 nCols,
105 nRows,
106 displayChannels,
107 dataPsnImgStartX,
108 dataPsnImgStartY,
109 dataPsnImgDownscaleFactor);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100110
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100111 /* Display message on the LCD - inference running. */
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000112 hal_lcd_display_text(
113 str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100114
115 /* Run inference over this image. */
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000116 info("Running inference on image %" PRIu32 " => %s\n",
117 ctx.Get<uint32_t>("imgIndex"),
118 GetFilename(ctx.Get<uint32_t>("imgIndex")));
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100119
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000120 const size_t imgSz =
121 inputTensor->bytes < IMAGE_DATA_SIZE ? inputTensor->bytes : IMAGE_DATA_SIZE;
Richard Burtonc20be972022-04-19 17:01:08 +0100122
123 /* Run the pre-processing, inference and post-processing. */
Richard Burtonb40ecf82022-04-22 16:14:57 +0100124 if (!preProcess.DoPreProcess(imgSrc, imgSz)) {
125 printf_err("Pre-processing failed.");
Richard Burtonc20be972022-04-19 17:01:08 +0100126 return false;
127 }
128
Richard Burtonb40ecf82022-04-22 16:14:57 +0100129 if (!RunInference(model, profiler)) {
130 printf_err("Inference failed.");
Richard Burtonc20be972022-04-19 17:01:08 +0100131 return false;
132 }
Richard Burtonc20be972022-04-19 17:01:08 +0100133
Richard Burtonb40ecf82022-04-22 16:14:57 +0100134 if (!postProcess.DoPostProcess()) {
135 printf_err("Post-processing failed.");
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100136 return false;
137 }
138
139 /* Erase. */
140 str_inf = std::string(str_inf.size(), ' ');
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000141 hal_lcd_display_text(
142 str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100143
144 /* Add results to context for access outside handler. */
145 ctx.Set<std::vector<ClassificationResult>>("results", results);
146
147#if VERIFY_TEST_OUTPUT
148 arm::app::DumpTensor(outputTensor);
149#endif /* VERIFY_TEST_OUTPUT */
150
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100151 if (!PresentInferenceResult(results)) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100152 return false;
153 }
154
155 profiler.PrintProfilingResult();
Richard Burtonc20be972022-04-19 17:01:08 +0100156
Kshitij Sisodia2ea46232022-12-19 16:37:33 +0000157 IncrementAppCtxIfmIdx(ctx, "imgIndex");
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100158
Richard Burtonc20be972022-04-19 17:01:08 +0100159 } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImgIdx);
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100160
161 return true;
162 }
163
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100164} /* namespace app */
Isabella Gottardi3107aa22022-01-27 16:39:37 +0000165} /* namespace arm */