Galaxis Codebase  1.0.0
Team Galaxis software stack for the Carolo Cup 2023 - ROS2 based software
QRCodeDetectionNode Class Reference

#include <QRCodeDetectionNode.hpp>

Inheritance diagram for QRCodeDetectionNode:
Collaboration diagram for QRCodeDetectionNode:

Public Member Functions

 QRCodeDetectionNode ()
 
void test_detection_loop (std::string path)
 
void onImgCallback (const sensor_msgs::msg::Image::SharedPtr p_ImgMsg)
 

Private Member Functions

bool checkBoundingBox (cv::Mat &bbox, cv::Mat &image)
 
void drawBox (cv::Mat &im, cv::Mat &bbox)
 
void publishQR (rclcpp::Time stamp, bool detected)
 
void publishImage (cv::Mat &image)
 

Private Attributes

bool m_qrCodeVisible = false
 
cv::QRCodeDetector m_detector
 
cv_bridge::CvImage m_imgBridge
 
std::string m_settingsJsonFilename
 
int m_publisherSeqNumber = 0
 
rclcpp::Publisher< QRCodeDetectType >::SharedPtr m_qrCodeDetectionPubSPtr
 
rclcpp::Subscription< sensor_msgs::msg::Image >::SharedPtr m_imgSubscriber
 
int m_foundQRCount = 0
 
int m_foundQRCountForSendingMessage = 10
 
bool m_qrCodeFoundSometime = false
 

Additional Inherited Members

- Protected Member Functions inherited from GalaxisNode
 GalaxisNode ()=delete
 
 GalaxisNode (std::string p_name, unsigned int p_heartbeatTimePeriod)
 
 GalaxisNode (std::string p_name)
 
template<typename MsgSPtr >
void tickSub (std::map< galaxis::serviceStates::State, std::function< void(const MsgSPtr)>> const *p_functionMapPtr, MsgSPtr const p_message)
 
template<typename... Args>
void tickPub (std::map< galaxis::serviceStates::State, std::function< void(Args...)>> const *p_functionMapPtr, Args... p_args)
 
void register_callback (std::function< void(galaxis::serviceStates::State)> p_function)
 
void service_state_callback (ServiceStateType::SharedPtr const p_msgSPtr)
 
galaxis::serviceStates::State get_current_service_state () const
 
void enable_parameters (std::function< rcl_interfaces::msg::SetParametersResult(const std::vector< rclcpp::Parameter > &)> callbackFunc)
 
void create_parameters (std::shared_ptr< json > configJsonSPtr)
 
json const & get_global_settings ()
 

Detailed Description

Definition at line 17 of file QRCodeDetectionNode.hpp.

Constructor & Destructor Documentation

◆ QRCodeDetectionNode()

QRCodeDetectionNode::QRCodeDetectionNode ( )

Definition at line 33 of file QRCodeDetectionNode.cpp.

33  : GalaxisNode("QRCodeDetection") {
34  this->m_detector = cv::QRCodeDetector();
35 
36  galaxis::ros::create_publisher(this, m_qrCodeDetectionPubSPtr);
37 
38  auto qosReliable = galaxis::ros::get_qos_profile(galaxis::ros::QoS_Profile_t::QOS_RELIABLE);
39  // rclcpp::QoS qosBestEffort = galaxis::ros::get_qos_profile(galaxis::ros::QoS_Profile_t::QOS_BEST_EFFORT);
40  m_imgSubscriber = this->create_subscription<sensor_msgs::msg::Image>(
41  galaxis::ros::TOPIC_CAMERA_RAW,
42  qosReliable,
43  std::bind(&QRCodeDetectionNode::onImgCallback, this, std::placeholders::_1));
44  // m_debugImgPublisher = this->create_publisher<sensor_msgs::msg::Image>("/galaxis/qrcodedetection/debug/img", qosBestEffort);
45 
46  // std::string test_path = "./nodes/QRCodeDetectorNode/test_video.mp4";
47  // std::thread(&QRCodeDetectionNode::test_detection_loop, this, test_path).detach();
48 }

References m_detector, m_imgSubscriber, m_qrCodeDetectionPubSPtr, and onImgCallback().

Here is the call graph for this function:

Member Function Documentation

◆ checkBoundingBox()

bool QRCodeDetectionNode::checkBoundingBox ( cv::Mat &  bbox,
cv::Mat &  image 
)
private

Definition at line 143 of file QRCodeDetectionNode.cpp.

143  {
144  // allowed deviation
145  float percentage = 0.3;
146  cv::Point2i topLeft = cv::Point2i(bbox.at<float>(0,0),bbox.at<float>(0,1));
147  cv::Point2i topRight = cv::Point2i(bbox.at<float>(1,0),bbox.at<float>(1,1));
148  cv::Point2i botRight = cv::Point2i(bbox.at<float>(2,0),bbox.at<float>(2,1));
149  cv::Point2i botLeft = cv::Point2i(bbox.at<float>(3,0),bbox.at<float>(3,1));
150  // std::cout << "Difference: " << std::abs(topLeft.y - topRight.y) << " Target: " << percentage * image.rows << std::endl;
151  // std::cout << "Difference: " << std::abs(topRight.x - botRight.x) << " Target: " << percentage * image.cols << std::endl;
152  // std::cout << "Difference: " << std::abs(botRight.y - botLeft.y) << " Target: " << percentage * image.rows << std::endl;
153  // std::cout << "Difference: " << std::abs(botLeft.x - topLeft.x) << " Target: " << percentage * image.cols << std::endl;
154 
155  return std::abs(topLeft.y - topRight.y) < percentage * image.rows&&
156  std::abs(topRight.x - botRight.x) < percentage * image.cols&&
157  std::abs(botRight.y - botLeft.y) < percentage * image.rows&&
158  std::abs(botLeft.x - topLeft.x) < percentage * image.cols;
159 }

Referenced by onImgCallback(), and test_detection_loop().

Here is the caller graph for this function:

◆ drawBox()

void QRCodeDetectionNode::drawBox ( cv::Mat &  im,
cv::Mat &  bbox 
)
private

Definition at line 161 of file QRCodeDetectionNode.cpp.

161  {
162  int n = bbox.rows;
163  for(int i = 0 ; i < n ; i++){
164  cv::line(
165  image,
166  cv::Point2i(bbox.at<float>(i,0),bbox.at<float>(i,1)),
167  cv::Point2i(bbox.at<float>((i+1) % n,0), bbox.at<float>((i+1) % n,1)), cv::Scalar(255,0,0),
168  3
169  );
170  }
171 }

Referenced by onImgCallback(), and test_detection_loop().

Here is the caller graph for this function:

◆ onImgCallback()

void QRCodeDetectionNode::onImgCallback ( const sensor_msgs::msg::Image::SharedPtr  p_ImgMsg)

Definition at line 50 of file QRCodeDetectionNode.cpp.

50  {
51  cv::Mat boundingBox;
52  cv::Mat image = cv_bridge::toCvShare(p_ImgMsg, "bgr8")->image;
53 
54  bool foundQR = this->m_detector.detect(image, boundingBox);
55 
56  // check if bounding box is a clean rectangle
57  foundQR = foundQR && this->checkBoundingBox(boundingBox, image);
58  if(foundQR){
59  this->drawBox(image, boundingBox);
60  }
61 
62  // Publish results
63  // this->publishImage(image); // enable for debug
64  if(foundQR && m_foundQRCount <= m_foundQRCountForSendingMessage * 2){
66  }
67  else if(!foundQR && m_foundQRCount >= 0) {
69  }
70 
72  log("Sending message: QR Code found.");
73 
74  m_qrCodeFoundSometime = true;
75  this->publishQR(p_ImgMsg->header.stamp, true);
76  }
77  else {
78  log("Sending message: QR Code NOT found.");
79  this->publishQR(p_ImgMsg->header.stamp, false);
80 
82  log("Node stopped.");
83  std::this_thread::sleep_for(std::chrono::milliseconds(250));
84  std::exit(0);
85  }
86  }
87 }

References checkBoundingBox(), drawBox(), galaxis::utils::log(), m_detector, m_foundQRCount, m_foundQRCountForSendingMessage, m_qrCodeFoundSometime, and publishQR().

Referenced by QRCodeDetectionNode().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ publishImage()

void QRCodeDetectionNode::publishImage ( cv::Mat &  image)
private

◆ publishQR()

void QRCodeDetectionNode::publishQR ( rclcpp::Time  stamp,
bool  detected 
)
private

Definition at line 125 of file QRCodeDetectionNode.cpp.

125  {
126  std::unique_ptr<QRCodeDetectType> qrMsgUPtr = std::make_unique<QRCodeDetectType>();
127  qrMsgUPtr->header.stamp = stamp;
128  qrMsgUPtr->detected = detected;
129 
130  m_qrCodeDetectionPubSPtr->publish(std::move(qrMsgUPtr));
131 }

References m_qrCodeDetectionPubSPtr.

Referenced by onImgCallback(), and test_detection_loop().

Here is the caller graph for this function:

◆ test_detection_loop()

void QRCodeDetectionNode::test_detection_loop ( std::string  path)

Definition at line 89 of file QRCodeDetectionNode.cpp.

89  {
90  cv::VideoCapture cap = cv::VideoCapture(path);
91  if(!cap.isOpened()){
92  log_error("Error openning video stream or file");
93  }
94  cv::Mat frame, boundingBox;
95  int frameCounter = 0;
96 
97  while(cap.isOpened()){
98  bool ret = cap.read(frame);
99  frameCounter++;
100 
101  // reset video to beginning if end has been reached
102  if(frameCounter == cap.get(cv::CAP_PROP_FRAME_COUNT)){
103  frameCounter = 0;
104  cap.set(cv::CAP_PROP_POS_FRAMES, 0);
105  }
106  if(!ret){continue;}
107 
108  bool foundQR = m_detector.detect(frame, boundingBox);
109  // check if bounding box is a clean rectangle
110  foundQR = foundQR && this->checkBoundingBox(boundingBox, frame);
111 
112  if(foundQR){
113  // draw bounding box on frame
114  drawBox(frame, boundingBox);
115  cv::imshow("detection", frame);
116  cv::waitKey(1);
117  // this->publishImage(frame);
118  }
119  // publish detection
120  this->publishQR(this->get_clock()->now(), foundQR);
121  }
122  cap.release();
123 }

References checkBoundingBox(), drawBox(), galaxis::utils::log_error(), m_detector, and publishQR().

Here is the call graph for this function:

Member Data Documentation

◆ m_detector

cv::QRCodeDetector QRCodeDetectionNode::m_detector
private

Definition at line 20 of file QRCodeDetectionNode.hpp.

Referenced by onImgCallback(), QRCodeDetectionNode(), and test_detection_loop().

◆ m_foundQRCount

int QRCodeDetectionNode::m_foundQRCount = 0
private

Definition at line 31 of file QRCodeDetectionNode.hpp.

Referenced by onImgCallback().

◆ m_foundQRCountForSendingMessage

int QRCodeDetectionNode::m_foundQRCountForSendingMessage = 10
private

Definition at line 32 of file QRCodeDetectionNode.hpp.

Referenced by onImgCallback().

◆ m_imgBridge

cv_bridge::CvImage QRCodeDetectionNode::m_imgBridge
private

Definition at line 21 of file QRCodeDetectionNode.hpp.

◆ m_imgSubscriber

rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr QRCodeDetectionNode::m_imgSubscriber
private

Definition at line 29 of file QRCodeDetectionNode.hpp.

Referenced by QRCodeDetectionNode().

◆ m_publisherSeqNumber

int QRCodeDetectionNode::m_publisherSeqNumber = 0
private

Definition at line 26 of file QRCodeDetectionNode.hpp.

◆ m_qrCodeDetectionPubSPtr

rclcpp::Publisher<QRCodeDetectType>::SharedPtr QRCodeDetectionNode::m_qrCodeDetectionPubSPtr
private

Definition at line 27 of file QRCodeDetectionNode.hpp.

Referenced by publishQR(), and QRCodeDetectionNode().

◆ m_qrCodeFoundSometime

bool QRCodeDetectionNode::m_qrCodeFoundSometime = false
private

Definition at line 34 of file QRCodeDetectionNode.hpp.

Referenced by onImgCallback().

◆ m_qrCodeVisible

bool QRCodeDetectionNode::m_qrCodeVisible = false
private

Definition at line 19 of file QRCodeDetectionNode.hpp.

◆ m_settingsJsonFilename

std::string QRCodeDetectionNode::m_settingsJsonFilename
private

Definition at line 23 of file QRCodeDetectionNode.hpp.


The documentation for this class was generated from the following files:
QRCodeDetectionNode::m_qrCodeFoundSometime
bool m_qrCodeFoundSometime
Definition: QRCodeDetectionNode.hpp:34
galaxis::utils::log_error
void log_error(const Args &... args)
Definition: logger.hpp:50
QRCodeDetectionNode::m_foundQRCountForSendingMessage
int m_foundQRCountForSendingMessage
Definition: QRCodeDetectionNode.hpp:32
QRCodeDetectionNode::m_qrCodeDetectionPubSPtr
rclcpp::Publisher< QRCodeDetectType >::SharedPtr m_qrCodeDetectionPubSPtr
Definition: QRCodeDetectionNode.hpp:27
QRCodeDetectionNode::m_detector
cv::QRCodeDetector m_detector
Definition: QRCodeDetectionNode.hpp:20
QRCodeDetectionNode::m_imgSubscriber
rclcpp::Subscription< sensor_msgs::msg::Image >::SharedPtr m_imgSubscriber
Definition: QRCodeDetectionNode.hpp:29
QRCodeDetectionNode::checkBoundingBox
bool checkBoundingBox(cv::Mat &bbox, cv::Mat &image)
Definition: QRCodeDetectionNode.cpp:143
GalaxisNode::GalaxisNode
GalaxisNode()=delete
QRCodeDetectionNode::m_foundQRCount
int m_foundQRCount
Definition: QRCodeDetectionNode.hpp:31
QRCodeDetectionNode::onImgCallback
void onImgCallback(const sensor_msgs::msg::Image::SharedPtr p_ImgMsg)
Definition: QRCodeDetectionNode.cpp:50
QRCodeDetectionNode::drawBox
void drawBox(cv::Mat &im, cv::Mat &bbox)
Definition: QRCodeDetectionNode.cpp:161
galaxis::utils::log
void log(const Args &... args)
Definition: logger.hpp:33
QRCodeDetectionNode::publishQR
void publishQR(rclcpp::Time stamp, bool detected)
Definition: QRCodeDetectionNode.cpp:125