blob: c68d8167f3ab510290f1bd28308a5f0683a5dd2b [file] [log] [blame]
alexander3c798932021-03-26 21:42:19 +00001/*
Richard Burtoned35a6f2022-02-14 11:55:35 +00002 * Copyright (c) 2021-2022 Arm Limited. All rights reserved.
alexander3c798932021-03-26 21:42:19 +00003 * 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 "UseCaseHandler.hpp"
18
19#include "Classifier.hpp"
20#include "InputFiles.hpp"
21#include "MobileNetModel.hpp"
Richard Burtoned35a6f2022-02-14 11:55:35 +000022#include "ImageUtils.hpp"
alexander3c798932021-03-26 21:42:19 +000023#include "UseCaseCommonUtils.hpp"
24#include "hal.h"
alexander31ae9f02022-02-10 16:15:54 +000025#include "log_macros.h"
Richard Burton11b75cc2022-04-07 18:00:55 +010026#include "ImgClassProcessing.hpp"
alexander3c798932021-03-26 21:42:19 +000027
alexander31ae9f02022-02-10 16:15:54 +000028#include <cinttypes>
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +010029
alexander3c798932021-03-26 21:42:19 +000030using ImgClassClassifier = arm::app::Classifier;
31
32namespace arm {
33namespace app {
34
Richard Burton11b75cc2022-04-07 18:00:55 +010035 /* Image classification inference handler. */
alexander3c798932021-03-26 21:42:19 +000036 bool ClassifyImageHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll)
37 {
Isabella Gottardi8df12f32021-04-07 17:15:31 +010038 auto& profiler = ctx.Get<Profiler&>("profiler");
Richard Burton11b75cc2022-04-07 18:00:55 +010039 auto& model = ctx.Get<Model&>("model");
Richard Burtone6398cd2022-04-13 11:58:28 +010040 /* If the request has a valid size, set the image index as it might not be set. */
41 if (imgIndex < NUMBER_OF_FILES) {
42 if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) {
43 return false;
44 }
45 }
Richard Burtonc20be972022-04-19 17:01:08 +010046 auto initialImgIdx = ctx.Get<uint32_t>("imgIndex");
alexander3c798932021-03-26 21:42:19 +000047
48 constexpr uint32_t dataPsnImgDownscaleFactor = 2;
49 constexpr uint32_t dataPsnImgStartX = 10;
50 constexpr uint32_t dataPsnImgStartY = 35;
51
52 constexpr uint32_t dataPsnTxtInfStartX = 150;
53 constexpr uint32_t dataPsnTxtInfStartY = 40;
54
Richard Burtone6398cd2022-04-13 11:58:28 +010055
alexander3c798932021-03-26 21:42:19 +000056 if (!model.IsInited()) {
57 printf_err("Model is not initialised! Terminating processing.\n");
58 return false;
59 }
60
alexander3c798932021-03-26 21:42:19 +000061 TfLiteTensor* inputTensor = model.GetInputTensor(0);
alexander3c798932021-03-26 21:42:19 +000062 if (!inputTensor->dims) {
63 printf_err("Invalid input tensor dims\n");
64 return false;
Richard Burtonc20be972022-04-19 17:01:08 +010065 } else if (inputTensor->dims->size < 4) {
66 printf_err("Input tensor dimension should be = 4\n");
alexander3c798932021-03-26 21:42:19 +000067 return false;
68 }
69
Richard Burton11b75cc2022-04-07 18:00:55 +010070 /* Get input shape for displaying the image. */
alexander3c798932021-03-26 21:42:19 +000071 TfLiteIntArray* inputShape = model.GetInputShape(0);
alexander3c798932021-03-26 21:42:19 +000072 const uint32_t nCols = inputShape->data[arm::app::MobileNetModel::ms_inputColsIdx];
73 const uint32_t nRows = inputShape->data[arm::app::MobileNetModel::ms_inputRowsIdx];
74 const uint32_t nChannels = inputShape->data[arm::app::MobileNetModel::ms_inputChannelsIdx];
75
Richard Burton11b75cc2022-04-07 18:00:55 +010076 /* Set up pre and post-processing. */
77 ImgClassPreProcess preprocess = ImgClassPreProcess(&model);
78
alexander3c798932021-03-26 21:42:19 +000079 std::vector<ClassificationResult> results;
Richard Burton11b75cc2022-04-07 18:00:55 +010080 ImgClassPostProcess postprocess = ImgClassPostProcess(ctx.Get<ImgClassClassifier&>("classifier"), &model,
81 ctx.Get<std::vector<std::string>&>("labels"), results);
82
83 UseCaseRunner runner = UseCaseRunner(&preprocess, &postprocess, &model);
alexander3c798932021-03-26 21:42:19 +000084
85 do {
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010086 hal_lcd_clear(COLOR_BLACK);
Richard Burton9b8d67a2021-12-10 12:32:51 +000087
alexander3c798932021-03-26 21:42:19 +000088 /* Strings for presentation/logging. */
89 std::string str_inf{"Running inference... "};
90
Richard Burton11b75cc2022-04-07 18:00:55 +010091 const uint8_t* imgSrc = get_img_array(ctx.Get<uint32_t>("imgIndex"));
92 if (nullptr == imgSrc) {
93 printf_err("Failed to get image index %" PRIu32 " (max: %u)\n", ctx.Get<uint32_t>("imgIndex"),
94 NUMBER_OF_FILES - 1);
95 return false;
96 }
alexander3c798932021-03-26 21:42:19 +000097
98 /* Display this image on the LCD. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010099 hal_lcd_display_image(
Richard Burton11b75cc2022-04-07 18:00:55 +0100100 imgSrc,
alexander3c798932021-03-26 21:42:19 +0000101 nCols, nRows, nChannels,
102 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
103
alexander3c798932021-03-26 21:42:19 +0000104 /* Display message on the LCD - inference running. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100105 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
Richard Burtone6398cd2022-04-13 11:58:28 +0100106 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
alexander3c798932021-03-26 21:42:19 +0000107
Richard Burton11b75cc2022-04-07 18:00:55 +0100108 /* Select the image to run inference with. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100109 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
alexander3c798932021-03-26 21:42:19 +0000110 get_filename(ctx.Get<uint32_t>("imgIndex")));
111
Richard Burton11b75cc2022-04-07 18:00:55 +0100112 const size_t imgSz = inputTensor->bytes < IMAGE_DATA_SIZE ?
113 inputTensor->bytes : IMAGE_DATA_SIZE;
114
115 /* Run the pre-processing, inference and post-processing. */
116 if (!runner.PreProcess(imgSrc, imgSz)) {
117 return false;
118 }
119
120 profiler.StartProfiling("Inference");
121 if (!runner.RunInference()) {
122 return false;
123 }
124 profiler.StopProfiling();
125
126 if (!runner.PostProcess()) {
alexander27b62d92021-05-04 20:46:08 +0100127 return false;
128 }
alexander3c798932021-03-26 21:42:19 +0000129
130 /* Erase. */
131 str_inf = std::string(str_inf.size(), ' ');
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100132 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
Richard Burtone6398cd2022-04-13 11:58:28 +0100133 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
alexander3c798932021-03-26 21:42:19 +0000134
alexander3c798932021-03-26 21:42:19 +0000135 /* Add results to context for access outside handler. */
136 ctx.Set<std::vector<ClassificationResult>>("results", results);
137
138#if VERIFY_TEST_OUTPUT
Richard Burton11b75cc2022-04-07 18:00:55 +0100139 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
alexander3c798932021-03-26 21:42:19 +0000140 arm::app::DumpTensor(outputTensor);
141#endif /* VERIFY_TEST_OUTPUT */
142
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100143 if (!PresentInferenceResult(results)) {
alexander3c798932021-03-26 21:42:19 +0000144 return false;
145 }
146
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100147 profiler.PrintProfilingResult();
148
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100149 IncrementAppCtxIfmIdx(ctx,"imgIndex");
alexander3c798932021-03-26 21:42:19 +0000150
Richard Burtonc20be972022-04-19 17:01:08 +0100151 } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImgIdx);
alexander3c798932021-03-26 21:42:19 +0000152
153 return true;
154 }
155
alexander3c798932021-03-26 21:42:19 +0000156} /* namespace app */
157} /* namespace arm */