Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
TorchObjectDetection.hpp
Go to the documentation of this file.
1
12#ifndef TORCH_OBJECT_DETECTION_HPP
13#define TORCH_OBJECT_DETECTION_HPP
14
15#include "../../util/vision/ObjectDetectionUtility.hpp"
16#include "../../util/vision/YOLOModel.hpp"
17
19#include <chrono>
20
22
23
31namespace torchobject
32{
33
45 inline std::vector<objectdetectutils::Object> Detect(const cv::Mat& cvFrame,
47 const float fMinObjectConfidence = 0.40f,
48 const float fNMSThreshold = 0.60f)
49 {
50 // Check if the input frame is in RGB format.
51 if (cvFrame.channels() != 3)
52 {
53 // Submit logger message.
54 LOG_ERROR(logging::g_qSharedLogger, "Detect() requires a RGB image.");
55 return {};
56 }
57
58 // Declare instance variables.
59 std::vector<objectdetectutils::Object> vDetectedTags;
60
61 // Check if the PyTorch interpreter hardware is opened and the model is loaded.
62 if (trPyTorchDetector.IsReadyForInference())
63 {
64 // Run inference on YOLO model with current image.
65 std::vector<yolomodel::Detection> vOutputTensorTags = trPyTorchDetector.Inference(cvFrame, fMinObjectConfidence, fNMSThreshold);
66
67 // Repackage detections into tensorflow tags.
68 for (const yolomodel::Detection& stTagDetection : vOutputTensorTags)
69 {
70 // Create and initialize new TensorflowTag.
71 objectdetectutils::Object stDetectedTag;
72 stDetectedTag.dConfidence = stTagDetection.fConfidence;
73 stDetectedTag.pBoundingBox = std::make_shared<cv::Rect2d>(stTagDetection.cvBoundingBox);
74 stDetectedTag.szClassName = stTagDetection.szClassName;
75 stDetectedTag.eDetectionMethod = objectdetectutils::ObjectDetectionMethod::eTorch;
76 stDetectedTag.cvImageResolution = cvFrame.size();
77
78 // Add the newly detected tag to the vector.
79 vDetectedTags.emplace_back(stDetectedTag);
80 }
81 }
82 else
83 {
84 // Submit logger message.
85 LOG_WARNING(logging::g_qSharedLogger,
86 "TorchDetect: Unable to detect tags using YOLO torch detection because hardware is not opened or model is not initialized.");
87 }
88
89 // Return the detected tags.
90 return vDetectedTags;
91 }
92
93
102 inline void DrawDetections(cv::Mat& cvDetectionsFrame, const std::vector<objectdetectutils::Object>& vDetectedTags)
103 {
104 // Check if the given frame is a 1 or 3 channel image. (not BGRA)
105 if (!cvDetectionsFrame.empty() && (cvDetectionsFrame.channels() == 1 || cvDetectionsFrame.channels() == 3))
106 {
107 // Loop through each detection.
108 for (const objectdetectutils::Object& stTag : vDetectedTags)
109 {
110 // Check if the tag detection type is Torch.
111 if (stTag.eDetectionMethod == objectdetectutils::ObjectDetectionMethod::eTorch)
112 {
113 // Draw bounding box onto image.
114 cv::rectangle(cvDetectionsFrame, *stTag.pBoundingBox, cv::Scalar(255, 255, 255), 2);
115 std::string szText = stTag.szClassName + " " + std::to_string(static_cast<int>(stTag.dConfidence * 100)) + "%";
116 cv::Size cvTextSize = cv::getTextSize(szText, cv::FONT_HERSHEY_SIMPLEX, 0.75, 1, nullptr);
117 // Draw classID background box onto image.
118 cv::rectangle(cvDetectionsFrame,
119 stTag.pBoundingBox->tl() + cv::Point2d(0, stTag.pBoundingBox->height),
120 stTag.pBoundingBox->tl() + cv::Point2d(cvTextSize.width, stTag.pBoundingBox->height + cvTextSize.height),
121 cv::Scalar(255, 255, 255),
122 cv::FILLED);
123 // Draw class text onto image.
124 cv::putText(cvDetectionsFrame,
125 szText,
126 stTag.pBoundingBox->tl() + cv::Point2d(0, stTag.pBoundingBox->height + cvTextSize.height),
128 0.5,
129 cv::Scalar(0, 0, 0));
130 }
131 }
132 }
133 else
134 {
135 // Submit logger message.
136 LOG_ERROR(logging::g_qSharedLogger,
137 "TorchDetect: Unable to draw markers on image because it is empty or because it has {} channels. (Should be 1 or 3)",
138 cvDetectionsFrame.channels());
139 }
140 }
141} // namespace torchobject
142
143#endif
MatSize size
int channels() const
bool empty() const
This class is designed to enable quick, easy, and robust inferencing of .pt yolo model.
Definition YOLOModel.hpp:710
bool IsReadyForInference() const
Check if the model is ready for inference.
Definition YOLOModel.hpp:946
std::vector< Detection > Inference(const cv::Mat &cvInputFrame, const float fMinObjectConfidence=0.85, const float fNMSThreshold=0.6)
Given an input image forward the image through the YOLO model to run inference on the PyTorch model,...
Definition YOLOModel.hpp:859
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Size getTextSize(const String &text, int fontFace, double fontScale, int thickness, int *baseLine)
void putText(InputOutputArray img, const String &text, Point org, int fontFace, double fontScale, Scalar color, int thickness=1, int lineType=LINE_8, bool bottomLeftOrigin=false)
FONT_HERSHEY_SIMPLEX
Namespace containing functions related to torch object detections operations on images using PyTorch.
Definition TorchObjectDetection.hpp:32
void DrawDetections(cv::Mat &cvDetectionsFrame, const std::vector< objectdetectutils::Object > &vDetectedTags)
Given a vector of objectdetectutils::Object structs draw each tag corner and confidence onto the give...
Definition TorchObjectDetection.hpp:102
std::vector< objectdetectutils::Object > Detect(const cv::Mat &cvFrame, yolomodel::pytorch::PyTorchInterpreter &trPyTorchDetector, const float fMinObjectConfidence=0.40f, const float fNMSThreshold=0.60f)
Detects objects in the given image using a PyTorch model.
Definition TorchObjectDetection.hpp:45
Represents a single detected object. Combines attributes from TorchObject and TensorflowObject struct...
Definition ObjectDetectionUtility.hpp:73
This struct is used to.
Definition YOLOModel.hpp:45