Richard Burton | 11b75cc | 2022-04-07 18:00:55 +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 "ImgClassProcessing.hpp" |
| 18 | #include "ImageUtils.hpp" |
| 19 | #include "log_macros.h" |
| 20 | |
| 21 | namespace arm { |
| 22 | namespace app { |
| 23 | |
| 24 | ImgClassPreProcess::ImgClassPreProcess(Model* model) |
| 25 | { |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 26 | if (!model->IsInited()) { |
| 27 | printf_err("Model is not initialised!.\n"); |
| 28 | } |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 29 | this->m_model = model; |
| 30 | } |
| 31 | |
| 32 | bool ImgClassPreProcess::DoPreProcess(const void* data, size_t inputSize) |
| 33 | { |
| 34 | if (data == nullptr) { |
| 35 | printf_err("Data pointer is null"); |
| 36 | } |
| 37 | |
| 38 | auto input = static_cast<const uint8_t*>(data); |
| 39 | TfLiteTensor* inputTensor = this->m_model->GetInputTensor(0); |
| 40 | |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 41 | std::memcpy(inputTensor->data.data, input, inputSize); |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 42 | debug("Input tensor populated \n"); |
| 43 | |
| 44 | if (this->m_model->IsDataSigned()) { |
| 45 | image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes); |
| 46 | } |
| 47 | |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | ImgClassPostProcess::ImgClassPostProcess(Classifier& classifier, Model* model, |
| 52 | const std::vector<std::string>& labels, |
| 53 | std::vector<ClassificationResult>& results) |
| 54 | :m_imgClassifier{classifier}, |
| 55 | m_labels{labels}, |
| 56 | m_results{results} |
| 57 | { |
Richard Burton | e6398cd | 2022-04-13 11:58:28 +0100 | [diff] [blame] | 58 | if (!model->IsInited()) { |
| 59 | printf_err("Model is not initialised!.\n"); |
| 60 | } |
Richard Burton | 11b75cc | 2022-04-07 18:00:55 +0100 | [diff] [blame] | 61 | this->m_model = model; |
| 62 | } |
| 63 | |
| 64 | bool ImgClassPostProcess::DoPostProcess() |
| 65 | { |
| 66 | return this->m_imgClassifier.GetClassificationResults( |
| 67 | this->m_model->GetOutputTensor(0), this->m_results, |
| 68 | this->m_labels, 5, false); |
| 69 | } |
| 70 | |
| 71 | } /* namespace app */ |
| 72 | } /* namespace arm */ |