blob: e931b7d1ada79027abf18f15a7fb24cd851e8a48 [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#ifndef IMG_CLASS_PROCESSING_HPP
18#define IMG_CLASS_PROCESSING_HPP
19
20#include "BaseProcessing.hpp"
21#include "Model.hpp"
22#include "Classifier.hpp"
23
24namespace arm {
25namespace app {
26
27 /**
28 * @brief Pre-processing class for Image Classification use case.
29 * Implements methods declared by BasePreProcess and anything else needed
30 * to populate input tensors ready for inference.
31 */
32 class ImgClassPreProcess : public BasePreProcess {
33
34 public:
Richard Burtone6398cd2022-04-13 11:58:28 +010035 /**
36 * @brief Constructor
Richard Burtonb40ecf82022-04-22 16:14:57 +010037 * @param[in] inputTensor Pointer to the TFLite Micro input Tensor.
38 * @param[in] convertToInt8 Should the image be converted to Int8 range.
Richard Burtone6398cd2022-04-13 11:58:28 +010039 **/
Richard Burtonb40ecf82022-04-22 16:14:57 +010040 explicit ImgClassPreProcess(TfLiteTensor* inputTensor, bool convertToInt8);
Richard Burton11b75cc2022-04-07 18:00:55 +010041
Richard Burtone6398cd2022-04-13 11:58:28 +010042 /**
43 * @brief Should perform pre-processing of 'raw' input image data and load it into
44 * TFLite Micro input tensors ready for inference
45 * @param[in] input Pointer to the data that pre-processing will work on.
46 * @param[in] inputSize Size of the input data.
47 * @return true if successful, false otherwise.
48 **/
Richard Burton11b75cc2022-04-07 18:00:55 +010049 bool DoPreProcess(const void* input, size_t inputSize) override;
Richard Burtonb40ecf82022-04-22 16:14:57 +010050
51 private:
52 TfLiteTensor* m_inputTensor;
53 bool m_convertToInt8;
Richard Burton11b75cc2022-04-07 18:00:55 +010054 };
55
56 /**
57 * @brief Post-processing class for Image Classification use case.
58 * Implements methods declared by BasePostProcess and anything else needed
59 * to populate result vector.
60 */
61 class ImgClassPostProcess : public BasePostProcess {
62
Richard Burton11b75cc2022-04-07 18:00:55 +010063 public:
Richard Burtone6398cd2022-04-13 11:58:28 +010064 /**
65 * @brief Constructor
Richard Burtonb40ecf82022-04-22 16:14:57 +010066 * @param[in] outputTensor Pointer to the TFLite Micro output Tensor.
67 * @param[in] classifier Classifier object used to get top N results from classification.
68 * @param[in] labels Vector of string labels to identify each output of the model.
69 * @param[in] results Vector of classification results to store decoded outputs.
Richard Burtone6398cd2022-04-13 11:58:28 +010070 **/
Richard Burtonb40ecf82022-04-22 16:14:57 +010071 ImgClassPostProcess(TfLiteTensor* outputTensor, Classifier& classifier,
Richard Burton11b75cc2022-04-07 18:00:55 +010072 const std::vector<std::string>& labels,
73 std::vector<ClassificationResult>& results);
74
Richard Burtone6398cd2022-04-13 11:58:28 +010075 /**
Richard Burtonb40ecf82022-04-22 16:14:57 +010076 * @brief Should perform post-processing of the result of inference then
Richard Burtone6398cd2022-04-13 11:58:28 +010077 * populate classification result data for any later use.
78 * @return true if successful, false otherwise.
79 **/
Richard Burton11b75cc2022-04-07 18:00:55 +010080 bool DoPostProcess() override;
Richard Burtonb40ecf82022-04-22 16:14:57 +010081
82 private:
83 TfLiteTensor* m_outputTensor;
84 Classifier& m_imgClassifier;
85 const std::vector<std::string>& m_labels;
86 std::vector<ClassificationResult>& m_results;
Richard Burton11b75cc2022-04-07 18:00:55 +010087 };
88
89} /* namespace app */
90} /* namespace arm */
91
92#endif /* IMG_CLASS_PROCESSING_HPP */