alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 1 | /* |
Richard Burton | f32a86a | 2022-11-15 11:46:11 +0000 | [diff] [blame^] | 2 | * SPDX-FileCopyrightText: Copyright 2021-2022 Arm Limited and/or its affiliates <open-source-office@arm.com> |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [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 | |
| 19 | #include "hal.h" |
| 20 | #include "InputFiles.hpp" |
| 21 | #include "AudioUtils.hpp" |
Richard Burton | ed35a6f | 2022-02-14 11:55:35 +0000 | [diff] [blame] | 22 | #include "ImageUtils.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 23 | #include "UseCaseCommonUtils.hpp" |
Kshitij Sisodia | 76a1580 | 2021-12-24 11:05:11 +0000 | [diff] [blame] | 24 | #include "MicroNetKwsModel.hpp" |
| 25 | #include "MicroNetKwsMfcc.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 26 | #include "Classifier.hpp" |
| 27 | #include "KwsResult.hpp" |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 28 | #include "Wav2LetterModel.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 29 | #include "Wav2LetterMfcc.hpp" |
| 30 | #include "Wav2LetterPreprocess.hpp" |
| 31 | #include "Wav2LetterPostprocess.hpp" |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 32 | #include "KwsProcessing.hpp" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 33 | #include "AsrResult.hpp" |
| 34 | #include "AsrClassifier.hpp" |
| 35 | #include "OutputDecode.hpp" |
alexander | 31ae9f0 | 2022-02-10 16:15:54 +0000 | [diff] [blame] | 36 | #include "log_macros.h" |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 37 | |
| 38 | |
| 39 | using KwsClassifier = arm::app::Classifier; |
| 40 | |
| 41 | namespace arm { |
| 42 | namespace app { |
| 43 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 44 | struct KWSOutput { |
| 45 | bool executionSuccess = false; |
| 46 | const int16_t* asrAudioStart = nullptr; |
| 47 | int32_t asrAudioSamples = 0; |
| 48 | }; |
| 49 | |
| 50 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 51 | * @brief Presents KWS inference results. |
| 52 | * @param[in] results Vector of KWS classification results to be displayed. |
| 53 | * @return true if successful, false otherwise. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 54 | **/ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 55 | static bool PresentInferenceResult(std::vector<kws::KwsResult>& results); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 56 | |
| 57 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 58 | * @brief Presents ASR inference results. |
| 59 | * @param[in] results Vector of ASR classification results to be displayed. |
| 60 | * @return true if successful, false otherwise. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 61 | **/ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 62 | static bool PresentInferenceResult(std::vector<asr::AsrResult>& results); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 63 | |
| 64 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 65 | * @brief Performs the KWS pipeline. |
| 66 | * @param[in,out] ctx pointer to the application context object |
| 67 | * @return struct containing pointer to audio data where ASR should begin |
| 68 | * and how much data to process. |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 69 | **/ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 70 | static KWSOutput doKws(ApplicationContext& ctx) |
| 71 | { |
| 72 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
| 73 | auto& kwsModel = ctx.Get<Model&>("kwsModel"); |
| 74 | const auto kwsMfccFrameLength = ctx.Get<int>("kwsFrameLength"); |
| 75 | const auto kwsMfccFrameStride = ctx.Get<int>("kwsFrameStride"); |
| 76 | const auto kwsScoreThreshold = ctx.Get<float>("kwsScoreThreshold"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 77 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 78 | auto currentIndex = ctx.Get<uint32_t>("clipIndex"); |
| 79 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 80 | constexpr uint32_t dataPsnTxtInfStartX = 20; |
| 81 | constexpr uint32_t dataPsnTxtInfStartY = 40; |
| 82 | |
| 83 | constexpr int minTensorDims = static_cast<int>( |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 84 | (MicroNetKwsModel::ms_inputRowsIdx > MicroNetKwsModel::ms_inputColsIdx)? |
| 85 | MicroNetKwsModel::ms_inputRowsIdx : MicroNetKwsModel::ms_inputColsIdx); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 86 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 87 | /* Output struct from doing KWS. */ |
| 88 | KWSOutput output {}; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 89 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 90 | if (!kwsModel.IsInited()) { |
| 91 | printf_err("KWS model has not been initialised\n"); |
| 92 | return output; |
| 93 | } |
| 94 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 95 | /* Get Input and Output tensors for pre/post processing. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 96 | TfLiteTensor* kwsInputTensor = kwsModel.GetInputTensor(0); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 97 | TfLiteTensor* kwsOutputTensor = kwsModel.GetOutputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 98 | if (!kwsInputTensor->dims) { |
| 99 | printf_err("Invalid input tensor dims\n"); |
| 100 | return output; |
| 101 | } else if (kwsInputTensor->dims->size < minTensorDims) { |
| 102 | printf_err("Input tensor dimension should be >= %d\n", minTensorDims); |
| 103 | return output; |
| 104 | } |
| 105 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 106 | /* Get input shape for feature extraction. */ |
| 107 | TfLiteIntArray* inputShape = kwsModel.GetInputShape(0); |
| 108 | const uint32_t numMfccFeatures = inputShape->data[MicroNetKwsModel::ms_inputColsIdx]; |
| 109 | const uint32_t numMfccFrames = inputShape->data[MicroNetKwsModel::ms_inputRowsIdx]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 110 | |
| 111 | /* We expect to be sampling 1 second worth of data at a time |
| 112 | * NOTE: This is only used for time stamp calculation. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 113 | const float kwsAudioParamsSecondsPerSample = 1.0 / audio::MicroNetKwsMFCC::ms_defaultSamplingFreq; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 114 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 115 | /* Set up pre and post-processing. */ |
| 116 | KwsPreProcess preProcess = KwsPreProcess(kwsInputTensor, numMfccFeatures, numMfccFrames, |
| 117 | kwsMfccFrameLength, kwsMfccFrameStride); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 118 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 119 | std::vector<ClassificationResult> singleInfResult; |
| 120 | KwsPostProcess postProcess = KwsPostProcess(kwsOutputTensor, ctx.Get<KwsClassifier &>("kwsClassifier"), |
| 121 | ctx.Get<std::vector<std::string>&>("kwsLabels"), |
| 122 | singleInfResult); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 123 | |
| 124 | /* Creating a sliding window through the whole audio clip. */ |
| 125 | auto audioDataSlider = audio::SlidingWindow<const int16_t>( |
| 126 | get_audio_array(currentIndex), |
| 127 | get_audio_array_size(currentIndex), |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 128 | preProcess.m_audioDataWindowSize, preProcess.m_audioDataStride); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 129 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 130 | /* Declare a container to hold kws results from across the whole audio clip. */ |
| 131 | std::vector<kws::KwsResult> finalResults; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 132 | |
| 133 | /* Display message on the LCD - inference running. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 134 | std::string str_inf{"Running KWS inference... "}; |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 135 | hal_lcd_display_text( |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 136 | str_inf.c_str(), str_inf.size(), |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 137 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 138 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 139 | info("Running KWS inference on audio clip %" PRIu32 " => %s\n", |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 140 | currentIndex, get_filename(currentIndex)); |
| 141 | |
| 142 | /* Start sliding through audio clip. */ |
| 143 | while (audioDataSlider.HasNext()) { |
| 144 | const int16_t* inferenceWindow = audioDataSlider.Next(); |
| 145 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 146 | /* Run the pre-processing, inference and post-processing. */ |
Richard Burton | ec5e99b | 2022-10-05 11:00:37 +0100 | [diff] [blame] | 147 | if (!preProcess.DoPreProcess(inferenceWindow, audioDataSlider.Index())) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 148 | printf_err("KWS Pre-processing failed."); |
| 149 | return output; |
| 150 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 151 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 152 | if (!RunInference(kwsModel, profiler)) { |
| 153 | printf_err("KWS Inference failed."); |
| 154 | return output; |
| 155 | } |
| 156 | |
| 157 | if (!postProcess.DoPostProcess()) { |
| 158 | printf_err("KWS Post-processing failed."); |
| 159 | return output; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | info("Inference %zu/%zu\n", audioDataSlider.Index() + 1, |
| 163 | audioDataSlider.TotalStrides() + 1); |
| 164 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 165 | /* Add results from this window to our final results vector. */ |
| 166 | finalResults.emplace_back( |
| 167 | kws::KwsResult(singleInfResult, |
| 168 | audioDataSlider.Index() * kwsAudioParamsSecondsPerSample * preProcess.m_audioDataStride, |
| 169 | audioDataSlider.Index(), kwsScoreThreshold)); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 170 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 171 | /* Break out when trigger keyword is detected. */ |
| 172 | if (singleInfResult[0].m_label == ctx.Get<const std::string&>("triggerKeyword") |
| 173 | && singleInfResult[0].m_normalisedVal > kwsScoreThreshold) { |
| 174 | output.asrAudioStart = inferenceWindow + preProcess.m_audioDataWindowSize; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 175 | output.asrAudioSamples = get_audio_array_size(currentIndex) - |
| 176 | (audioDataSlider.NextWindowStartIndex() - |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 177 | preProcess.m_audioDataStride + preProcess.m_audioDataWindowSize); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | |
| 181 | #if VERIFY_TEST_OUTPUT |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 182 | DumpTensor(kwsOutputTensor); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 183 | #endif /* VERIFY_TEST_OUTPUT */ |
| 184 | |
| 185 | } /* while (audioDataSlider.HasNext()) */ |
| 186 | |
| 187 | /* Erase. */ |
| 188 | str_inf = std::string(str_inf.size(), ' '); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 189 | hal_lcd_display_text(str_inf.c_str(), str_inf.size(), |
| 190 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 191 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 192 | if (!PresentInferenceResult(finalResults)) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 193 | return output; |
| 194 | } |
| 195 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 196 | profiler.PrintProfilingResult(); |
| 197 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 198 | output.executionSuccess = true; |
| 199 | return output; |
| 200 | } |
| 201 | |
| 202 | /** |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 203 | * @brief Performs the ASR pipeline. |
| 204 | * @param[in,out] ctx Pointer to the application context object. |
| 205 | * @param[in] kwsOutput Struct containing pointer to audio data where ASR should begin |
| 206 | * and how much data to process. |
| 207 | * @return true if pipeline executed without failure. |
| 208 | **/ |
| 209 | static bool doAsr(ApplicationContext& ctx, const KWSOutput& kwsOutput) |
| 210 | { |
| 211 | auto& asrModel = ctx.Get<Model&>("asrModel"); |
| 212 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
| 213 | auto asrMfccFrameLen = ctx.Get<uint32_t>("asrFrameLength"); |
| 214 | auto asrMfccFrameStride = ctx.Get<uint32_t>("asrFrameStride"); |
| 215 | auto asrScoreThreshold = ctx.Get<float>("asrScoreThreshold"); |
| 216 | auto asrInputCtxLen = ctx.Get<uint32_t>("ctxLen"); |
| 217 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 218 | constexpr uint32_t dataPsnTxtInfStartX = 20; |
| 219 | constexpr uint32_t dataPsnTxtInfStartY = 40; |
| 220 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 221 | if (!asrModel.IsInited()) { |
| 222 | printf_err("ASR model has not been initialised\n"); |
| 223 | return false; |
| 224 | } |
| 225 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 226 | hal_lcd_clear(COLOR_BLACK); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 227 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 228 | /* Get Input and Output tensors for pre/post processing. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 229 | TfLiteTensor* asrInputTensor = asrModel.GetInputTensor(0); |
| 230 | TfLiteTensor* asrOutputTensor = asrModel.GetOutputTensor(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 231 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 232 | /* Get input shape. Dimensions of the tensor should have been verified by |
| 233 | * the callee. */ |
| 234 | TfLiteIntArray* inputShape = asrModel.GetInputShape(0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 235 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 236 | |
| 237 | const uint32_t asrInputRows = asrInputTensor->dims->data[Wav2LetterModel::ms_inputRowsIdx]; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 238 | const uint32_t asrInputInnerLen = asrInputRows - (2 * asrInputCtxLen); |
| 239 | |
| 240 | /* Make sure the input tensor supports the above context and inner lengths. */ |
| 241 | if (asrInputRows <= 2 * asrInputCtxLen || asrInputRows <= asrInputInnerLen) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 242 | printf_err("ASR input rows not compatible with ctx length %" PRIu32 "\n", |
| 243 | asrInputCtxLen); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | |
| 247 | /* Audio data stride corresponds to inputInnerLen feature vectors. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 248 | const uint32_t asrAudioDataWindowLen = (asrInputRows - 1) * asrMfccFrameStride + (asrMfccFrameLen); |
| 249 | const uint32_t asrAudioDataWindowStride = asrInputInnerLen * asrMfccFrameStride; |
| 250 | const float asrAudioParamsSecondsPerSample = 1.0 / audio::Wav2LetterMFCC::ms_defaultSamplingFreq; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 251 | |
| 252 | /* Get the remaining audio buffer and respective size from KWS results. */ |
| 253 | const int16_t* audioArr = kwsOutput.asrAudioStart; |
| 254 | const uint32_t audioArrSize = kwsOutput.asrAudioSamples; |
| 255 | |
| 256 | /* Audio clip must have enough samples to produce 1 MFCC feature. */ |
| 257 | std::vector<int16_t> audioBuffer = std::vector<int16_t>(audioArr, audioArr + audioArrSize); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 258 | if (audioArrSize < asrMfccFrameLen) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 259 | printf_err("Not enough audio samples, minimum needed is %" PRIu32 "\n", |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 260 | asrMfccFrameLen); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
| 264 | /* Initialise an audio slider. */ |
alexander | 80eecfb | 2021-07-06 19:47:59 +0100 | [diff] [blame] | 265 | auto audioDataSlider = audio::FractionalSlidingWindow<const int16_t>( |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 266 | audioBuffer.data(), |
| 267 | audioBuffer.size(), |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 268 | asrAudioDataWindowLen, |
| 269 | asrAudioDataWindowStride); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 270 | |
| 271 | /* Declare a container for results. */ |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 272 | std::vector<asr::AsrResult> asrResults; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 273 | |
| 274 | /* Display message on the LCD - inference running. */ |
| 275 | std::string str_inf{"Running ASR inference... "}; |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 276 | hal_lcd_display_text(str_inf.c_str(), str_inf.size(), |
alexander | c350cdc | 2021-04-29 20:36:09 +0100 | [diff] [blame] | 277 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 278 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 279 | size_t asrInferenceWindowLen = asrAudioDataWindowLen; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 280 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 281 | /* Set up pre and post-processing objects. */ |
| 282 | AsrPreProcess asrPreProcess = AsrPreProcess(asrInputTensor, arm::app::Wav2LetterModel::ms_numMfccFeatures, |
| 283 | inputShape->data[Wav2LetterModel::ms_inputRowsIdx], |
| 284 | asrMfccFrameLen, asrMfccFrameStride); |
| 285 | |
| 286 | std::vector<ClassificationResult> singleInfResult; |
| 287 | const uint32_t outputCtxLen = AsrPostProcess::GetOutputContextLen(asrModel, asrInputCtxLen); |
| 288 | AsrPostProcess asrPostProcess = AsrPostProcess( |
| 289 | asrOutputTensor, ctx.Get<AsrClassifier&>("asrClassifier"), |
| 290 | ctx.Get<std::vector<std::string>&>("asrLabels"), |
| 291 | singleInfResult, outputCtxLen, |
| 292 | Wav2LetterModel::ms_blankTokenIdx, Wav2LetterModel::ms_outputRowsIdx |
| 293 | ); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 294 | /* Start sliding through audio clip. */ |
| 295 | while (audioDataSlider.HasNext()) { |
| 296 | |
| 297 | /* If not enough audio see how much can be sent for processing. */ |
| 298 | size_t nextStartIndex = audioDataSlider.NextWindowStartIndex(); |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 299 | if (nextStartIndex + asrAudioDataWindowLen > audioBuffer.size()) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 300 | asrInferenceWindowLen = audioBuffer.size() - nextStartIndex; |
| 301 | } |
| 302 | |
| 303 | const int16_t* asrInferenceWindow = audioDataSlider.Next(); |
| 304 | |
| 305 | info("Inference %zu/%zu\n", audioDataSlider.Index() + 1, |
| 306 | static_cast<size_t>(ceilf(audioDataSlider.FractionalTotalStrides() + 1))); |
| 307 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 308 | /* Run the pre-processing, inference and post-processing. */ |
| 309 | if (!asrPreProcess.DoPreProcess(asrInferenceWindow, asrInferenceWindowLen)) { |
| 310 | printf_err("ASR pre-processing failed."); |
| 311 | return false; |
| 312 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 313 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 314 | /* Run inference over this audio clip sliding window. */ |
alexander | 27b62d9 | 2021-05-04 20:46:08 +0100 | [diff] [blame] | 315 | if (!RunInference(asrModel, profiler)) { |
| 316 | printf_err("ASR inference failed\n"); |
| 317 | return false; |
| 318 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 319 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 320 | /* Post processing needs to know if we are on the last audio window. */ |
| 321 | asrPostProcess.m_lastIteration = !audioDataSlider.HasNext(); |
| 322 | if (!asrPostProcess.DoPostProcess()) { |
| 323 | printf_err("ASR post-processing failed."); |
| 324 | return false; |
| 325 | } |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 326 | |
| 327 | /* Get results. */ |
| 328 | std::vector<ClassificationResult> asrClassificationResult; |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 329 | auto& asrClassifier = ctx.Get<AsrClassifier&>("asrClassifier"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 330 | asrClassifier.GetClassificationResults( |
| 331 | asrOutputTensor, asrClassificationResult, |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 332 | ctx.Get<std::vector<std::string>&>("asrLabels"), 1); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 333 | |
| 334 | asrResults.emplace_back(asr::AsrResult(asrClassificationResult, |
| 335 | (audioDataSlider.Index() * |
| 336 | asrAudioParamsSecondsPerSample * |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 337 | asrAudioDataWindowStride), |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 338 | audioDataSlider.Index(), asrScoreThreshold)); |
| 339 | |
| 340 | #if VERIFY_TEST_OUTPUT |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 341 | armDumpTensor(asrOutputTensor, asrOutputTensor->dims->data[Wav2LetterModel::ms_outputColsIdx]); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 342 | #endif /* VERIFY_TEST_OUTPUT */ |
| 343 | |
| 344 | /* Erase */ |
| 345 | str_inf = std::string(str_inf.size(), ' '); |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 346 | hal_lcd_display_text( |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 347 | str_inf.c_str(), str_inf.size(), |
| 348 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, false); |
| 349 | } |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 350 | if (!PresentInferenceResult(asrResults)) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | |
Isabella Gottardi | 8df12f3 | 2021-04-07 17:15:31 +0100 | [diff] [blame] | 354 | profiler.PrintProfilingResult(); |
| 355 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 356 | return true; |
| 357 | } |
| 358 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 359 | /* KWS and ASR inference handler. */ |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 360 | bool ClassifyAudioHandler(ApplicationContext& ctx, uint32_t clipIndex, bool runAll) |
| 361 | { |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 362 | hal_lcd_clear(COLOR_BLACK); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 363 | |
| 364 | /* If the request has a valid size, set the audio index. */ |
| 365 | if (clipIndex < NUMBER_OF_FILES) { |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 366 | if (!SetAppCtxIfmIdx(ctx, clipIndex,"kws_asr")) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 367 | return false; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | auto startClipIdx = ctx.Get<uint32_t>("clipIndex"); |
| 372 | |
| 373 | do { |
| 374 | KWSOutput kwsOutput = doKws(ctx); |
| 375 | if (!kwsOutput.executionSuccess) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 376 | printf_err("KWS failed\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 377 | return false; |
| 378 | } |
| 379 | |
| 380 | if (kwsOutput.asrAudioStart != nullptr && kwsOutput.asrAudioSamples > 0) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 381 | info("Trigger keyword spotted\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 382 | if(!doAsr(ctx, kwsOutput)) { |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 383 | printf_err("ASR failed\n"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 384 | return false; |
| 385 | } |
| 386 | } |
| 387 | |
Éanna Ó Catháin | 8f95887 | 2021-09-15 09:32:30 +0100 | [diff] [blame] | 388 | IncrementAppCtxIfmIdx(ctx,"kws_asr"); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 389 | |
| 390 | } while (runAll && ctx.Get<uint32_t>("clipIndex") != startClipIdx); |
| 391 | |
| 392 | return true; |
| 393 | } |
| 394 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 395 | static bool PresentInferenceResult(std::vector<arm::app::kws::KwsResult>& results) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 396 | { |
| 397 | constexpr uint32_t dataPsnTxtStartX1 = 20; |
| 398 | constexpr uint32_t dataPsnTxtStartY1 = 30; |
| 399 | constexpr uint32_t dataPsnTxtYIncr = 16; /* Row index increment. */ |
| 400 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 401 | hal_lcd_set_text_color(COLOR_GREEN); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 402 | |
| 403 | /* Display each result. */ |
| 404 | uint32_t rowIdx1 = dataPsnTxtStartY1 + 2 * dataPsnTxtYIncr; |
| 405 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 406 | for (auto & result : results) { |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 407 | std::string topKeyword{"<none>"}; |
| 408 | float score = 0.f; |
| 409 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 410 | if (!result.m_resultVec.empty()) { |
| 411 | topKeyword = result.m_resultVec[0].m_label; |
| 412 | score = result.m_resultVec[0].m_normalisedVal; |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | std::string resultStr = |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 416 | std::string{"@"} + std::to_string(result.m_timeStamp) + |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 417 | std::string{"s: "} + topKeyword + std::string{" ("} + |
| 418 | std::to_string(static_cast<int>(score * 100)) + std::string{"%)"}; |
| 419 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 420 | hal_lcd_display_text(resultStr.c_str(), resultStr.size(), |
| 421 | dataPsnTxtStartX1, rowIdx1, 0); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 422 | rowIdx1 += dataPsnTxtYIncr; |
| 423 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 424 | info("For timestamp: %f (inference #: %" PRIu32 "); threshold: %f\n", |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 425 | result.m_timeStamp, result.m_inferenceNumber, |
| 426 | result.m_threshold); |
| 427 | for (uint32_t j = 0; j < result.m_resultVec.size(); ++j) { |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 428 | info("\t\tlabel @ %" PRIu32 ": %s, score: %f\n", j, |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 429 | result.m_resultVec[j].m_label.c_str(), |
| 430 | result.m_resultVec[j].m_normalisedVal); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | return true; |
| 435 | } |
| 436 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 437 | static bool PresentInferenceResult(std::vector<arm::app::asr::AsrResult>& results) |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 438 | { |
| 439 | constexpr uint32_t dataPsnTxtStartX1 = 20; |
| 440 | constexpr uint32_t dataPsnTxtStartY1 = 80; |
| 441 | constexpr bool allow_multiple_lines = true; |
| 442 | |
Kshitij Sisodia | 68fdd11 | 2022-04-06 13:03:20 +0100 | [diff] [blame] | 443 | hal_lcd_set_text_color(COLOR_GREEN); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 444 | |
| 445 | /* Results from multiple inferences should be combined before processing. */ |
| 446 | std::vector<arm::app::ClassificationResult> combinedResults; |
| 447 | for (auto& result : results) { |
| 448 | combinedResults.insert(combinedResults.end(), |
| 449 | result.m_resultVec.begin(), |
| 450 | result.m_resultVec.end()); |
| 451 | } |
| 452 | |
| 453 | for (auto& result : results) { |
| 454 | /* Get the final result string using the decoder. */ |
| 455 | std::string infResultStr = audio::asr::DecodeOutput(result.m_resultVec); |
| 456 | |
Kshitij Sisodia | f9c19ea | 2021-05-07 16:08:14 +0100 | [diff] [blame] | 457 | info("Result for inf %" PRIu32 ": %s\n", result.m_inferenceNumber, |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 458 | infResultStr.c_str()); |
| 459 | } |
| 460 | |
| 461 | std::string finalResultStr = audio::asr::DecodeOutput(combinedResults); |
| 462 | |
Richard Burton | 4e00279 | 2022-05-04 09:45:02 +0100 | [diff] [blame] | 463 | hal_lcd_display_text(finalResultStr.c_str(), finalResultStr.size(), |
| 464 | dataPsnTxtStartX1, dataPsnTxtStartY1, allow_multiple_lines); |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 465 | |
| 466 | info("Final result: %s\n", finalResultStr.c_str()); |
| 467 | return true; |
| 468 | } |
| 469 | |
alexander | 3c79893 | 2021-03-26 21:42:19 +0000 | [diff] [blame] | 470 | } /* namespace app */ |
Kshitij Sisodia | aa4bcb1 | 2022-05-06 09:13:03 +0100 | [diff] [blame] | 471 | } /* namespace arm */ |