blob: 38de65b007fc1371c8fbe7d51510c476313643b3 [file] [log] [blame]
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +01001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include "ArmnnNetworkExecutor.hpp"
9#include "YoloResultDecoder.hpp"
10#include "SSDResultDecoder.hpp"
11# include "ImageUtils.hpp"
12
13#include <opencv2/opencv.hpp>
14
15namespace od
16{
17/**
18 * Generic object detection pipeline with 3 steps: data pre-processing, inference execution and inference
19 * result post-processing.
20 *
21 */
22class ObjDetectionPipeline {
23public:
24
25 /**
26 * Creates object detection pipeline with given network executor and decoder.
27 * @param executor - unique pointer to inference runner
28 * @param decoder - unique pointer to inference results decoder
29 */
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010030 ObjDetectionPipeline(std::unique_ptr<common::ArmnnNetworkExecutor<float>> executor,
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010031 std::unique_ptr<IDetectionResultDecoder> decoder);
32
33 /**
34 * @brief Standard image pre-processing implementation.
35 *
36 * Re-sizes an image keeping aspect ratio, pads if necessary to fit the network input layer dimensions.
37
38 * @param[in] frame - input image, expected data type is uint8.
39 * @param[out] processed - output image, data type is preserved.
40 */
41 virtual void PreProcessing(const cv::Mat& frame, cv::Mat& processed);
42
43 /**
44 * @brief Executes inference
45 *
46 * Calls inference runner provided during instance construction.
47 *
48 * @param[in] processed - input inference data. Data type should be aligned with input tensor.
49 * @param[out] result - raw floating point inference results.
50 */
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010051 virtual void Inference(const cv::Mat& processed, common::InferenceResults<float>& result);
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010052
53 /**
54 * @brief Standard inference results post-processing implementation.
55 *
56 * Decodes inference results using decoder provided during construction.
57 *
58 * @param[in] inferenceResult - inference results to be decoded.
59 * @param[in] callback - a function to be called after successful inference results decoding.
60 */
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010061 virtual void PostProcessing(common::InferenceResults<float>& inferenceResult,
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010062 const std::function<void (DetectedObjects)>& callback);
63
64protected:
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010065 std::unique_ptr<common::ArmnnNetworkExecutor<float>> m_executor;
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010066 std::unique_ptr<IDetectionResultDecoder> m_decoder;
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010067 common::Size m_inputImageSize{};
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010068 cv::Mat m_processedFrame;
69};
70
71/**
72 * Specific to Yolo v3 tiny object detection pipeline implementation.
73 */
74class YoloV3Tiny: public ObjDetectionPipeline{
75public:
76
77 /**
78 * Constructs object detection pipeline for Yolo v3 tiny network.
79 *
80 * Network input is expected to be uint8 or fp32. Data range [0, 255].
81 * Network output is FP32.
82 *
83 * @param executor[in] - unique pointer to inference runner
84 * @param NMSThreshold[in] - non max suppression threshold for decoding step
85 * @param ClsThreshold[in] - class probability threshold for decoding step
86 * @param ObjectThreshold[in] - detected object score threshold for decoding step
87 */
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +010088 YoloV3Tiny(std::unique_ptr<common::ArmnnNetworkExecutor<float>> executor,
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +010089 float NMSThreshold, float ClsThreshold, float ObjectThreshold);
90
91 /**
92 * @brief Yolo v3 tiny image pre-processing implementation.
93 *
94 * On top of the standard pre-processing, converts input data type according to the network input tensor data type.
95 * Supported data types: uint8 and float32.
96 *
97 * @param[in] original - input image data
98 * @param[out] processed - image data ready to be used for inference.
99 */
100 void PreProcessing(const cv::Mat& original, cv::Mat& processed);
101
102};
103
104/**
105 * Specific to MobileNet SSD v1 object detection pipeline implementation.
106 */
107class MobileNetSSDv1: public ObjDetectionPipeline {
108
109public:
110 /**
111 * Constructs object detection pipeline for MobileNet SSD network.
112 *
113 * Network input is expected to be uint8 or fp32. Data range [-1, 1].
114 * Network output is FP32.
115 *
116 * @param[in] - unique pointer to inference runner
117 * @paramp[in] objectThreshold - detected object score threshold for decoding step
118 */
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +0100119 MobileNetSSDv1(std::unique_ptr<common::ArmnnNetworkExecutor<float>> executor,
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +0100120 float objectThreshold);
121
122 /**
123 * @brief MobileNet SSD image pre-processing implementation.
124 *
125 * On top of the standard pre-processing, converts input data type according to the network input tensor data type
126 * and scales input data from [0, 255] to [-1, 1] for FP32 input.
127 *
128 * Supported input data types: uint8 and float32.
129 *
130 * @param[in] original - input image data
131 * @param processed[out] - image data ready to be used for inference.
132 */
133 void PreProcessing(const cv::Mat& original, cv::Mat& processed);
134
135};
136
137using IPipelinePtr = std::unique_ptr<od::ObjDetectionPipeline>;
138
139/**
140 * Constructs object detection pipeline based on configuration provided.
141 *
142 * @param[in] config - object detection pipeline configuration.
143 *
144 * @return unique pointer to object detection pipeline.
145 */
Éanna Ó Catháinc6ab02a2021-04-07 14:35:25 +0100146IPipelinePtr CreatePipeline(common::PipelineOptions& config);
Éanna Ó Catháin919c14e2020-09-14 17:36:49 +0100147
148}// namespace od