Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 1 | /* |
Liam Barry | e958850 | 2022-01-25 14:31:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Arm Limited. All rights reserved. |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 3 | * 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 Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 21 | #include "ImageUtils.hpp" |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 22 | #include "UseCaseCommonUtils.hpp" |
| 23 | #include "hal.h" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 24 | #include "log_macros.h" |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 25 | #include "VisualWakeWordProcessing.hpp" |
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. */ |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 31 | bool ClassifyImageHandler(ApplicationContext &ctx, uint32_t imgIndex, bool runAll) |
| 32 | { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 33 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 34 | 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áin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 42 | |
| 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á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 | |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [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]; |
| 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 Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 73 | |
| 74 | /* We expect RGB images to be provided. */ |
| 75 | const uint32_t displayChannels = 3; |
| 76 | |
| 77 | /* Set up pre and post-processing. */ |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 78 | VisualWakeWordPreProcess preProcess = VisualWakeWordPreProcess(inputTensor); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 79 | |
| 80 | std::vector<ClassificationResult> results; |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 81 | VisualWakeWordPostProcess postProcess = VisualWakeWordPostProcess(outputTensor, |
| 82 | ctx.Get<Classifier&>("classifier"), |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 83 | ctx.Get<std::vector<std::string>&>("labels"), results); |
| 84 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 85 | do { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 86 | hal_lcd_clear(COLOR_BLACK); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 87 | |
| 88 | /* Strings for presentation/logging. */ |
| 89 | std::string str_inf{"Running inference... "}; |
| 90 | |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 91 | 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áin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 97 | |
| 98 | /* Display this image on the LCD. */ |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 99 | hal_lcd_display_image( |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 100 | imgSrc, |
| 101 | nCols, nRows, displayChannels, |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 102 | dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor); |
| 103 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 104 | /* Display message on the LCD - inference running. */ |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 105 | hal_lcd_display_text(str_inf.c_str(), str_inf.size(), |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 106 | 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 Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 112 | 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 Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 116 | if (!preProcess.DoPreProcess(imgSrc, imgSz)) { |
| 117 | printf_err("Pre-processing failed."); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 118 | return false; |
| 119 | } |
| 120 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 121 | if (!RunInference(model, profiler)) { |
| 122 | printf_err("Inference failed."); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 123 | return false; |
| 124 | } |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 125 | |
Richard Burton | b40ecf8 | 2022-04-22 16:14:57 +0100 | [diff] [blame] | 126 | if (!postProcess.DoPostProcess()) { |
| 127 | printf_err("Post-processing failed."); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 128 | return false; |
| 129 | } |
| 130 | |
| 131 | /* Erase. */ |
| 132 | str_inf = std::string(str_inf.size(), ' '); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 133 | hal_lcd_display_text(str_inf.c_str(), str_inf.size(), |
| 134 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 135 | |
| 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 Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 143 | if (!PresentInferenceResult(results)) { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 144 | return false; |
| 145 | } |
| 146 | |
| 147 | profiler.PrintProfilingResult(); |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 148 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 149 | IncrementAppCtxIfmIdx(ctx,"imgIndex"); |
| 150 | |
Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame] | 151 | } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImgIdx); |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 156 | } /* namespace app */ |
Isabella Gottardi | 3107aa2 | 2022-01-27 16:39:37 +0000 | [diff] [blame] | 157 | } /* namespace arm */ |