7#include <opencv2/core.hpp>
8#include <opencv2/core/mat.hpp>
9#include <opencv2/core/types.hpp>
10#include <opencv2/dnn/dnn.hpp>
12#include "helpers/Globals.hpp"
14#include "detectors/ObjectDetector.hpp"
28 switch (p_objectDetectorTypes)
30 case Globals::ObjectDetectorTypes::NONE:
34 case Globals::ObjectDetectorTypes::STANDARD:
36 m_net = cv::dnn::readNetFromDarknet(p_yoloFolderPath +
"yolov7.cfg", p_yoloFolderPath +
"yolov7.weights");
39 case Globals::ObjectDetectorTypes::TINY:
41 m_net = cv::dnn::readNetFromDarknet(p_yoloFolderPath +
"yolov7-tiny.cfg", p_yoloFolderPath +
"yolov7-tiny.weights");
48 switch (p_objectDetectorBackEnds)
50 case Globals::ObjectDetectorBackEnds::NONE:
52 case Globals::ObjectDetectorBackEnds::CPU:
53 m_net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
54 m_net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU);
57 case Globals::ObjectDetectorBackEnds::GPU:
58 m_net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV);
59 m_net.setPreferableTarget(cv::dnn::DNN_TARGET_OPENCL);
62 case Globals::ObjectDetectorBackEnds::CUDA:
63 m_net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
64 m_net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA);
71 m_blobSize =
static_cast<int32_t
>(p_objectDetectorBlobSizes);
88 std::vector<cv::Mat> outputBlobs;
90 m_net.setInput(BLOB_FROM_IMAGE);
94 std::vector<std::string> initialObjectNames;
95 std::vector<cv::Rect> initialObjectBoundingBoxes;
96 std::vector<float> initialObjectConfidences;
97 cv::Point maxConfidenceObjectIndex;
98 double maxConfidence = -DBL_MAX;
100 for (
auto& outputBlob : outputBlobs)
102 for (int32_t rowIndex = 0; rowIndex < outputBlob.rows; rowIndex++)
121 initialObjectConfidences.push_back(
static_cast<float>(maxConfidence));
128 std::vector<int> nonMaximaSuppressedFilteredIndicies;
132 for (
const int32_t& index : nonMaximaSuppressedFilteredIndicies)
Used for functionality that has not been implemented.
ObjectDetectionInformation m_objectDetectionInformation
The ObjectDetectionInformation struct containing all object detection-related information.
cv::dnn::Net m_net
OpenCV object which allows the use of pre-trained neural networks.
std::vector< std::string > m_unconnectedOutputLayerNames
The names of layers with unconnected outputs.
void SetProperties(const std::string &p_yoloFolderPath, const Globals::ObjectDetectorTypes &p_objectDetectorTypes, const Globals::ObjectDetectorBackEnds &p_objectDetectorBackEnds, const Globals::ObjectDetectorBlobSizes &p_objectDetectorBlobSizes)
Set the properties of the ObjectDetector object.
int32_t m_blobSize
The spatial size for the output image used by the cv::dnn::blobFromImage function.
ObjectDetector()
Construct a new ObjectDetector object.
void RunObjectDetector(const cv::Mat &p_frame)
Run the object detector against p_frame.
bool m_skipObjectDetection
Whether to skip object detection.
ObjectDetectionInformation GetInformation()
Get the ObjectDetectionInformation struct.
static const cv::Scalar G_COLOUR_BLACK
OpenCV Colours (in BGR format).
static const int32_t G_OBJECT_DETECTOR_OUTPUT_BLOBS_CENTER_Y_COORD_INDEX
The indicies representing various values in the output blobs.
static const uint32_t G_OBJECT_DETECTOR_BOUNDING_BOX_BUFFER
Object detection bounding box properties.
static const uint32_t G_VIDEO_INPUT_HEIGHT
Input video dimensions.
static const uint32_t G_VIDEO_INPUT_WIDTH
Input video dimensions.
static const double G_OBJECT_DETECTOR_CONFIDENCE_THRESHOLD
Object detection threshold and properties.
static const double G_OBJECT_DETECTOR_NMS_THRESHOLD
Object detection threshold and properties.
static const double G_DIVIDE_BY_TWO
Divide by two.
static const int32_t G_OBJECT_DETECTOR_OUTPUT_BLOBS_HEIGHT_INDEX
The indicies representing various values in the output blobs.
static const std::array< std::string, G_OBJECT_DETECTOR_NUMBER_OF_DETECTABLE_OBJECTS > G_OBJECT_DETECTOR_OBJECT_NAMES
Names of detectable objects. The order is significant and should not be changed.
static const int32_t G_ROI_BOTTOM_HEIGHT
Region-of-interest dimensions.
ObjectDetectorTypes
The type of object detector to use with an option to disable object detection. The tiny version is mo...
static const int32_t G_OBJECT_DETECTOR_OUTPUT_BLOBS_OBJECT_SCORES_START_INDEX
The indicies representing various values in the output blobs.
ObjectDetectorBackEnds
The supported backends for the object detector to run on. In theory, GPU should be significantly more...
static const int32_t G_OBJECT_DETECTOR_OUTPUT_BLOBS_CENTER_X_COORD_INDEX
The indicies representing various values in the output blobs.
static const std::map< std::string, cv::Scalar > G_OBJECT_DETECTOR_OBJECT_NAMES_AND_COLOURS
Object names and bounding box colours. G_OPENCV_WHITE is used as the default colour while custom colo...
static const double G_OBJECT_DETECTOR_SCALE_FACTOR
Object detection threshold and properties.
static const int32_t G_OBJECT_DETECTOR_OUTPUT_BLOBS_WIDTH_INDEX
The indicies representing various values in the output blobs.
ObjectDetectorBlobSizes
The supported blob sizes for the object detector to run with. The larger the blob size the more perfo...
static const uint32_t G_CONVERT_DECIMAL_TO_PERCENTAGE
Convert a decimal value to a percentage.
Contains all Lane-and-Object-Detection objects.