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> RequestDetectedArucoTags(std::vector<tagdetectutils::ArucoTag>& vArucoTags);
76 bool InitTorchDetection(const std::string& szModelPath,
77 yolomodel::pytorch::PyTorchInterpreter::HardwareDevices eDevice = yolomodel::pytorch::PyTorchInterpreter::HardwareDevices::eCUDA);
78
80 // Mutators.
82
83 void EnableTorchDetection(const float fMinObjectConfidence = 0.4f, const float fNMSThreshold = 0.6f);
85 void SetDetectorMaxFPS(const int nRecordingFPS);
86 void SetEnableRecordingFlag(const bool bEnableRecordingFlag);
87
89 // Accessors.
91
92 bool GetIsReady();
93 int GetDetectorMaxFPS() const;
94 bool GetEnableRecordingFlag() const;
95 std::string GetCameraName();
97
98 private:
100 // Declare private methods.
102
103 void ThreadedContinuousCode() override;
104 void PooledLinearCode() override;
105 void UpdateDetectedTags(std::vector<tagdetectutils::ArucoTag>& vNewlyDetectedTags);
106
108 // Declare private member variables.
110 // Class member variables.
111
112 std::shared_ptr<Camera<cv::Mat>> m_pCamera;
113 cv::aruco::ArucoDetector m_cvArucoDetector;
114 cv::aruco::DetectorParameters m_cvArucoDetectionParams;
115 cv::aruco::Dictionary m_cvTagDictionary;
116 std::shared_ptr<yolomodel::pytorch::PyTorchInterpreter> m_pTorchDetector;
117 std::atomic<float> m_fTorchMinObjectConfidence;
118 std::atomic<float> m_fTorchNMSThreshold;
119 std::atomic_bool m_bTorchInitialized;
120 std::atomic_bool m_bTorchEnabled;
121 std::shared_ptr<tracking::MultiTracker> m_pMultiTracker;
122 bool m_bUsingZedCamera;
123 bool m_bUsingGpuMats;
124 bool m_bCameraIsOpened;
125 bool m_bEnableTracking;
126 int m_nNumDetectedTagsRetrievalThreads;
127 std::string m_szCameraName;
128 std::atomic_bool m_bEnableRecordingFlag;
129
130 // Detected tags storage.
131
132 std::vector<tagdetectutils::ArucoTag> m_vNewlyDetectedTags;
133 std::vector<tagdetectutils::ArucoTag> m_vDetectedArucoTags;
134
135 // Rover position for tag geolocalization.
136 geoops::RoverPose m_stRoverPose;
137
138 // Create frames for storing images and point clouds.
139
140 cv::Mat m_cvFrame;
141 cv::cuda::GpuMat m_cvGPUFrame;
142 cv::Mat m_cvArucoProcFrame;
143 cv::Mat m_cvPointCloud;
144 cv::cuda::GpuMat m_cvGPUPointCloud;
145
146 // Queues and mutexes for scheduling and copying data to other threads.
147
148 std::queue<containers::FrameFetchContainer<cv::Mat>> m_qDetectedTagDrawnOverlayFramesCopySchedule;
149 std::queue<containers::DataFetchContainer<std::vector<tagdetectutils::ArucoTag>>> m_qDetectedArucoTagCopySchedule;
150 std::shared_mutex m_muPoolScheduleMutex;
151 std::shared_mutex m_muFrameCopyMutex;
152 std::shared_mutex m_muArucoDataCopyMutex;
153};
154
155#endif
Interface class used to easily multithread a child class.
Definition AutonomyThread.hpp:38
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:645
int GetDetectorMaxFPS() const
Accessor for the desired max FPS for this detector.
Definition TagDetector.cpp:630
void SetDetectorMaxFPS(const int nRecordingFPS)
Mutator for the desired max FPS for this detector.
Definition TagDetector.cpp:561
std::future< bool > RequestDetectionOverlayFrame(cv::Mat &cvFrame)
Request a copy of a frame containing the tag detection overlays from the aruco and tensorflow library...
Definition TagDetector.cpp:431
void SetEnableRecordingFlag(const bool bEnableRecordingFlag)
Mutator for the Enable Recording Flag private member.
Definition TagDetector.cpp:575
void EnableTorchDetection(const float fMinObjectConfidence=0.4f, const float fNMSThreshold=0.6f)
Turn on torch detection with given parameters.
Definition TagDetector.cpp:519
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:458
~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:486
std::string GetCameraName()
Accessor for the camera name or path that this TagDetector is tied to.
Definition TagDetector.cpp:658
cv::Size GetProcessFrameResolution() const
Accessor for the resolution of the process image used for tag detection.
Definition TagDetector.cpp:671
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:695
void DisableTorchDetection()
Set flag to stop tag detection with the torch model.
Definition TagDetector.cpp:547
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:369
bool GetIsReady()
Accessor for the status of this TagDetector.
Definition TagDetector.cpp:589
This struct is used by the WaypointHandler to provide an easy way to store all pose data about the ro...
Definition GeospatialOperations.hpp:677