blob: 0724320bfffe86411a9e24d9f877dc38b6f8e66c [file] [log] [blame]
Richard Burton11b75cc2022-04-07 18:00:55 +01001/*
Richard Burtonf32a86a2022-11-15 11:46:11 +00002 * SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
Richard Burton11b75cc2022-04-07 18:00:55 +01003 * 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 Sisodiaaa4bcb12022-05-06 09:13:03 +010018
Richard Burton11b75cc2022-04-07 18:00:55 +010019#include "ImageUtils.hpp"
20#include "log_macros.h"
21
22namespace arm {
23namespace app {
24
Richard Burtonb40ecf82022-04-22 16:14:57 +010025 ImgClassPreProcess::ImgClassPreProcess(TfLiteTensor* inputTensor, bool convertToInt8)
26 :m_inputTensor{inputTensor},
27 m_convertToInt8{convertToInt8}
28 {}
Richard Burton11b75cc2022-04-07 18:00:55 +010029
30 bool ImgClassPreProcess::DoPreProcess(const void* data, size_t inputSize)
31 {
32 if (data == nullptr) {
33 printf_err("Data pointer is null");
Richard Burtonb40ecf82022-04-22 16:14:57 +010034 return false;
Richard Burton11b75cc2022-04-07 18:00:55 +010035 }
36
37 auto input = static_cast<const uint8_t*>(data);
Richard Burton11b75cc2022-04-07 18:00:55 +010038
Richard Burtonb40ecf82022-04-22 16:14:57 +010039 std::memcpy(this->m_inputTensor->data.data, input, inputSize);
Richard Burton11b75cc2022-04-07 18:00:55 +010040 debug("Input tensor populated \n");
41
Richard Burtonb40ecf82022-04-22 16:14:57 +010042 if (this->m_convertToInt8) {
43 image::ConvertImgToInt8(this->m_inputTensor->data.data, this->m_inputTensor->bytes);
Richard Burton11b75cc2022-04-07 18:00:55 +010044 }
45
46 return true;
47 }
48
Richard Burtonb40ecf82022-04-22 16:14:57 +010049 ImgClassPostProcess::ImgClassPostProcess(TfLiteTensor* outputTensor, Classifier& classifier,
Richard Burton11b75cc2022-04-07 18:00:55 +010050 const std::vector<std::string>& labels,
51 std::vector<ClassificationResult>& results)
Richard Burtonb40ecf82022-04-22 16:14:57 +010052 :m_outputTensor{outputTensor},
53 m_imgClassifier{classifier},
Richard Burton11b75cc2022-04-07 18:00:55 +010054 m_labels{labels},
55 m_results{results}
Richard Burtonb40ecf82022-04-22 16:14:57 +010056 {}
Richard Burton11b75cc2022-04-07 18:00:55 +010057
58 bool ImgClassPostProcess::DoPostProcess()
59 {
60 return this->m_imgClassifier.GetClassificationResults(
Richard Burtonb40ecf82022-04-22 16:14:57 +010061 this->m_outputTensor, this->m_results,
Richard Burton11b75cc2022-04-07 18:00:55 +010062 this->m_labels, 5, false);
63 }
64
65} /* namespace app */
66} /* namespace arm */