blob: 98e2b5980ca627ff2d5f2f1fa4705ae0d95e215f [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");
40 auto initialImIdx = ctx.Get<uint32_t>("imgIndex");
alexander3c798932021-03-26 21:42:19 +000041
42 constexpr uint32_t dataPsnImgDownscaleFactor = 2;
43 constexpr uint32_t dataPsnImgStartX = 10;
44 constexpr uint32_t dataPsnImgStartY = 35;
45
46 constexpr uint32_t dataPsnTxtInfStartX = 150;
47 constexpr uint32_t dataPsnTxtInfStartY = 40;
48
alexander3c798932021-03-26 21:42:19 +000049 /* If the request has a valid size, set the image index. */
50 if (imgIndex < NUMBER_OF_FILES) {
Éanna Ó Catháin8f958872021-09-15 09:32:30 +010051 if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) {
alexander3c798932021-03-26 21:42:19 +000052 return false;
53 }
54 }
55 if (!model.IsInited()) {
56 printf_err("Model is not initialised! Terminating processing.\n");
57 return false;
58 }
59
alexander3c798932021-03-26 21:42:19 +000060 TfLiteTensor* inputTensor = model.GetInputTensor(0);
alexander3c798932021-03-26 21:42:19 +000061 if (!inputTensor->dims) {
62 printf_err("Invalid input tensor dims\n");
63 return false;
64 } else if (inputTensor->dims->size < 3) {
65 printf_err("Input tensor dimension should be >= 3\n");
66 return false;
67 }
68
Richard Burton11b75cc2022-04-07 18:00:55 +010069 /* Get input shape for displaying the image. */
alexander3c798932021-03-26 21:42:19 +000070 TfLiteIntArray* inputShape = model.GetInputShape(0);
alexander3c798932021-03-26 21:42:19 +000071 const uint32_t nCols = inputShape->data[arm::app::MobileNetModel::ms_inputColsIdx];
72 const uint32_t nRows = inputShape->data[arm::app::MobileNetModel::ms_inputRowsIdx];
73 const uint32_t nChannels = inputShape->data[arm::app::MobileNetModel::ms_inputChannelsIdx];
74
Richard Burton11b75cc2022-04-07 18:00:55 +010075 /* Set up pre and post-processing. */
76 ImgClassPreProcess preprocess = ImgClassPreProcess(&model);
77
alexander3c798932021-03-26 21:42:19 +000078 std::vector<ClassificationResult> results;
Richard Burton11b75cc2022-04-07 18:00:55 +010079 ImgClassPostProcess postprocess = ImgClassPostProcess(ctx.Get<ImgClassClassifier&>("classifier"), &model,
80 ctx.Get<std::vector<std::string>&>("labels"), results);
81
82 UseCaseRunner runner = UseCaseRunner(&preprocess, &postprocess, &model);
alexander3c798932021-03-26 21:42:19 +000083
84 do {
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010085 hal_lcd_clear(COLOR_BLACK);
Richard Burton9b8d67a2021-12-10 12:32:51 +000086
alexander3c798932021-03-26 21:42:19 +000087 /* Strings for presentation/logging. */
88 std::string str_inf{"Running inference... "};
89
Richard Burton11b75cc2022-04-07 18:00:55 +010090 const uint8_t* imgSrc = get_img_array(ctx.Get<uint32_t>("imgIndex"));
91 if (nullptr == imgSrc) {
92 printf_err("Failed to get image index %" PRIu32 " (max: %u)\n", ctx.Get<uint32_t>("imgIndex"),
93 NUMBER_OF_FILES - 1);
94 return false;
95 }
alexander3c798932021-03-26 21:42:19 +000096
97 /* Display this image on the LCD. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +010098 hal_lcd_display_image(
Richard Burton11b75cc2022-04-07 18:00:55 +010099 imgSrc,
alexander3c798932021-03-26 21:42:19 +0000100 nCols, nRows, nChannels,
101 dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor);
102
alexander3c798932021-03-26 21:42:19 +0000103 /* Display message on the LCD - inference running. */
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100104 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
alexander31ae9f02022-02-10 16:15:54 +0000105 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
alexander3c798932021-03-26 21:42:19 +0000106
Richard Burton11b75cc2022-04-07 18:00:55 +0100107 /* Select the image to run inference with. */
Kshitij Sisodiaf9c19ea2021-05-07 16:08:14 +0100108 info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"),
alexander3c798932021-03-26 21:42:19 +0000109 get_filename(ctx.Get<uint32_t>("imgIndex")));
110
Richard Burton11b75cc2022-04-07 18:00:55 +0100111 const size_t imgSz = inputTensor->bytes < IMAGE_DATA_SIZE ?
112 inputTensor->bytes : IMAGE_DATA_SIZE;
113
114 /* Run the pre-processing, inference and post-processing. */
115 if (!runner.PreProcess(imgSrc, imgSz)) {
116 return false;
117 }
118
119 profiler.StartProfiling("Inference");
120 if (!runner.RunInference()) {
121 return false;
122 }
123 profiler.StopProfiling();
124
125 if (!runner.PostProcess()) {
alexander27b62d92021-05-04 20:46:08 +0100126 return false;
127 }
alexander3c798932021-03-26 21:42:19 +0000128
129 /* Erase. */
130 str_inf = std::string(str_inf.size(), ' ');
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100131 hal_lcd_display_text(str_inf.c_str(), str_inf.size(),
alexander31ae9f02022-02-10 16:15:54 +0000132 dataPsnTxtInfStartX, dataPsnTxtInfStartY, false);
alexander3c798932021-03-26 21:42:19 +0000133
alexander3c798932021-03-26 21:42:19 +0000134 /* Add results to context for access outside handler. */
135 ctx.Set<std::vector<ClassificationResult>>("results", results);
136
137#if VERIFY_TEST_OUTPUT
Richard Burton11b75cc2022-04-07 18:00:55 +0100138 TfLiteTensor* outputTensor = model.GetOutputTensor(0);
alexander3c798932021-03-26 21:42:19 +0000139 arm::app::DumpTensor(outputTensor);
140#endif /* VERIFY_TEST_OUTPUT */
141
Kshitij Sisodia68fdd112022-04-06 13:03:20 +0100142 if (!PresentInferenceResult(results)) {
alexander3c798932021-03-26 21:42:19 +0000143 return false;
144 }
145
Isabella Gottardi8df12f32021-04-07 17:15:31 +0100146 profiler.PrintProfilingResult();
147
Éanna Ó Catháin8f958872021-09-15 09:32:30 +0100148 IncrementAppCtxIfmIdx(ctx,"imgIndex");
alexander3c798932021-03-26 21:42:19 +0000149
Richard Burton11b75cc2022-04-07 18:00:55 +0100150 } while (runAll && ctx.Get<uint32_t>("imgIndex") != initialImIdx);
alexander3c798932021-03-26 21:42:19 +0000151
152 return true;
153 }
154
alexander3c798932021-03-26 21:42:19 +0000155} /* namespace app */
156} /* namespace arm */