Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdnn_image_encoder_node.cpp
More file actions
executable file
·242 lines (213 loc) · 9.27 KB
/
Copy pathdnn_image_encoder_node.cpp
File metadata and controls
executable file
·242 lines (213 loc) · 9.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#include "module89/dnn_image_encoder_node.hpp"
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "cv_bridge/cv_bridge.h"
#include "opencv2/dnn.hpp"
#include "opencv2/opencv.hpp"
namespace
{
enum NormalizationTypes
{
kNone,
kUnitScaling,
kPositiveNegative,
kImageNormalization
};
const std::unordered_map<std::string, int32_t> g_str_to_normalization_type({
{"none", NormalizationTypes::kNone},
{"unit_scaling", NormalizationTypes::kUnitScaling},
{"positive_negative", NormalizationTypes::kPositiveNegative},
{"image_normalization", NormalizationTypes::kImageNormalization}}
);
const std::unordered_map<std::string, std::string> g_str_to_image_encoding({
{"rgb8", sensor_msgs::image_encodings::RGB8},
{"bgr8", sensor_msgs::image_encodings::BGR8},
{"mono8", sensor_msgs::image_encodings::MONO8}});
const int32_t kFloat32 = 9; // float data type defined in ROS Tensor message
} // namespace
struct DnnImageEncoderNode::DnnImageEncoderImpl
{
DnnImageEncoderNode * node;
int image_width_;
int image_height_;
std::string image_encoding_;
std::string normalization_type_;
std::string tensor_name_;
bool maintain_aspect_ratio_;
bool center_crop_;
std::vector<double> image_mean_;
std::vector<double> image_stddev_;
void Initialize(DnnImageEncoderNode * encoder_node)
{
node = encoder_node;
image_width_ = node->network_image_width_;
image_height_ = node->network_image_height_;
image_encoding_ = node->network_image_encoding_;
normalization_type_ = node->network_normalization_type_;
tensor_name_ = node->tensor_name_;
maintain_aspect_ratio_ = node->maintain_aspect_ratio_;
center_crop_ = node->center_crop_;
image_mean_ = node->image_mean_;
image_stddev_ = node->image_stddev_;
}
isaac_ros_nvengine_interfaces::msg::TensorList OnCallback(
const sensor_msgs::msg::Image::ConstSharedPtr & image_msg)
{
// Cv bridge conversion and convert the color space
cv_bridge::CvImagePtr image_ptr;
image_ptr = cv_bridge::toCvCopy(image_msg, g_str_to_image_encoding.at(image_encoding_));
// Resize the image to the user specified dimensions
cv::Mat image_resized;
if (maintain_aspect_ratio_) {
const double width_ratio = static_cast<double>(image_msg->width) /
static_cast<double>(image_width_);
const double height_ratio = static_cast<double>(image_msg->height) /
static_cast<double>(image_height_);
cv::Size size;
if (height_ratio < width_ratio) { // Cropping width
const double target_ratio = static_cast<double>(image_width_) /
static_cast<double>(image_height_);
const double crop_height = image_msg->height;
// Make sure the amount cropped is less than or equal to the current width of image
const bool cropped_less = target_ratio * image_msg->height < image_msg->width;
const double crop_width =
(cropped_less) ? target_ratio * image_msg->height : image_msg->width;
cv::Rect cropped_area(
(center_crop_) ? (static_cast<double>(image_msg->width) - crop_width) / 2.0 : 0,
0, crop_width, crop_height);
image_ptr->image = image_ptr->image(cropped_area);
} else { // Cropping height
const double target_ratio = static_cast<double>(image_height_) /
static_cast<double>(image_width_);
const double crop_width = image_msg->width;
// Make sure the amount cropped is less than or equal to the current height of image
const bool cropped_less = target_ratio * image_msg->width < image_msg->height;
const double crop_height =
(cropped_less) ? target_ratio * image_msg->width : image_msg->height;
cv::Rect cropped_area(0,
(center_crop_) ? (static_cast<double>(image_msg->height) - crop_height) / 2.0 : 0,
crop_width, crop_height);
image_ptr->image = image_ptr->image(cropped_area);
}
}
cv::resize(image_ptr->image, image_resized, cv::Size(image_width_, image_height_));
// Normalize tensor depending on normalization type required
switch (g_str_to_normalization_type.at(normalization_type_)) {
case NormalizationTypes::kUnitScaling:
image_resized.convertTo(image_resized, CV_32F, 1.0f / 255.0f);
break;
case NormalizationTypes::kPositiveNegative:
image_resized.convertTo(image_resized, CV_32F, 2.0f / 255.0f, -1.0f);
break;
case NormalizationTypes::kImageNormalization:
image_resized.convertTo(image_resized, CV_32F);
image_resized.forEach<cv::Vec3f>(
[this](cv::Vec3f & pixel, const int *) -> void
{
pixel[0] = (pixel[0] / 255.0f - image_mean_[0]) / image_stddev_[0];
pixel[1] = (pixel[1] / 255.0f - image_mean_[1]) / image_stddev_[1];
pixel[2] = (pixel[2] / 255.0f - image_mean_[2]) / image_stddev_[2];
});
break;
default:
image_resized.convertTo(image_resized, CV_32F);
}
// Convert to NCHW
cv::Mat cv_tensor = cv::dnn::blobFromImage(image_resized);
// Convert CV matrix to ROS2 tensor message
auto tensor_list_msg = isaac_ros_nvengine_interfaces::msg::TensorList();
tensor_list_msg.header = image_msg->header;
tensor_list_msg.tensors.push_back(isaac_ros_nvengine_interfaces::msg::Tensor());
auto & tensor = tensor_list_msg.tensors[0];
tensor.name = tensor_name_;
tensor.data_type = kFloat32;
// Copy the dimensions of the tensor
tensor.shape.rank = cv_tensor.dims;
for (int i = 0; i < cv_tensor.dims; ++i) {
tensor.shape.dims.push_back(cv_tensor.size[i]);
}
// Calculate and copy the strides
tensor.strides.resize(tensor.shape.rank);
tensor.strides[tensor.shape.rank - 1] = sizeof(float);
for (int i = tensor.shape.rank - 2; i >= 0; --i) {
tensor.strides[i] = tensor.shape.dims[i + 1] * tensor.strides[i + 1];
}
// Transfer the CV matrix's data to the ROS2 tensor
tensor.data.resize(cv_tensor.total() * cv_tensor.elemSize());
memcpy(tensor.data.data(), cv_tensor.data, tensor.data.size());
return tensor_list_msg;
}
};
DnnImageEncoderNode::DnnImageEncoderNode(const rclcpp::NodeOptions options)
: Node("dnn_image_encoder_node", options),
// Parameters
network_image_width_(declare_parameter<int>("network_image_width", 224)),
network_image_height_(declare_parameter<int>("network_image_height", 224)),
network_image_encoding_(declare_parameter<std::string>("network_image_encoding", "rgb8")),
maintain_aspect_ratio_(declare_parameter<bool>("maintain_aspect_ratio", false)),
center_crop_(declare_parameter<bool>("center_crop", false)),
image_mean_(declare_parameter<std::vector<double>>("image_mean", {0.5, 0.5, 0.5})),
image_stddev_(declare_parameter<std::vector<double>>("image_stddev", {0.5, 0.5, 0.5})),
tensor_name_(declare_parameter<std::string>("tensor_name", "input")),
network_normalization_type_(declare_parameter<std::string>(
"network_normalization_type", "unit_scaling")),
// Subscriber
image_sub_(create_subscription<sensor_msgs::msg::Image>(
"image",
rclcpp::SensorDataQoS(),
std::bind(&DnnImageEncoderNode::DnnImageEncoderCallback,
this, std::placeholders::_1))),
// Publisher
tensor_pub_(
create_publisher<isaac_ros_nvengine_interfaces::msg::TensorList>(
"encoded_tensor", 1)),
// Impl initialization
impl_(std::make_unique<DnnImageEncoderImpl>())
{
if (g_str_to_image_encoding.find(network_image_encoding_) == g_str_to_image_encoding.end()) {
throw std::runtime_error(
"Error: received unsupported network image encoding: " + network_image_encoding_);
}
if (g_str_to_normalization_type.find(network_normalization_type_) ==
g_str_to_normalization_type.end())
{
throw std::runtime_error(
"Error: received unsupported network image normalization type: " +
network_normalization_type_);
}
if (network_normalization_type_ == "image_normalization" &&
(image_mean_.size() != 3 || image_stddev_.size() != 3))
{
throw std::runtime_error(
"Error: if normalization type is set to Image Normalization, vectors image_mean "
"and image_stddev must have exactly 3 elements");
}
impl_->Initialize(this);
}
void DnnImageEncoderNode::DnnImageEncoderCallback(const sensor_msgs::msg::Image::ConstSharedPtr image_msg)
{
isaac_ros_nvengine_interfaces::msg::TensorList msg;
try {
msg = impl_->OnCallback(image_msg);
} catch (cv_bridge::Exception & e) {
RCLCPP_ERROR(get_logger(), "cv_bridge exception: %s", e.what());
return;
}
msg.header.stamp = image_msg->header.stamp; // Passing through time stamp (to match output tensor with input image)
tensor_pub_->publish(msg);
}
DnnImageEncoderNode::~DnnImageEncoderNode() = default;
// Register as component
#include "rclcpp_components/register_node_macro.hpp"
RCLCPP_COMPONENTS_REGISTER_NODE(DnnImageEncoderNode)
You can’t perform that action at this time.
