Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2020 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
Éanna Ó Catháin | c6ab02a | 2021-04-07 14:35:25 +0100 | [diff] [blame] | 6 | #include "ObjectDetectionPipeline.hpp" |
Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 7 | #include "ImageUtils.hpp" |
| 8 | |
| 9 | namespace od |
| 10 | { |
| 11 | |
Éanna Ó Catháin | c6ab02a | 2021-04-07 14:35:25 +0100 | [diff] [blame] | 12 | ObjDetectionPipeline::ObjDetectionPipeline(std::unique_ptr<common::ArmnnNetworkExecutor<float>> executor, |
Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 13 | std::unique_ptr<IDetectionResultDecoder> decoder) : |
| 14 | m_executor(std::move(executor)), |
| 15 | m_decoder(std::move(decoder)){} |
| 16 | |
Éanna Ó Catháin | c6ab02a | 2021-04-07 14:35:25 +0100 | [diff] [blame] | 17 | void od::ObjDetectionPipeline::Inference(const cv::Mat& processed, common::InferenceResults<float>& result) |
Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 18 | { |
| 19 | m_executor->Run(processed.data, processed.total() * processed.elemSize(), result); |
| 20 | } |
| 21 | |
Éanna Ó Catháin | c6ab02a | 2021-04-07 14:35:25 +0100 | [diff] [blame] | 22 | void ObjDetectionPipeline::PostProcessing(common::InferenceResults<float>& inferenceResult, |
Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 23 | const std::function<void (DetectedObjects)>& callback) |
| 24 | { |
| 25 | DetectedObjects detections = m_decoder->Decode(inferenceResult, m_inputImageSize, |
| 26 | m_executor->GetImageAspectRatio(), {}); |
| 27 | if (callback) |
| 28 | { |
| 29 | callback(detections); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | void ObjDetectionPipeline::PreProcessing(const cv::Mat& frame, cv::Mat& processed) |
| 34 | { |
| 35 | m_inputImageSize.m_Height = frame.rows; |
| 36 | m_inputImageSize.m_Width = frame.cols; |
| 37 | ResizeWithPad(frame, processed, m_processedFrame, m_executor->GetImageAspectRatio()); |
| 38 | } |
| 39 | |
Éanna Ó Catháin | c6ab02a | 2021-04-07 14:35:25 +0100 | [diff] [blame] | 40 | MobileNetSSDv1::MobileNetSSDv1(std::unique_ptr<common::ArmnnNetworkExecutor<float>> executor, |
Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 41 | float objectThreshold) : |
| 42 | ObjDetectionPipeline(std::move(executor), |
| 43 | std::make_unique<SSDResultDecoder>(objectThreshold)) |
| 44 | {} |
| 45 | |
| 46 | void MobileNetSSDv1::PreProcessing(const cv::Mat& frame, cv::Mat& processed) |
| 47 | { |
| 48 | ObjDetectionPipeline::PreProcessing(frame, processed); |
| 49 | if (m_executor->GetInputDataType() == armnn::DataType::Float32) |
| 50 | { |
| 51 | // [0, 255] => [-1.0, 1.0] |
| 52 | processed.convertTo(processed, CV_32FC3, 1 / 127.5, -1); |
| 53 | } |
| 54 | } |
| 55 | |
Éanna Ó Catháin | c6ab02a | 2021-04-07 14:35:25 +0100 | [diff] [blame] | 56 | YoloV3Tiny::YoloV3Tiny(std::unique_ptr<common::ArmnnNetworkExecutor<float>> executor, |
Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 57 | float NMSThreshold, float ClsThreshold, float ObjectThreshold) : |
| 58 | ObjDetectionPipeline(std::move(executor), |
| 59 | std::move(std::make_unique<YoloResultDecoder>(NMSThreshold, |
| 60 | ClsThreshold, |
| 61 | ObjectThreshold))) |
| 62 | {} |
| 63 | |
| 64 | void YoloV3Tiny::PreProcessing(const cv::Mat& frame, cv::Mat& processed) |
| 65 | { |
| 66 | ObjDetectionPipeline::PreProcessing(frame, processed); |
| 67 | if (m_executor->GetInputDataType() == armnn::DataType::Float32) |
| 68 | { |
| 69 | processed.convertTo(processed, CV_32FC3); |
| 70 | } |
| 71 | } |
| 72 | |
Éanna Ó Catháin | c6ab02a | 2021-04-07 14:35:25 +0100 | [diff] [blame] | 73 | IPipelinePtr CreatePipeline(common::PipelineOptions& config) |
Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 74 | { |
Éanna Ó Catháin | c6ab02a | 2021-04-07 14:35:25 +0100 | [diff] [blame] | 75 | auto executor = std::make_unique<common::ArmnnNetworkExecutor<float>>(config.m_ModelFilePath, config.m_backends); |
Éanna Ó Catháin | 919c14e | 2020-09-14 17:36:49 +0100 | [diff] [blame] | 76 | |
| 77 | if (config.m_ModelName == "SSD_MOBILE") |
| 78 | { |
| 79 | float detectionThreshold = 0.6; |
| 80 | |
| 81 | return std::make_unique<od::MobileNetSSDv1>(std::move(executor), |
| 82 | detectionThreshold |
| 83 | ); |
| 84 | } |
| 85 | else if (config.m_ModelName == "YOLO_V3_TINY") |
| 86 | { |
| 87 | float NMSThreshold = 0.6f; |
| 88 | float ClsThreshold = 0.6f; |
| 89 | float ObjectThreshold = 0.6f; |
| 90 | return std::make_unique<od::YoloV3Tiny>(std::move(executor), |
| 91 | NMSThreshold, |
| 92 | ClsThreshold, |
| 93 | ObjectThreshold |
| 94 | ); |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | throw std::invalid_argument("Unknown Model name: " + config.m_ModelName + " supplied by user."); |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | }// namespace od |