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
TagDetector.h
Go to the documentation of this file.
1
12#ifndef TAG_DETECTOR_H
13#define TAG_DETECTOR_H
14
15#include "../../interfaces/BasicCamera.hpp"
16#include "../../interfaces/ZEDCamera.hpp"
17#include "../../util/vision/BoundingBoxTracking.h"
18#include "../../util/vision/TagDetectionUtilty.hpp"
19#include "../../util/vision/YOLOModel.hpp"
20
22#include <future>
23#include <shared_mutex>
24#include <vector>
25
27
28
45class TagDetector : public AutonomyThread<void>
46{
47 public:
49 // Declare public methods.
51 TagDetector(std::shared_ptr<BasicCamera> pBasicCam,
52 const int nArucoCornerRefinementMaxIterations = 30,
53 const int nArucoCornerRefinementMethod = cv::aruco::CORNER_REFINE_NONE,
54 const int nArucoMarkerBorderBits = 1,
55 const bool bArucoDetectInvertedMarkers = false,
56 const bool bUseAruco3Detection = false,
57 const bool bEnableTracking = false,
58 const int nDetectorMaxFPS = 30,
59 const bool bEnableRecordingFlag = false,
60 const int nNumDetectedTagsRetrievalThreads = 5,
61 const bool bUsingGpuMats = false);
62 TagDetector(std::shared_ptr<ZEDCamera> pZEDCam,
63 const int nArucoCornerRefinementMaxIterations = 30,
64 const int nArucoCornerRefinementMethod = cv::aruco::CORNER_REFINE_NONE,
65 const int nArucoMarkerBorderBits = 1,
66 const bool bArucoDetectInvertedMarkers = false,
67 const bool bUseAruco3Detection = false,
68 const bool bEnableTracking = false,
69 const int nDetectorMaxFPS = 30,
70 const bool bEnableRecordingFlag = false,
71 const int nNumDetectedTagsRetrievalThreads = 5,
72 const bool bUsingGpuMats = false);
74 std::future<bool> RequestDetectionOverlayFrame(cv::Mat& cvFrame);
75 std::future<bool> RequestLastGoodOverlayFrame(cv::Mat& cvFrame);
76 std::future<bool> RequestDetectedArucoTags(std::vector<tagdetectutils::ArucoTag>& vArucoTags);
77 bool InitTorchDetection(const std::string& szModelPath,
78 yolomodel::pytorch::PyTorchInterpreter::HardwareDevices eDevice = yolomodel::pytorch::PyTorchInterpreter::HardwareDevices::eCUDA);
79
81 // Mutators.
83
84 void EnableTorchDetection(const float fMinObjectConfidence = 0.4f, const float fNMSThreshold = 0.6f);
86 void SetDetectorMaxFPS(const int nRecordingFPS);
87 void SetEnableRecordingFlag(const bool bEnableRecordingFlag);
88
90 // Accessors.
92
93 bool GetIsReady();
94 int GetDetectorMaxFPS() const;
95 bool GetEnableRecordingFlag() const;
96 std::string GetCameraName();
98
99 private:
101 // Declare private methods.
103
104 void ThreadedContinuousCode() override;
105 void PooledLinearCode() override;
106 void UpdateDetectedTags(std::vector<tagdetectutils::ArucoTag>& vNewlyDetectedTags);
107
109 // Declare private member variables.
111 // Class member variables.
112
113 std::shared_ptr<Camera<cv::Mat>> m_pCamera;
114 cv::aruco::ArucoDetector m_cvArucoDetector;
115 cv::aruco::DetectorParameters m_cvArucoDetectionParams;
116 cv::aruco::Dictionary m_cvTagDictionary;
117 std::shared_ptr<yolomodel::pytorch::PyTorchInterpreter> m_pTorchDetector;
118 std::atomic<float> m_fTorchMinObjectConfidence;
119 std::atomic<float> m_fTorchNMSThreshold;
120 std::atomic_bool m_bTorchInitialized;
121 std::atomic_bool m_bTorchEnabled;
122 std::shared_ptr<tracking::MultiTracker> m_pMultiTracker;
123 bool m_bUsingZedCamera;
124 bool m_bUsingGpuMats;
125 bool m_bCameraIsOpened;
126 bool m_bEnableTracking;
127 int m_nNumDetectedTagsRetrievalThreads;
128 std::string m_szCameraName;
129 std::atomic_bool m_bEnableRecordingFlag;
130
131 // Detected tags storage.
132
133 std::vector<tagdetectutils::ArucoTag> m_vNewlyDetectedTags;
134 std::vector<tagdetectutils::ArucoTag> m_vDetectedArucoTags;
135
136 // Rover position for tag geolocalization.
137 geoops::RoverPose m_stRoverPose;
138
139 // Create frames for storing images and point clouds.
140
141 cv::Mat m_cvFrame;
142 cv::cuda::GpuMat m_cvGPUFrame;
143 cv::Mat m_cvArucoProcFrame;
144 cv::Mat m_cvLastGoodDetectionOverlayFrame;
145 cv::Mat m_cvPointCloud;
146 cv::cuda::GpuMat m_cvGPUPointCloud;
147
148 // Queues and mutexes for scheduling and copying data to other threads.
149
150 std::queue<containers::FrameFetchContainer<cv::Mat>> m_qDetectionOverlayFramesCopySchedule;
151 std::queue<containers::FrameFetchContainer<cv::Mat>> m_qLastGoodDetectionOverlayFramesCopySchedule;
152 std::queue<containers::DataFetchContainer<std::vector<tagdetectutils::ArucoTag>>> m_qDetectedArucoTagCopySchedule;
153 std::shared_mutex m_muPoolScheduleMutex;
154 std::shared_mutex m_muDetectionOverlayCopyMutex;
155 std::shared_mutex m_muLastGoodDetectionOverlayCopyMutex;
156 std::shared_mutex m_muArucoDataCopyMutex;
157};
158
159#endif
Interface class used to easily multithread a child class.
Definition AutonomyThread.hpp:40
Run's Aruco detection & camera pose estimation in a multithreading environment. Given a camera,...
Definition TagDetector.h:46
bool GetEnableRecordingFlag() const
Accessor for the Enable Recording Flag private member.
Definition TagDetector.cpp:737
int GetDetectorMaxFPS() const
Accessor for the desired max FPS for this detector.
Definition TagDetector.cpp:722
void SetDetectorMaxFPS(const int nRecordingFPS)
Mutator for the desired max FPS for this detector.
Definition TagDetector.cpp:653
std::future< bool > RequestDetectionOverlayFrame(cv::Mat &cvFrame)
Request a copy of a frame containing the tag detection overlays from the aruco and torch library.
Definition TagDetector.cpp:496
void SetEnableRecordingFlag(const bool bEnableRecordingFlag)
Mutator for the Enable Recording Flag private member.
Definition TagDetector.cpp:667
void EnableTorchDetection(const float fMinObjectConfidence=0.4f, const float fNMSThreshold=0.6f)
Turn on torch detection with given parameters.
Definition TagDetector.cpp:611
std::future< bool > RequestDetectedArucoTags(std::vector< tagdetectutils::ArucoTag > &vArucoTags)
Request the most up to date vector of detected tags from OpenCV's Aruco algorithm.
Definition TagDetector.cpp:550
~TagDetector()
Destroy the Tag Detector:: Tag Detector object.
Definition TagDetector.cpp:157
bool InitTorchDetection(const std::string &szModelPath, yolomodel::pytorch::PyTorchInterpreter::HardwareDevices eDevice=yolomodel::pytorch::PyTorchInterpreter::HardwareDevices::eCUDA)
Attempt to open the next available Torch hardware and load model at the given path onto the device.
Definition TagDetector.cpp:578
std::string GetCameraName()
Accessor for the camera name or path that this TagDetector is tied to.
Definition TagDetector.cpp:750
cv::Size GetProcessFrameResolution() const
Accessor for the resolution of the process image used for tag detection.
Definition TagDetector.cpp:763
void UpdateDetectedTags(std::vector< tagdetectutils::ArucoTag > &vNewlyDetectedTags)
Updates the detected torch tags including tracking the detected tags over time and removing tags that...
Definition TagDetector.cpp:787
std::future< bool > RequestLastGoodOverlayFrame(cv::Mat &cvFrame)
Request a copy of a frame containing the last good tag detection overlays from the aruco and torch li...
Definition TagDetector.cpp:523
void DisableTorchDetection()
Set flag to stop tag detection with the torch model.
Definition TagDetector.cpp:639
void ThreadedContinuousCode() override
This code will run continuously in a separate thread. New frames from the given camera are grabbed an...
Definition TagDetector.cpp:176
void PooledLinearCode() override
This method holds the code that is ran in the thread pool started by the ThreadedLinearCode() method....
Definition TagDetector.cpp:408
bool GetIsReady()
Accessor for the status of this TagDetector.
Definition TagDetector.cpp:681
This struct is used by the WaypointHandler to provide an easy way to store all pose data about the ro...
Definition GeospatialOperations.hpp:708