blob: adf9794eeb5283b7a0b72ee2d93f6019b21ad664 [file] [log] [blame]
Richard Burton11b75cc2022-04-07 18:00:55 +01001/*
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
21namespace arm {
22namespace app {
23
Richard Burtonb40ecf82022-04-22 16:14:57 +010024 ImgClassPreProcess::ImgClassPreProcess(TfLiteTensor* inputTensor, bool convertToInt8)
25 :m_inputTensor{inputTensor},
26 m_convertToInt8{convertToInt8}
27 {}
Richard Burton11b75cc2022-04-07 18:00:55 +010028
29 bool ImgClassPreProcess::DoPreProcess(const void* data, size_t inputSize)
30 {
31 if (data == nullptr) {
32 printf_err("Data pointer is null");
Richard Burtonb40ecf82022-04-22 16:14:57 +010033 return false;
Richard Burton11b75cc2022-04-07 18:00:55 +010034 }
35
36 auto input = static_cast<const uint8_t*>(data);
Richard Burton11b75cc2022-04-07 18:00:55 +010037
Richard Burtonb40ecf82022-04-22 16:14:57 +010038 std::memcpy(this->m_inputTensor->data.data, input, inputSize);
Richard Burton11b75cc2022-04-07 18:00:55 +010039 debug("Input tensor populated \n");
40
Richard Burtonb40ecf82022-04-22 16:14:57 +010041 if (this->m_convertToInt8) {
42 image::ConvertImgToInt8(this->m_inputTensor->data.data, this->m_inputTensor->bytes);
Richard Burton11b75cc2022-04-07 18:00:55 +010043 }
44
45 return true;
46 }
47
Richard Burtonb40ecf82022-04-22 16:14:57 +010048 ImgClassPostProcess::ImgClassPostProcess(TfLiteTensor* outputTensor, Classifier& classifier,
Richard Burton11b75cc2022-04-07 18:00:55 +010049 const std::vector<std::string>& labels,
50 std::vector<ClassificationResult>& results)
Richard Burtonb40ecf82022-04-22 16:14:57 +010051 :m_outputTensor{outputTensor},
52 m_imgClassifier{classifier},
Richard Burton11b75cc2022-04-07 18:00:55 +010053 m_labels{labels},
54 m_results{results}
Richard Burtonb40ecf82022-04-22 16:14:57 +010055 {}
Richard Burton11b75cc2022-04-07 18:00:55 +010056
57 bool ImgClassPostProcess::DoPostProcess()
58 {
59 return this->m_imgClassifier.GetClassificationResults(
Richard Burtonb40ecf82022-04-22 16:14:57 +010060 this->m_outputTensor, this->m_results,
Richard Burton11b75cc2022-04-07 18:00:55 +010061 this->m_labels, 5, false);
62 }
63
64} /* namespace app */
65} /* namespace arm */