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