Michael Levit | 06fcf75 | 2022-01-12 11:53:46 +0200 | [diff] [blame^] | 1 | /* |
| 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 "UseCaseHandler.hpp" |
| 18 | #include "InputFiles.hpp" |
| 19 | #include "YoloFastestModel.hpp" |
| 20 | #include "UseCaseCommonUtils.hpp" |
| 21 | #include "DetectionUseCaseUtils.hpp" |
| 22 | #include "DetectorPostProcessing.hpp" |
| 23 | #include "hal.h" |
| 24 | |
| 25 | #include <inttypes.h> |
| 26 | |
| 27 | |
| 28 | /* used for presentation, original images are read-only"*/ |
| 29 | static uint8_t g_image_buffer[INPUT_IMAGE_WIDTH*INPUT_IMAGE_HEIGHT*FORMAT_MULTIPLY_FACTOR] IFM_BUF_ATTRIBUTE = {}; |
| 30 | |
| 31 | namespace arm { |
| 32 | namespace app { |
| 33 | |
| 34 | |
| 35 | /* Object detection classification handler. */ |
| 36 | bool ObjectDetectionHandler(ApplicationContext& ctx, uint32_t imgIndex, bool runAll) |
| 37 | { |
| 38 | auto& platform = ctx.Get<hal_platform&>("platform"); |
| 39 | auto& profiler = ctx.Get<Profiler&>("profiler"); |
| 40 | |
| 41 | constexpr uint32_t dataPsnImgDownscaleFactor = 1; |
| 42 | constexpr uint32_t dataPsnImgStartX = 10; |
| 43 | constexpr uint32_t dataPsnImgStartY = 35; |
| 44 | |
| 45 | constexpr uint32_t dataPsnTxtInfStartX = 150; |
| 46 | constexpr uint32_t dataPsnTxtInfStartY = 40; |
| 47 | |
| 48 | platform.data_psn->clear(COLOR_BLACK); |
| 49 | |
| 50 | auto& model = ctx.Get<Model&>("model"); |
| 51 | |
| 52 | /* If the request has a valid size, set the image index. */ |
| 53 | if (imgIndex < NUMBER_OF_FILES) { |
| 54 | if (!SetAppCtxIfmIdx(ctx, imgIndex, "imgIndex")) { |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | if (!model.IsInited()) { |
| 59 | printf_err("Model is not initialised! Terminating processing.\n"); |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | auto curImIdx = ctx.Get<uint32_t>("imgIndex"); |
| 64 | |
| 65 | TfLiteTensor* inputTensor = model.GetInputTensor(0); |
| 66 | |
| 67 | if (!inputTensor->dims) { |
| 68 | printf_err("Invalid input tensor dims\n"); |
| 69 | return false; |
| 70 | } else if (inputTensor->dims->size < 3) { |
| 71 | printf_err("Input tensor dimension should be >= 3\n"); |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | TfLiteIntArray* inputShape = model.GetInputShape(0); |
| 76 | |
| 77 | const uint32_t nCols = inputShape->data[arm::app::YoloFastestModel::ms_inputColsIdx]; |
| 78 | const uint32_t nRows = inputShape->data[arm::app::YoloFastestModel::ms_inputRowsIdx]; |
| 79 | const uint32_t nPresentationChannels = FORMAT_MULTIPLY_FACTOR; |
| 80 | |
| 81 | std::vector<DetectionResult> results; |
| 82 | |
| 83 | do { |
| 84 | /* Strings for presentation/logging. */ |
| 85 | std::string str_inf{"Running inference... "}; |
| 86 | |
| 87 | const uint8_t* curr_image = get_img_array(ctx.Get<uint32_t>("imgIndex")); |
| 88 | |
| 89 | /* Copy over the data and convert to gryscale */ |
| 90 | #if DISPLAY_RGB_IMAGE |
| 91 | memcpy(g_image_buffer,curr_image, INPUT_IMAGE_WIDTH*INPUT_IMAGE_HEIGHT*FORMAT_MULTIPLY_FACTOR); |
| 92 | #else |
| 93 | RgbToGrayscale(curr_image,g_image_buffer,INPUT_IMAGE_WIDTH,INPUT_IMAGE_HEIGHT); |
| 94 | #endif /*DISPLAY_RGB_IMAGE*/ |
| 95 | |
| 96 | RgbToGrayscale(curr_image,inputTensor->data.uint8,INPUT_IMAGE_WIDTH,INPUT_IMAGE_HEIGHT); |
| 97 | |
| 98 | |
| 99 | /* Display this image on the LCD. */ |
| 100 | platform.data_psn->present_data_image( |
| 101 | g_image_buffer, |
| 102 | nCols, nRows, nPresentationChannels, |
| 103 | dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor); |
| 104 | |
| 105 | /* If the data is signed. */ |
| 106 | if (model.IsDataSigned()) { |
| 107 | image::ConvertImgToInt8(inputTensor->data.data, inputTensor->bytes); |
| 108 | } |
| 109 | |
| 110 | /* Display message on the LCD - inference running. */ |
| 111 | platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(), |
| 112 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
| 113 | |
| 114 | /* Run inference over this image. */ |
| 115 | info("Running inference on image %" PRIu32 " => %s\n", ctx.Get<uint32_t>("imgIndex"), |
| 116 | get_filename(ctx.Get<uint32_t>("imgIndex"))); |
| 117 | |
| 118 | if (!RunInference(model, profiler)) { |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | /* Erase. */ |
| 123 | str_inf = std::string(str_inf.size(), ' '); |
| 124 | platform.data_psn->present_data_text(str_inf.c_str(), str_inf.size(), |
| 125 | dataPsnTxtInfStartX, dataPsnTxtInfStartY, 0); |
| 126 | |
| 127 | /* Detector post-processing*/ |
| 128 | TfLiteTensor* output_arr[2] = {nullptr,nullptr}; |
| 129 | output_arr[0] = model.GetOutputTensor(0); |
| 130 | output_arr[1] = model.GetOutputTensor(1); |
| 131 | RunPostProcessing(g_image_buffer,output_arr,results); |
| 132 | |
| 133 | platform.data_psn->present_data_image( |
| 134 | g_image_buffer, |
| 135 | nCols, nRows, nPresentationChannels, |
| 136 | dataPsnImgStartX, dataPsnImgStartY, dataPsnImgDownscaleFactor); |
| 137 | |
| 138 | /*Detector post-processing*/ |
| 139 | |
| 140 | |
| 141 | /* Add results to context for access outside handler. */ |
| 142 | ctx.Set<std::vector<DetectionResult>>("results", results); |
| 143 | |
| 144 | #if VERIFY_TEST_OUTPUT |
| 145 | arm::app::DumpTensor(outputTensor); |
| 146 | #endif /* VERIFY_TEST_OUTPUT */ |
| 147 | |
| 148 | if (!image::PresentInferenceResult(platform, results)) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | profiler.PrintProfilingResult(); |
| 153 | |
| 154 | IncrementAppCtxIfmIdx(ctx,"imgIndex"); |
| 155 | |
| 156 | } while (runAll && ctx.Get<uint32_t>("imgIndex") != curImIdx); |
| 157 | |
| 158 | return true; |
| 159 | } |
| 160 | |
| 161 | } /* namespace app */ |
| 162 | } /* namespace arm */ |