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
ObjectDetectionChecker.hpp
Go to the documentation of this file.
1
12#ifndef OBJECT_DETECTION_CHECKER_HPP
13#define OBJECT_DETECTION_CHECKER_HPP
14
15#include "../../AutonomyGlobals.h"
16#include "../../vision/objects/ObjectDetector.h"
17
19
21
22
29namespace statemachine
30{
31
40 inline void LoadDetectedObjects(std::vector<objectdetectutils::Object>& vDetectedObjects, const std::vector<std::shared_ptr<ObjectDetector>>& vObjectDetectors)
41 {
42 // Number of object detectors.
43 size_t siNumObjectDetectors = vObjectDetectors.size();
44
45 // Initialize vectors to store detected objects temporarily.
46 std::vector<std::vector<objectdetectutils::Object>> vDetectedObjectBuffers(siNumObjectDetectors);
47
48 // Initialize vectors to store detected objects futures.
49 std::vector<std::future<bool>> vDetectedObjectsFuture;
50
51 // Request objects from each detector.
52 for (size_t siIdx = 0; siIdx < siNumObjectDetectors; ++siIdx)
53 {
54 // Check if this object detector is ready.
55 if (vObjectDetectors[siIdx]->GetIsReady())
56 {
57 // Request detected objects from detector.
58 vDetectedObjectsFuture.emplace_back(vObjectDetectors[siIdx]->RequestDetectedObjects(vDetectedObjectBuffers[siIdx]));
59 }
60 }
61
62 // Ensure all requests have been fulfilled.
63 // Then transfer objects from the buffer to vDetectedObjects for the user to access.
64 for (size_t siIdx = 0; siIdx < vDetectedObjectsFuture.size(); ++siIdx)
65 {
66 // Wait for the request to be fulfilled.
67 vDetectedObjectsFuture[siIdx].get();
68
69 // Loop through the detected objects and add them to the vDetectedObjects vector.
70 for (const objectdetectutils::Object& tObject : vDetectedObjectBuffers[siIdx])
71 {
72 vDetectedObjects.emplace_back(tObject);
73 }
74 }
75 }
76
77
90 inline int IdentifyTargetObject(const std::vector<std::shared_ptr<ObjectDetector>>& vObjectDetectors,
91 objectdetectutils::Object& stObjectTarget,
92 const geoops::WaypointType& eDesiredDetectionType = geoops::WaypointType::eUNKNOWN)
93 {
94 // Create instance variables.
95 std::vector<objectdetectutils::Object> vDetectedObjects;
96 objectdetectutils::Object stBestObject;
97 std::string szIdentifiedObjects = "";
98
99 // Get the current time
100 std::chrono::system_clock::time_point tmCurrentTime = std::chrono::system_clock::now();
101
102 // Load all detected objects in the rover's vision.
103 LoadDetectedObjects(vDetectedObjects, vObjectDetectors);
104 // Find the best object.
105 for (const objectdetectutils::Object& stCandidate : vDetectedObjects)
106 {
107 // Calculate the total age of the object.
108 double dObjectTotalAge = std::fabs(std::chrono::duration_cast<std::chrono::milliseconds>(tmCurrentTime - stCandidate.tmCreation).count() / 1000.0);
109 // Calculate the total object area.
110 double dArea = stCandidate.pBoundingBox->area();
111 // Calculate what percentage of the screen the object takes up.
112 double dAreaPercentage = (dArea / (stCandidate.cvImageResolution.width * stCandidate.cvImageResolution.height)) * 100.0;
113
114 // If the distance of the object is not greater than 0, skip it.
115 if (stCandidate.dStraightLineDistance <= 0.0)
116 {
117 continue;
118 }
119
120 // Determine the desired detection type.
121 switch (eDesiredDetectionType)
122 {
123 case geoops::WaypointType::eMalletWaypoint:
124 {
125 if (stCandidate.eDetectionType != objectdetectutils::ObjectDetectionType::eMallet)
126 {
127 continue;
128 }
129 break;
130 }
131 case geoops::WaypointType::eWaterBottleWaypoint:
132 {
133 if (stCandidate.eDetectionType != objectdetectutils::ObjectDetectionType::eWaterBottle)
134 {
135 continue;
136 }
137 break;
138 }
139 case geoops::WaypointType::eObjectWaypoint:
140 {
141 if (stCandidate.eDetectionType != objectdetectutils::ObjectDetectionType::eMallet &&
142 stCandidate.eDetectionType != objectdetectutils::ObjectDetectionType::eWaterBottle)
143 {
144 continue;
145 }
146 break;
147 }
148 default:
149 {
150 break;
151 }
152 }
153
154 // Check the object detection method type.
155 if (stCandidate.eDetectionMethod == objectdetectutils::ObjectDetectionMethod::eTorch)
156 {
157 // Assemble the identified objects string.
158 szIdentifiedObjects += "\tObject Class: " + stCandidate.szClassName + " Object Age: " + std::to_string(dObjectTotalAge) +
159 "s Object Screen Percentage: " + std::to_string(dAreaPercentage) + "%\n";
160 // Check if the object meets the requirements.
161 if (dAreaPercentage < constants::BBOX_MIN_SCREEN_PERCENTAGE || dObjectTotalAge < constants::BBOX_MIN_LIFETIME_THRESHOLD)
162 {
163 continue;
164 }
165
166 // Check other object requirements.
167 if (dArea > stBestObject.pBoundingBox->area())
168 {
169 // Set the target object to the detected object.
170 stBestObject = stCandidate;
171 }
172 }
173 }
174
175 // Only print the identified objects if there are any.
176 if (stBestObject.dConfidence != 0.0)
177 {
178 // Submit logger message.
179 LOG_DEBUG(logging::g_qSharedLogger, "ObjectDetectionChecker: Identified objects:\n{}", szIdentifiedObjects);
180 }
181
182 // Set the target object to the best object.
183 stObjectTarget = stBestObject;
184
185 return static_cast<int>(vDetectedObjects.size());
186 }
187} // namespace statemachine
188#endif
Namespace containing all state machine related classes.
Definition State.hpp:23
int IdentifyTargetObject(const std::vector< std::shared_ptr< ObjectDetector > > &vObjectDetectors, objectdetectutils::Object &stObjectTarget, const geoops::WaypointType &eDesiredDetectionType=geoops::WaypointType::eUNKNOWN)
Identify a target object in the rover's vision, using Torch detection.
Definition ObjectDetectionChecker.hpp:90
void LoadDetectedObjects(std::vector< objectdetectutils::Object > &vDetectedObjects, const std::vector< std::shared_ptr< ObjectDetector > > &vObjectDetectors)
Aggregates all detected objects from each provided object detector.
Definition ObjectDetectionChecker.hpp:40
Represents a single detected object. Combines attributes from TorchObject and TensorflowObject struct...
Definition ObjectDetectionUtility.hpp:73