forked from zbw/yiliao2026
把去年的二维码检测、图生文客户端、锥桶检测v8给加了进来,二维码检测新增深度相机检测
This commit is contained in:
75
src/qr_detection/CMakeLists.txt
Normal file
75
src/qr_detection/CMakeLists.txt
Normal file
@@ -0,0 +1,75 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(qr_detection)
|
||||
|
||||
# Default to C99
|
||||
if(NOT CMAKE_C_STANDARD)
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
endif()
|
||||
|
||||
# Default to C++14
|
||||
if(NOT CMAKE_CXX_STANDARD)
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
endif()
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||
add_compile_options(-Wall -Wextra -Wpedantic)
|
||||
endif()
|
||||
|
||||
# Find dependencies
|
||||
find_package(ament_cmake REQUIRED)
|
||||
find_package(rclcpp REQUIRED)
|
||||
find_package(std_msgs REQUIRED)
|
||||
find_package(dnn_node REQUIRED)
|
||||
# find_package(hbm_img_msgs REQUIRED)
|
||||
find_package(sensor_msgs REQUIRED)
|
||||
find_package(ai_msgs REQUIRED)
|
||||
find_package(OpenCV REQUIRED)
|
||||
find_package(geometry_msgs REQUIRED)
|
||||
find_package(rosidl_default_generators REQUIRED)
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(ZBar REQUIRED zbar)
|
||||
|
||||
# rosidl_generate_interfaces(${PROJECT_NAME}
|
||||
# "msg/SampleMessage.msg"
|
||||
# )
|
||||
|
||||
# # Define executables
|
||||
# add_executable(talker src/publisher_hbmem.cpp)
|
||||
# ...
|
||||
|
||||
# add_executable(qr_dete_node src/qr_dete.cpp)
|
||||
# ...
|
||||
|
||||
# install(DIRECTORY
|
||||
# ${PROJECT_SOURCE_DIR}/launch/
|
||||
# DESTINATION share/${PROJECT_NAME}/launch)
|
||||
|
||||
# ---- qr_dete_depth_node (sensor_msgs::Image, no hbmem) ----
|
||||
add_executable(qr_dete_depth_node src/qr_dete_depth.cpp)
|
||||
ament_target_dependencies(qr_dete_depth_node
|
||||
rclcpp
|
||||
std_msgs
|
||||
sensor_msgs
|
||||
geometry_msgs
|
||||
)
|
||||
|
||||
target_include_directories(qr_dete_depth_node PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
${OpenCV_INCLUDE_DIRS} ${ZBar_INCLUDE_DIRS})
|
||||
|
||||
target_link_libraries(qr_dete_depth_node
|
||||
${OpenCV_LIBS} ${ZBar_LIBRARIES}
|
||||
)
|
||||
|
||||
install(TARGETS qr_dete_depth_node
|
||||
DESTINATION lib/${PROJECT_NAME})
|
||||
|
||||
if(BUILD_TESTING)
|
||||
find_package(ament_lint_auto REQUIRED)
|
||||
set(ament_cmake_copyright_FOUND TRUE)
|
||||
set(ament_cmake_cpplint_FOUND TRUE)
|
||||
ament_lint_auto_find_test_dependencies()
|
||||
endif()
|
||||
|
||||
ament_package()
|
||||
37
src/qr_detection/launch/qr_detection.launch.py
Normal file
37
src/qr_detection/launch/qr_detection.launch.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import os
|
||||
|
||||
from launch import LaunchDescription
|
||||
from launch.actions import DeclareLaunchArgument
|
||||
from launch.actions import IncludeLaunchDescription
|
||||
from launch_ros.actions import Node
|
||||
from launch.substitutions import TextSubstitution
|
||||
from launch.substitutions import LaunchConfiguration
|
||||
from launch.launch_description_sources import PythonLaunchDescriptionSource
|
||||
from ament_index_python import get_package_share_directory
|
||||
|
||||
def generate_launch_description():
|
||||
nv12_codec_node = IncludeLaunchDescription(
|
||||
PythonLaunchDescriptionSource(
|
||||
os.path.join(
|
||||
get_package_share_directory('hobot_codec'),
|
||||
'launch/hobot_codec_decode.launch.py')),
|
||||
launch_arguments={
|
||||
# 'codec_channel': '2',
|
||||
'codec_in_mode': 'ros',
|
||||
'codec_out_mode': 'shared_mem',
|
||||
'codec_sub_topic': '/image',
|
||||
'codec_pub_topic': '/hbmem_img'
|
||||
}.items()
|
||||
)
|
||||
|
||||
qr_detection_node = Node(
|
||||
package='qr_detection',
|
||||
executable='qr_dete_node',
|
||||
name='qr_dete_node',
|
||||
output='screen'
|
||||
)
|
||||
|
||||
return LaunchDescription([
|
||||
nv12_codec_node,
|
||||
qr_detection_node
|
||||
])
|
||||
5
src/qr_detection/msg/SampleMessage.msg
Normal file
5
src/qr_detection/msg/SampleMessage.msg
Normal file
@@ -0,0 +1,5 @@
|
||||
int32 index
|
||||
uint64 time_stamp
|
||||
uint8[4194304] data
|
||||
|
||||
uint32 MAX_SIZE=4194304
|
||||
22
src/qr_detection/package.xml
Normal file
22
src/qr_detection/package.xml
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0"?>
|
||||
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
|
||||
<package format="3">
|
||||
<name>qr_detection</name>
|
||||
<version>0.0.0</version>
|
||||
<description>TODO: Package description</description>
|
||||
<maintainer email="root@todo.todo">root</maintainer>
|
||||
<license>TODO: License declaration</license>
|
||||
|
||||
<buildtool_depend>ament_cmake</buildtool_depend>
|
||||
<build_depend>rosidl_default_generators</build_depend>
|
||||
<exec_depend>rosidl_default_runtime</exec_depend>
|
||||
<member_of_group>rosidl_interface_packages</member_of_group>
|
||||
<depend>rclcpp</depend>
|
||||
|
||||
<test_depend>ament_lint_auto</test_depend>
|
||||
<test_depend>ament_lint_common</test_depend>
|
||||
|
||||
<export>
|
||||
<build_type>ament_cmake</build_type>
|
||||
</export>
|
||||
</package>
|
||||
70
src/qr_detection/src/publisher_hbmem.cpp
Normal file
70
src/qr_detection/src/publisher_hbmem.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "qr_detection/msg/sample_message.hpp"
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
class MinimalHbmemPublisher : public rclcpp::Node {
|
||||
public:
|
||||
MinimalHbmemPublisher () : Node("minimal_hbmem_publisher"), count_(0) {
|
||||
// 创建publisher_hbmem,topic为"topic"
|
||||
publisher_ = this->create_publisher_hbmem<qr_detection::msg::SampleMessage>(
|
||||
"topic", rclcpp::SensorDataQoS());
|
||||
|
||||
// 定时器,每隔40毫秒调用一次timer_callback进行消息发送
|
||||
timer_ = this->create_wall_timer(
|
||||
40ms, std::bind(&MinimalHbmemPublisher::timer_callback, this));
|
||||
}
|
||||
|
||||
private:
|
||||
// 定时器回调函数
|
||||
void timer_callback() {
|
||||
// 获取要发送的消息
|
||||
auto loanedMsg = publisher_->borrow_loaned_message();
|
||||
// 判断消息是否可用,可能出现获取消息失败导致消息不可用的情况
|
||||
if (loanedMsg.is_valid()) {
|
||||
// 引用方式获取实际的消息
|
||||
auto& msg = loanedMsg.get();
|
||||
|
||||
// 获取当前时间,单位为us
|
||||
auto time_now =
|
||||
std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch()).count();
|
||||
|
||||
// 对消息的index和time_stamp进行赋值
|
||||
msg.index = count_;
|
||||
msg.time_stamp = time_now;
|
||||
|
||||
// 打印发送消息
|
||||
RCLCPP_INFO(this->get_logger(), "message: %d", msg.index);
|
||||
publisher_->publish(std::move(loanedMsg));
|
||||
// 注意,发送后,loanedMsg已不可用
|
||||
// 计数器加一
|
||||
count_++;
|
||||
} else {
|
||||
// 获取消息失败,丢弃该消息
|
||||
RCLCPP_INFO(this->get_logger(), "Failed to get LoanMessage!");
|
||||
}
|
||||
}
|
||||
|
||||
// 定时器
|
||||
rclcpp::TimerBase::SharedPtr timer_;
|
||||
|
||||
// hbmem publisher
|
||||
rclcpp::PublisherHbmem<qr_detection::msg::SampleMessage>::SharedPtr publisher_;
|
||||
|
||||
// 计数器
|
||||
size_t count_;
|
||||
};
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
rclcpp::init(argc, argv);
|
||||
rclcpp::spin(std::make_shared<MinimalHbmemPublisher>());
|
||||
rclcpp::shutdown();
|
||||
return 0;
|
||||
}
|
||||
290
src/qr_detection/src/qr_dete.cpp
Normal file
290
src/qr_detection/src/qr_dete.cpp
Normal file
@@ -0,0 +1,290 @@
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <zbar.h>
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "std_msgs/msg/int32.hpp"
|
||||
#include "std_msgs/msg/string.hpp"
|
||||
#include "hbm_img_msgs/msg/hbm_msg1080_p.hpp"
|
||||
|
||||
class MinimalHbmemSubscriber : public rclcpp::Node
|
||||
{
|
||||
public:
|
||||
MinimalHbmemSubscriber()
|
||||
: Node("qr_detection"), detect_qr_code_(true) // 默认不检测二维码
|
||||
{
|
||||
// 创建 subscription_hbmem,topic 为 "hbmem_img"
|
||||
subscription_hbmem_ =
|
||||
this->create_subscription_hbmem<hbm_img_msgs::msg::HbmMsg1080P>(
|
||||
"hbmem_img",
|
||||
10,
|
||||
std::bind(&MinimalHbmemSubscriber::hbmem_callback, this, std::placeholders::_1));
|
||||
|
||||
// 创建订阅器,订阅 'sign4return' 话题
|
||||
subscription_sign_ =
|
||||
this->create_subscription<std_msgs::msg::Int32>(
|
||||
"sign4return",
|
||||
10,
|
||||
std::bind(&MinimalHbmemSubscriber::sign_callback, this, std::placeholders::_1));
|
||||
|
||||
// 创建 publisher,topic 为 "qr_results"
|
||||
publisher_ =
|
||||
this->create_publisher<std_msgs::msg::String>("qr_results", 10);
|
||||
}
|
||||
|
||||
private:
|
||||
// 消息回调函数,处理 hbmem_img 话题
|
||||
void hbmem_callback(const hbm_img_msgs::msg::HbmMsg1080P::SharedPtr msg)
|
||||
{
|
||||
if (!detect_qr_code_)
|
||||
{
|
||||
RCLCPP_INFO(this->get_logger(), "QR detection is disabled");
|
||||
return; // 如果标志位为 false,则不进行二维码检测
|
||||
}
|
||||
|
||||
// 创建 Clock 对象并获取当前时间
|
||||
auto clock = std::make_shared<rclcpp::Clock>(RCL_SYSTEM_TIME);
|
||||
auto now = clock->now();
|
||||
|
||||
// 转换消息时间戳为 ROS 时间
|
||||
auto msg_time = rclcpp::Time(msg->time_stamp.sec, msg->time_stamp.nanosec);
|
||||
|
||||
// 计算延时(单位:微秒)
|
||||
auto duration = now - msg_time;
|
||||
auto delay_us = duration.nanoseconds() / 1000; // 转换为微秒
|
||||
|
||||
// 打印延迟
|
||||
RCLCPP_INFO(this->get_logger(), "msg index: %d, time cost %dus", msg->index, delay_us);
|
||||
|
||||
// 获取图像数据并将其转换为 OpenCV 格式
|
||||
cv::Mat image(msg->height, msg->width, CV_8UC1, const_cast<uint8_t*>(msg->data.data()));
|
||||
|
||||
// 初始化 ZBar 扫描器
|
||||
zbar::ImageScanner scanner;
|
||||
scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);
|
||||
|
||||
// 将 OpenCV 图像数据包装成 ZBar 图像
|
||||
zbar::Image zbar_image(image.cols, image.rows, "Y800", image.data, image.cols * image.rows);
|
||||
|
||||
// 扫描图像中的条形码和二维码
|
||||
std::string qr_results;
|
||||
int n = scanner.scan(zbar_image);
|
||||
if (n > 0) {
|
||||
for (auto symbol = zbar_image.symbol_begin(); symbol != zbar_image.symbol_end(); ++symbol) {
|
||||
qr_results += symbol->get_data();
|
||||
RCLCPP_INFO(this->get_logger(), "Decoded %s symbol \"%s\"", symbol->get_type_name().c_str(), symbol->get_data().c_str());
|
||||
}
|
||||
} else {
|
||||
qr_results = "QR Code not detected";
|
||||
RCLCPP_INFO(this->get_logger(), "QR Code not detected");
|
||||
}
|
||||
|
||||
// 发布 QR 结果
|
||||
auto message = std_msgs::msg::String();
|
||||
message.data = qr_results;
|
||||
publisher_->publish(message);
|
||||
}
|
||||
|
||||
// 消息回调函数,处理 sign4return 话题
|
||||
void sign_callback(const std_msgs::msg::Int32::SharedPtr msg)
|
||||
{
|
||||
if (msg->data == 0)
|
||||
{
|
||||
detect_qr_code_ = true; // 启动二维码检测
|
||||
RCLCPP_INFO(this->get_logger(), "QR detection started");
|
||||
}
|
||||
else if (msg->data == 5)
|
||||
{
|
||||
detect_qr_code_ = false; // 停止二维码检测
|
||||
RCLCPP_INFO(this->get_logger(), "QR detection stopped");
|
||||
}
|
||||
}
|
||||
|
||||
// hbmem_img 订阅器
|
||||
rclcpp::SubscriptionHbmem<hbm_img_msgs::msg::HbmMsg1080P>::SharedPtr subscription_hbmem_;
|
||||
// sign4return 订阅器
|
||||
rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr subscription_sign_;
|
||||
// QR code results 发布器
|
||||
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
|
||||
|
||||
// 二维码检测标志位
|
||||
bool detect_qr_code_;
|
||||
};
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
rclcpp::init(argc, argv);
|
||||
rclcpp::spin(std::make_shared<MinimalHbmemSubscriber>());
|
||||
rclcpp::shutdown();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
实现接收和发布
|
||||
*/
|
||||
//#include <memory>
|
||||
//#include <iostream>
|
||||
//#include <opencv2/opencv.hpp>
|
||||
//#include <zbar.h>
|
||||
//#include "rclcpp/rclcpp.hpp"
|
||||
//#include "std_msgs/msg/string.hpp"
|
||||
//#include "hbm_img_msgs/msg/hbm_msg1080_p.hpp"
|
||||
//
|
||||
//class MinimalHbmemSubscriber : public rclcpp::Node
|
||||
//{
|
||||
//public:
|
||||
// MinimalHbmemSubscriber()
|
||||
// : Node("qr_detection")
|
||||
// {
|
||||
// // 创建 subscription_hbmem,topic 为 "hbmem_img"
|
||||
// // 消息回调函数为 topic_callback
|
||||
// subscription_ =
|
||||
// this->create_subscription_hbmem<hbm_img_msgs::msg::HbmMsg1080P>(
|
||||
// "hbmem_img",
|
||||
// 10,
|
||||
// std::bind(&MinimalHbmemSubscriber::topic_callback, this, std::placeholders::_1));
|
||||
//
|
||||
// // 创建 publisher,topic 为 "qr_results"
|
||||
// publisher_ =
|
||||
// this->create_publisher<std_msgs::msg::String>("qr_results", 10);
|
||||
// }
|
||||
//
|
||||
//private:
|
||||
// // 消息回调函数
|
||||
// void topic_callback(const hbm_img_msgs::msg::HbmMsg1080P::SharedPtr msg) const
|
||||
// {
|
||||
// // 创建 Clock 对象并获取当前时间
|
||||
// auto clock = std::make_shared<rclcpp::Clock>(RCL_SYSTEM_TIME);
|
||||
// auto now = clock->now();
|
||||
//
|
||||
// // 转换消息时间戳为 ROS 时间
|
||||
// auto msg_time = rclcpp::Time(msg->time_stamp.sec, msg->time_stamp.nanosec);
|
||||
//
|
||||
// // 计算延时(单位:微秒)
|
||||
// auto duration = now - msg_time;
|
||||
// auto delay_us = duration.nanoseconds() / 1000; // 转换为微秒
|
||||
//
|
||||
// // 打印延迟
|
||||
// RCLCPP_INFO(this->get_logger(), "msg index: %d, time cost %dus", msg->index, delay_us);
|
||||
//
|
||||
// // 获取图像数据并将其转换为 OpenCV 格式
|
||||
// cv::Mat image(msg->height, msg->width, CV_8UC1, const_cast<uint8_t*>(msg->data.data()));
|
||||
//
|
||||
// // 初始化 ZBar 扫描器
|
||||
// zbar::ImageScanner scanner;
|
||||
// scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);
|
||||
//
|
||||
// // 将 OpenCV 图像数据包装成 ZBar 图像
|
||||
// zbar::Image zbar_image(image.cols, image.rows, "Y800", image.data, image.cols * image.rows);
|
||||
//
|
||||
// // 扫描图像中的条形码和二维码
|
||||
// std::string qr_results;
|
||||
// int n = scanner.scan(zbar_image);
|
||||
// if (n > 0) {
|
||||
// for (auto symbol = zbar_image.symbol_begin(); symbol != zbar_image.symbol_end(); ++symbol) {
|
||||
// qr_results += symbol->get_data() ;
|
||||
// RCLCPP_INFO(this->get_logger(), "Decoded %s symbol \"%s\"", symbol->get_type_name().c_str(), symbol->get_data().c_str());
|
||||
// }
|
||||
// } else {
|
||||
// qr_results = "QR Code not detected";
|
||||
// RCLCPP_INFO(this->get_logger(), "QR Code not detected");
|
||||
// }
|
||||
//
|
||||
// // 发布 QR 结果
|
||||
// auto message = std_msgs::msg::String();
|
||||
// message.data = qr_results;
|
||||
// publisher_->publish(message);
|
||||
// }
|
||||
//
|
||||
// // hbmem subscription
|
||||
// rclcpp::SubscriptionHbmem<hbm_img_msgs::msg::HbmMsg1080P>::SharedPtr subscription_;
|
||||
// // QR code results publisher
|
||||
// rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
|
||||
//};
|
||||
//
|
||||
//int main(int argc, char * argv[])
|
||||
//{
|
||||
// rclcpp::init(argc, argv);
|
||||
// rclcpp::spin(std::make_shared<MinimalHbmemSubscriber>());
|
||||
// rclcpp::shutdown();
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
|
||||
/*
|
||||
只实现接收消息,不进行发布
|
||||
*/
|
||||
|
||||
|
||||
//#include <memory>
|
||||
//#include <iostream>
|
||||
//#include <opencv2/opencv.hpp>
|
||||
//#include <zbar.h>
|
||||
//#include "rclcpp/rclcpp.hpp"
|
||||
//#include "hbm_img_msgs/msg/hbm_msg1080_p.hpp"
|
||||
//
|
||||
//class MinimalHbmemSubscriber : public rclcpp::Node
|
||||
//{
|
||||
//public:
|
||||
// MinimalHbmemSubscriber()
|
||||
// : Node("qr_detection")
|
||||
// {
|
||||
// // 创建 subscription_hbmem,topic 为 "hbmem_img"
|
||||
// // 消息回调函数为 topic_callback
|
||||
// subscription_ =
|
||||
// this->create_subscription_hbmem<hbm_img_msgs::msg::HbmMsg1080P>(
|
||||
// "hbmem_img",
|
||||
// 10,
|
||||
// std::bind(&MinimalHbmemSubscriber::topic_callback, this, std::placeholders::_1));
|
||||
// }
|
||||
//private:
|
||||
// // 消息回调函数
|
||||
// void topic_callback(
|
||||
// const hbm_img_msgs::msg::HbmMsg1080P::SharedPtr msg) const
|
||||
// {
|
||||
// // 创建 Clock 对象并获取当前时间
|
||||
// auto clock = std::make_shared<rclcpp::Clock>(RCL_SYSTEM_TIME);
|
||||
// auto now = clock->now();
|
||||
//
|
||||
// // 转换消息时间戳为 ROS 时间
|
||||
// auto msg_time = rclcpp::Time(msg->time_stamp.sec, msg->time_stamp.nanosec);
|
||||
//
|
||||
// // 计算延时(单位:微秒)
|
||||
// auto duration = now - msg_time;
|
||||
// auto delay_us = duration.nanoseconds() / 1000; // 转换为微秒
|
||||
//
|
||||
// // 打印延迟
|
||||
// RCLCPP_INFO(this->get_logger(), "msg index: %d, time cost %dus", msg->index, delay_us);
|
||||
//
|
||||
// // 获取图像数据并将其转换为 OpenCV 格式
|
||||
// cv::Mat image(msg->height, msg->width, CV_8UC1, const_cast<uint8_t*>(msg->data.data()));
|
||||
//
|
||||
// // 初始化 ZBar 扫描器
|
||||
// zbar::ImageScanner scanner;
|
||||
// scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);
|
||||
//
|
||||
// // 将 OpenCV 图像数据包装成 ZBar 图像
|
||||
// zbar::Image zbar_image(image.cols, image.rows, "Y800", image.data, image.cols * image.rows);
|
||||
//
|
||||
// // 扫描图像中的条形码和二维码
|
||||
// int n = scanner.scan(zbar_image);
|
||||
// if (n > 0) {
|
||||
// for (auto symbol = zbar_image.symbol_begin(); symbol != zbar_image.symbol_end(); ++symbol) {
|
||||
// RCLCPP_INFO(this->get_logger(), "Decoded %s symbol \"%s\"", symbol->get_type_name().c_str(), symbol->get_data().c_str());
|
||||
// }
|
||||
// } else {
|
||||
// RCLCPP_INFO(this->get_logger(), "QR Code not detected");
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // hbmem subscription
|
||||
// rclcpp::SubscriptionHbmem<hbm_img_msgs::msg::HbmMsg1080P>::SharedPtr subscription_;
|
||||
//};
|
||||
//
|
||||
//int main(int argc, char * argv[])
|
||||
//{
|
||||
// rclcpp::init(argc, argv);
|
||||
// rclcpp::spin(std::make_shared<MinimalHbmemSubscriber>());
|
||||
// rclcpp::shutdown();
|
||||
// return 0;
|
||||
//}
|
||||
131
src/qr_detection/src/qr_dete_depth.cpp
Normal file
131
src/qr_detection/src/qr_dete_depth.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
#include <memory>
|
||||
#include <iostream>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <zbar.h>
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "std_msgs/msg/int32.hpp"
|
||||
#include "std_msgs/msg/string.hpp"
|
||||
#include "sensor_msgs/msg/image.hpp"
|
||||
|
||||
class MinimalHbmemSubscriber : public rclcpp::Node
|
||||
{
|
||||
public:
|
||||
MinimalHbmemSubscriber()
|
||||
: Node("qr_detection"), detect_qr_code_(true) // 默认检测二维码
|
||||
{
|
||||
// 订阅 /aurora/rgb/image_raw(sensor_msgs::msg::Image 格式)
|
||||
subscription_image_ =
|
||||
this->create_subscription<sensor_msgs::msg::Image>(
|
||||
"/aurora/rgb/image_raw",
|
||||
10,
|
||||
std::bind(&MinimalHbmemSubscriber::image_callback, this, std::placeholders::_1));
|
||||
|
||||
// 创建订阅器,订阅 'sign4return' 话题
|
||||
subscription_sign_ =
|
||||
this->create_subscription<std_msgs::msg::Int32>(
|
||||
"sign4return",
|
||||
10,
|
||||
std::bind(&MinimalHbmemSubscriber::sign_callback, this, std::placeholders::_1));
|
||||
|
||||
// 创建 publisher,topic 为 "qr_results"
|
||||
publisher_ =
|
||||
this->create_publisher<std_msgs::msg::String>("qr_results", 10);
|
||||
}
|
||||
|
||||
private:
|
||||
// 图像回调函数,处理 /aurora/rgb/image_raw 话题
|
||||
void image_callback(const sensor_msgs::msg::Image::SharedPtr msg)
|
||||
{
|
||||
if (!detect_qr_code_)
|
||||
{
|
||||
RCLCPP_INFO(this->get_logger(), "QR detection is disabled");
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建 Clock 对象并获取当前时间
|
||||
auto clock = std::make_shared<rclcpp::Clock>(RCL_SYSTEM_TIME);
|
||||
auto now = clock->now();
|
||||
|
||||
// 转换消息时间戳为 ROS 时间
|
||||
auto msg_time = rclcpp::Time(msg->header.stamp.sec, msg->header.stamp.nanosec);
|
||||
|
||||
// 计算延时(单位:微秒)
|
||||
auto duration = now - msg_time;
|
||||
auto delay_us = duration.nanoseconds() / 1000; // 转换为微秒
|
||||
|
||||
// 打印延迟
|
||||
RCLCPP_INFO(this->get_logger(), "frame_id: %s, time cost %ldus",
|
||||
msg->header.frame_id.c_str(), delay_us);
|
||||
|
||||
// 将 sensor_msgs::Image 转换为 OpenCV 格式(Aurora RGB 为 bgr8 编码,需转为灰度给 ZBar)
|
||||
if (msg->encoding != "bgr8") {
|
||||
RCLCPP_ERROR(this->get_logger(), "Expected bgr8 encoding, got %s", msg->encoding.c_str());
|
||||
return;
|
||||
}
|
||||
cv::Mat rgb_image(msg->height, msg->width, CV_8UC3,
|
||||
const_cast<uint8_t*>(msg->data.data()));
|
||||
cv::Mat gray_image;
|
||||
cv::cvtColor(rgb_image, gray_image, cv::COLOR_BGR2GRAY);
|
||||
|
||||
// 初始化 ZBar 扫描器
|
||||
zbar::ImageScanner scanner;
|
||||
scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);
|
||||
|
||||
// 将 OpenCV 图像数据包装成 ZBar 图像
|
||||
zbar::Image zbar_image(gray_image.cols, gray_image.rows, "Y800",
|
||||
gray_image.data, gray_image.cols * gray_image.rows);
|
||||
|
||||
// 扫描图像中的条形码和二维码
|
||||
std::string qr_results;
|
||||
int n = scanner.scan(zbar_image);
|
||||
if (n > 0) {
|
||||
for (auto symbol = zbar_image.symbol_begin();
|
||||
symbol != zbar_image.symbol_end(); ++symbol) {
|
||||
qr_results += symbol->get_data();
|
||||
RCLCPP_INFO(this->get_logger(), "Decoded %s symbol \"%s\"",
|
||||
symbol->get_type_name().c_str(), symbol->get_data().c_str());
|
||||
}
|
||||
} else {
|
||||
qr_results = "QR Code not detected";
|
||||
RCLCPP_INFO(this->get_logger(), "QR Code not detected");
|
||||
}
|
||||
|
||||
// 发布 QR 结果
|
||||
auto message = std_msgs::msg::String();
|
||||
message.data = qr_results;
|
||||
publisher_->publish(message);
|
||||
}
|
||||
|
||||
// 消息回调函数,处理 sign4return 话题
|
||||
void sign_callback(const std_msgs::msg::Int32::SharedPtr msg)
|
||||
{
|
||||
if (msg->data == 0)
|
||||
{
|
||||
detect_qr_code_ = true; // 启动二维码检测
|
||||
RCLCPP_INFO(this->get_logger(), "QR detection started");
|
||||
}
|
||||
else if (msg->data == 5)
|
||||
{
|
||||
detect_qr_code_ = false; // 停止二维码检测
|
||||
RCLCPP_INFO(this->get_logger(), "QR detection stopped");
|
||||
}
|
||||
}
|
||||
|
||||
// /aurora/rgb/image_raw 订阅器
|
||||
rclcpp::Subscription<sensor_msgs::msg::Image>::SharedPtr subscription_image_;
|
||||
// sign4return 订阅器
|
||||
rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr subscription_sign_;
|
||||
// QR code results 发布器
|
||||
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
|
||||
|
||||
// 二维码检测标志位
|
||||
bool detect_qr_code_;
|
||||
};
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
rclcpp::init(argc, argv);
|
||||
rclcpp::spin(std::make_shared<MinimalHbmemSubscriber>());
|
||||
rclcpp::shutdown();
|
||||
return 0;
|
||||
}
|
||||
50
src/qr_detection/src/qr_detection_node.cpp
Normal file
50
src/qr_detection/src/qr_detection_node.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <memory>
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "hbm_img_msgs/msg/hbm_msg1080_p.hpp"
|
||||
|
||||
class MinimalHbmemSubscriber : public rclcpp::Node
|
||||
{
|
||||
public:
|
||||
MinimalHbmemSubscriber()
|
||||
: Node("qr_detection")
|
||||
{
|
||||
// 创建 subscription_hbmem,topic 为 "hbmem_img"
|
||||
// 消息回调函数为 topic_callback
|
||||
subscription_ =
|
||||
this->create_subscription_hbmem<hbm_img_msgs::msg::HbmMsg1080P>(
|
||||
"hbmem_img",
|
||||
10,
|
||||
std::bind(&MinimalHbmemSubscriber::topic_callback, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
private:
|
||||
// 消息回调函数
|
||||
void topic_callback(
|
||||
const hbm_img_msgs::msg::HbmMsg1080P::SharedPtr msg) const
|
||||
{
|
||||
// 创建 Clock 对象并获取当前时间
|
||||
auto clock = std::make_shared<rclcpp::Clock>(RCL_SYSTEM_TIME);
|
||||
auto now = clock->now();
|
||||
|
||||
// 转换消息时间戳为 ROS 时间
|
||||
auto msg_time = rclcpp::Time(msg->time_stamp.sec, msg->time_stamp.nanosec);
|
||||
|
||||
// 计算延时(单位:微秒)
|
||||
auto duration = now - msg_time;
|
||||
auto delay_us = duration.nanoseconds() / 1000; // 转换为微秒
|
||||
|
||||
// 打印延迟
|
||||
RCLCPP_INFO(this->get_logger(), "msg index: %d, time cost %dus", msg->index, delay_us);
|
||||
}
|
||||
|
||||
// hbmem subscription
|
||||
rclcpp::SubscriptionHbmem<hbm_img_msgs::msg::HbmMsg1080P>::SharedPtr subscription_;
|
||||
};
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
rclcpp::init(argc, argv);
|
||||
rclcpp::spin(std::make_shared<MinimalHbmemSubscriber>());
|
||||
rclcpp::shutdown();
|
||||
return 0;
|
||||
}
|
||||
36
src/qr_detection/src/sameple_zbar.cpp
Normal file
36
src/qr_detection/src/sameple_zbar.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
#include <zbar.h>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc != 2) {
|
||||
std::cerr << "Usage: " << argv[0] << " <image_path>" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
std::string image_path = argv[1];
|
||||
cv::Mat image = cv::imread(image_path, cv::IMREAD_GRAYSCALE);
|
||||
if (image.empty()) {
|
||||
std::cerr << "Could not open or find the image: " << image_path << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Initialize ZBar scanner
|
||||
zbar::ImageScanner scanner;
|
||||
scanner.set_config(zbar::ZBAR_NONE, zbar::ZBAR_CFG_ENABLE, 1);
|
||||
|
||||
// Wrap OpenCV image data in a ZBar image
|
||||
zbar::Image zbar_image(image.cols, image.rows, "Y800", image.data, image.cols * image.rows);
|
||||
|
||||
// Scan the image for barcodes and QR codes
|
||||
int n = scanner.scan(zbar_image);
|
||||
if (n > 0) {
|
||||
for (auto symbol = zbar_image.symbol_begin(); symbol != zbar_image.symbol_end(); ++symbol) {
|
||||
std::cout << "Decoded " << symbol->get_type_name() << " symbol \"" << symbol->get_data() << '"' << std::endl;
|
||||
}
|
||||
} else {
|
||||
std::cout << "QR Code not detected" << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
45
src/qr_detection/src/subscriber_hbmem.cpp
Normal file
45
src/qr_detection/src/subscriber_hbmem.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <memory>
|
||||
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "qr_detection/msg/sample_message.hpp"
|
||||
|
||||
class MinimalHbmemSubscriber : public rclcpp::Node {
|
||||
public:
|
||||
MinimalHbmemSubscriber () : Node("minimal_hbmem_subscriber") {
|
||||
// 创建subscription_hbmem,topic为"sample"
|
||||
// 消息回调函数为topic_callback
|
||||
subscription_ =
|
||||
this->create_subscription_hbmem<qr_detection::msg::SampleMessage>(
|
||||
"topic", rclcpp::SensorDataQoS(),
|
||||
std::bind(&MinimalHbmemSubscriber ::topic_callback, this,
|
||||
std::placeholders::_1));
|
||||
}
|
||||
|
||||
private:
|
||||
// 消息回调函数
|
||||
void topic_callback(
|
||||
const qr_detection::msg::SampleMessage::SharedPtr msg) const {
|
||||
// 注意,msg只能在回调函数中使用,回调函数返回后,该消息就会被释放
|
||||
// 获取当前时间
|
||||
auto time_now =
|
||||
std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
// 计算延时并打印出来
|
||||
RCLCPP_INFO(this->get_logger(), "msg %d, time cost %dus", msg->index,
|
||||
time_now - msg->time_stamp);
|
||||
}
|
||||
|
||||
// hbmem subscription
|
||||
rclcpp::SubscriptionHbmem<qr_detection::msg::SampleMessage>::SharedPtr
|
||||
subscription_;
|
||||
};
|
||||
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
rclcpp::init(argc, argv);
|
||||
rclcpp::spin(std::make_shared<MinimalHbmemSubscriber>());
|
||||
rclcpp::shutdown();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user