Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 1 | /* |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 2 | * 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áin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 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" |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 18 | #include "Classifier.hpp" |
Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 19 | #include "ImageUtils.hpp" |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 20 | #include "InputFiles.hpp" |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 21 | #include "UseCaseCommonUtils.hpp" |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 22 | #include "VisualWakeWordModel.hpp" |
| 23 | #include "VisualWakeWordProcessing.hpp" |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 24 | #include "hal.h" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 25 | #include "log_macros.h" |
Isabella Gottardi | 79d4154 | 2021-10-20 15:52:32 +0100 | [diff] [blame] | 26 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 27 | namespace arm { |
| 28 | namespace app { |
| 29 | |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 30 | /* Visual Wake Word inference handler. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 31 | bool ClassifyImageHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll) |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 32 | { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 33 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 34 | auto& model = ctx.Get<Model&>("model"); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 35 | /* If the request has a valid size, set the image index. */ |
| 36 | if (imgIndex < NUMBER_OF_FILES) { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 37 | if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) { |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | auto initialImgIdx = ctx.Get<uint32_t>("imgIndex"); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 42 | |
| 43 | constexpr uint32_t dataPsnImgDownscaleFactor = 1; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 44 | constexpr uint32_t dataPsnImgStartX = 10; |
| 45 | constexpr uint32_t dataPsnImgStartY = 35; |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 46 | |
| 47 | constexpr uint32_t dataPsnTxtInfStartX = 150; |
Richard Burton | 71f282e | 2022-12-01 12:31:23 +0000 | [diff] [blame] | 48 | constexpr uint32_t dataPsnTxtInfStartY = 40; |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 49 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 50 | if (!model.IsInited()) { |
| 51 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 52 | return false; |
| 53 | } |
| 54 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 55 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 56 | TfLiteTensor* outputTensor = model.GetOutputTensor(0); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 57 | if (!inputTensor->dims) { |
| 58 | printf_err("Invalid input tensor dims\n"); |
| 59 | return false; |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 60 | } else if (inputTensor->dims->size < 4) { |
| 61 | printf_err("Input tensor dimension should be = 4\n"); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 62 | return false; |
| 63 | } |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 64 | |
| 65 | /* Get input shape for displaying the image. */ |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 66 | TfLiteIntArray* inputShape = model.GetInputShape(0); |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 67 | const uint32_t nCols = inputShape->data[arm::app::VisualWakeWordModel::ms_inputColsIdx]; |
| 68 | const uint32_t nRows = inputShape->data[arm::app::VisualWakeWordModel::ms_inputRowsIdx]; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 69 | if (arm::app::VisualWakeWordModel::ms_inputChannelsIdx >= |
| 70 | static_cast<uint32_t>(inputShape->size)) { |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 71 | printf_err("Invalid channel index.\n"); |
| 72 | return false; |
| 73 | } |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 74 | |
| 75 | /* We expect RGB images to be provided. */ |
| 76 | const uint32_t displayChannels = 3; |
| 77 | |
| 78 | /* Set up pre and post-processing. */ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 79 | VisualWakeWordPreProcess preProcess = VisualWakeWordPreProcess(inputTensor); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 80 | |
| 81 | std::vector<ClassificationResult> results; |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 82 | VisualWakeWordPostProcess postProcess = |
| 83 | VisualWakeWordPostProcess(outputTensor, |
| 84 | ctx.Get<Classifier&>("classifier"), |
| 85 | ctx.Get<std::vector<std::string>&>("labels"), |
| 86 | results); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 87 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 88 | do { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 89 | hal_lcd_clear(COLOR_BLACK); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 90 | |
| 91 | /* Strings for presentation/logging. */ |
| 92 | std::string str_inf{"Running inference... "}; |
| 93 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 94 | const uint8_t* imgSrc = GetImgArray(ctx.Get<uint32_t>("imgIndex")); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 95 | if (nullptr == imgSrc) { |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 96 | printf_err("Failed to get image index %" PRIu32 " (max: %u)\n", |
| 97 | ctx.Get<uint32_t>("imgIndex"), |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 98 | NUMBER_OF_FILES - 1); |
| 99 | return false; |
| 100 | } |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 101 | |
| 102 | /* Display this image on the LCD. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 103 | hal_lcd_display_image(imgSrc, |
| 104 | nCols, |
| 105 | nRows, |
| 106 | displayChannels, |
| 107 | dataPsnImgStartX, |
| 108 | dataPsnImgStartY, |
| 109 | dataPsnImgDownscaleFactor); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 110 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 111 | /* Display message on the LCD - inference running. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 112 | hal_lcd_display_text( |
| 113 | str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 114 | |
| 115 | /* Run inference over this image. */ |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 116 | info("Running inference on image %" PRIu32 " => %s\n", |
| 117 | ctx.Get<uint32_t>("imgIndex"), |
| 118 | GetFilename(ctx.Get<uint32_t>("imgIndex"))); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 119 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 120 | const size_t imgSz = |
| 121 | inputTensor->bytes < IMAGE_DATA_SIZE ? inputTensor->bytes : IMAGE_DATA_SIZE; |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 122 | |
| 123 | /* Run the pre-processing, inference and post-processing. */ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 124 | if (!preProcess.DoPreProcess(imgSrc, imgSz)) { |
| 125 | printf_err("Pre-processing failed."); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 126 | return false; |
| 127 | } |
| 128 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 129 | if (!RunInference(model, profiler)) { |
| 130 | printf_err("Inference failed."); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 131 | return false; |
| 132 | } |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 133 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 134 | if (!postProcess.DoPostProcess()) { |
| 135 | printf_err("Post-processing failed."); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
| 139 | /* Erase. */ |
| 140 | str_inf = std::string(str_inf.size(), ' '); |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 141 | hal_lcd_display_text( |
| 142 | str_inf.c_str(), str_inf.size(), dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 143 | |
| 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 Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 151 | if (!PresentInferenceResult(results)) { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | |
| 155 | profiler.PrintProfilingResult(); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 156 | |
Kshitij Sisodia | 2ea4623 | 2022-12-19 16:37:33 +0000 | [diff] [blame] | 157 | IncrementAppCtxIfmIdx(ctx, "imgIndex"); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 158 | |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 159 | } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImgIdx); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 164 | } /* namespace app */ |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 165 | } /* namespace arm */ |