Richard Burton | c20be97 | 2022-04-19 17:01:08 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2022 Arm Limited. All rights reserved. |
| 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 "VisualWakeWordProcessing.hpp" |
| 18 | #include "ImageUtils.hpp" |
| 19 | #include "VisualWakeWordModel.hpp" |
| 20 | #include "log_macros.h" |
| 21 | |
| 22 | namespace arm { |
| 23 | namespace app { |
| 24 | |
| 25 | VisualWakeWordPreProcess::VisualWakeWordPreProcess(Model* model) |
| 26 | { |
| 27 | if (!model->IsInited()) { |
| 28 | printf_err("Model is not initialised!.\n"); |
| 29 | } |
| 30 | this->m_model = model; |
| 31 | } |
| 32 | |
| 33 | bool VisualWakeWordPreProcess::DoPreProcess(const void* data, size_t inputSize) |
| 34 | { |
| 35 | if (data == nullptr) { |
| 36 | printf_err("Data pointer is null"); |
| 37 | } |
| 38 | |
| 39 | auto input = static_cast<const uint8_t*>(data); |
| 40 | TfLiteTensor* inputTensor = this->m_model->GetInputTensor(0); |
| 41 | |
| 42 | auto unsignedDstPtr = static_cast<uint8_t*>(inputTensor->data.data); |
| 43 | |
| 44 | /* VWW model has one channel input => Convert image to grayscale here. |
| 45 | * We expect images to always be RGB. */ |
| 46 | image::RgbToGrayscale(input, unsignedDstPtr, inputSize); |
| 47 | |
| 48 | /* VWW model pre-processing is image conversion from uint8 to [0,1] float values, |
| 49 | * then quantize them with input quantization info. */ |
| 50 | QuantParams inQuantParams = GetTensorQuantParams(inputTensor); |
| 51 | |
| 52 | auto signedDstPtr = static_cast<int8_t*>(inputTensor->data.data); |
| 53 | for (size_t i = 0; i < inputTensor->bytes; i++) { |
| 54 | auto i_data_int8 = static_cast<int8_t>( |
| 55 | ((static_cast<float>(unsignedDstPtr[i]) / 255.0f) / inQuantParams.scale) + inQuantParams.offset |
| 56 | ); |
| 57 | signedDstPtr[i] = std::min<int8_t>(INT8_MAX, std::max<int8_t>(i_data_int8, INT8_MIN)); |
| 58 | } |
| 59 | |
| 60 | debug("Input tensor populated \n"); |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | VisualWakeWordPostProcess::VisualWakeWordPostProcess(Classifier& classifier, Model* model, |
| 66 | const std::vector<std::string>& labels, std::vector<ClassificationResult>& results) |
| 67 | :m_vwwClassifier{classifier}, |
| 68 | m_labels{labels}, |
| 69 | m_results{results} |
| 70 | { |
| 71 | if (!model->IsInited()) { |
| 72 | printf_err("Model is not initialised!.\n"); |
| 73 | } |
| 74 | this->m_model = model; |
| 75 | } |
| 76 | |
| 77 | bool VisualWakeWordPostProcess::DoPostProcess() |
| 78 | { |
| 79 | return this->m_vwwClassifier.GetClassificationResults( |
| 80 | this->m_model->GetOutputTensor(0), this->m_results, |
| 81 | this->m_labels, 1, true); |
| 82 | } |
| 83 | |
| 84 | } /* namespace app */ |
| 85 | } /* namespace arm */ |