Skip to content

Commit

Permalink
fix: move rle into utils
Browse files Browse the repository at this point in the history
Signed-off-by: badai-nguyen <dai.nguyen@tier4.jp>
  • Loading branch information
badai-nguyen committed Jul 26, 2024
1 parent 10bfceb commit c22d052
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 22 deletions.
1 change: 1 addition & 0 deletions perception/autoware_tensorrt_yolox/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ rclcpp_components_register_node(yolox_single_image_inference_node
)

ament_auto_add_library(${PROJECT_NAME}_node SHARED
src/utils.cpp
src/tensorrt_yolox_node.cpp
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class TrtYoloXNode : public rclcpp::Node

public:
explicit TrtYoloXNode(const rclcpp::NodeOptions & node_options);
std::vector<std::pair<uint8_t, int>> rle_compress(const cv::Mat & mask);

private:
void onConnect();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2024 TIER IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef AUTOWARE__TENSORRT_YOLOX__UTILS_HPP_
#define AUTOWARE__TENSORRT_YOLOX__UTILS_HPP_
#include <opencv2/opencv.hpp>

namespace autoware::tensorrt_yolox
{
std::vector<std::pair<uint8_t, int>> runLengthEncoder(const cv::Mat & mask);
cv::Mat runLengthDecoder(const std::vector<uint8_t> & rle_data, const int rows, const int cols);
} // namespace autoware::tensorrt_yolox

#endif // AUTOWARE__TENSORRT_YOLOX__UTILS_HPP_
23 changes: 2 additions & 21 deletions perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "autoware/tensorrt_yolox/tensorrt_yolox_node.hpp"

#include "autoware/tensorrt_yolox/utils.hpp"
#include "object_recognition_utils/object_classification.hpp"

#include <autoware_perception_msgs/msg/object_classification.hpp>
Expand Down Expand Up @@ -126,25 +127,6 @@ void TrtYoloXNode::onConnect()
}
}

std::vector<std::pair<uint8_t, int>> TrtYoloXNode::rle_compress(const cv::Mat & image)
{
std::vector<std::pair<uint8_t, int>> compressed_data;
const int rows = image.rows;
const int cols = image.cols;
compressed_data.emplace_back(image.at<uint8_t>(0, 0), 0);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
uint8_t current_value = image.at<uint8_t>(i, j);
if (compressed_data.back().first == current_value) {
++compressed_data.back().second;
} else {
compressed_data.emplace_back(current_value, 1);
}
}
}
return compressed_data;
}

void TrtYoloXNode::onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg)
{
stop_watch_ptr_->toc("processing_time", true);
Expand Down Expand Up @@ -196,14 +178,13 @@ void TrtYoloXNode::onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg)
overlapSegmentByRoi(yolox_object, mask, width, height);
}
}
// Compress mask to RLE format
if (trt_yolox_->getMultitaskNum() > 0) {
sensor_msgs::msg::Image::SharedPtr out_mask_msg =
cv_bridge::CvImage(std_msgs::msg::Header(), sensor_msgs::image_encodings::MONO8, mask)
.toImageMsg();
out_mask_msg->header = msg->header;

std::vector<std::pair<uint8_t, int>> compressed_data = TrtYoloXNode::rle_compress(mask);
std::vector<std::pair<uint8_t, int>> compressed_data = runLengthEncoder(mask);

Check warning on line 187 in perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp#L187

Added line #L187 was not covered by tests
int step = sizeof(uint8_t) + sizeof(int);
out_mask_msg->data.resize(static_cast<int>(compressed_data.size()) * step);

Check warning on line 189 in perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp#L189

Added line #L189 was not covered by tests
for (size_t i = 0; i < compressed_data.size(); ++i) {
Expand Down
64 changes: 64 additions & 0 deletions perception/autoware_tensorrt_yolox/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2024 Tier IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "autoware/tensorrt_yolox/utils.hpp"

namespace autoware::tensorrt_yolox
{

std::vector<std::pair<uint8_t, int>> runLengthEncoder(const cv::Mat & image)

Check warning on line 20 in perception/autoware_tensorrt_yolox/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/utils.cpp#L20

Added line #L20 was not covered by tests
{
std::vector<std::pair<uint8_t, int>> compressed_data;
const int rows = image.rows;
const int cols = image.cols;
compressed_data.emplace_back(image.at<uint8_t>(0, 0), 0);

Check warning on line 25 in perception/autoware_tensorrt_yolox/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/utils.cpp#L23-L25

Added lines #L23 - L25 were not covered by tests
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
uint8_t current_value = image.at<uint8_t>(i, j);

Check warning on line 28 in perception/autoware_tensorrt_yolox/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/utils.cpp#L28

Added line #L28 was not covered by tests
if (compressed_data.back().first == current_value) {
++compressed_data.back().second;

Check warning on line 30 in perception/autoware_tensorrt_yolox/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/utils.cpp#L30

Added line #L30 was not covered by tests
} else {
compressed_data.emplace_back(current_value, 1);

Check warning on line 32 in perception/autoware_tensorrt_yolox/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/utils.cpp#L32

Added line #L32 was not covered by tests
}
}
}
return compressed_data;

Check warning on line 36 in perception/autoware_tensorrt_yolox/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/utils.cpp#L36

Added line #L36 was not covered by tests
}

cv::Mat runLengthDecoder(const std::vector<uint8_t> & rle_data, const int rows, const int cols)

Check warning on line 39 in perception/autoware_tensorrt_yolox/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/utils.cpp#L39

Added line #L39 was not covered by tests
{
cv::Mat mask(rows, cols, CV_8UC1, cv::Scalar(0));

Check warning on line 41 in perception/autoware_tensorrt_yolox/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/utils.cpp#L41

Added line #L41 was not covered by tests
int idx = 0;
int step = sizeof(uint8_t) + sizeof(int);
int nb_pixels = 0;
for (size_t i = 0; i < rle_data.size(); i += step) {
uint8_t value;
int length;
std::memcpy(&value, &rle_data[i], sizeof(uint8_t));
std::memcpy(&length, &rle_data[i + sizeof(uint8_t)], sizeof(int));

Check warning on line 49 in perception/autoware_tensorrt_yolox/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/utils.cpp#L49

Added line #L49 was not covered by tests
nb_pixels += length;
for (int j = 0; j < length; ++j) {
int row_idx = static_cast<int>(idx / cols);
int col_idx = static_cast<int>(idx % cols);
mask.at<uint8_t>(row_idx, col_idx) = value;
idx++;

Check warning on line 55 in perception/autoware_tensorrt_yolox/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/utils.cpp#L52-L55

Added lines #L52 - L55 were not covered by tests
if (idx > rows * cols) {
break;
}
}
}
return mask;

Check warning on line 61 in perception/autoware_tensorrt_yolox/src/utils.cpp

View check run for this annotation

Codecov / codecov/patch

perception/autoware_tensorrt_yolox/src/utils.cpp#L61

Added line #L61 was not covered by tests
}

} // namespace autoware::tensorrt_yolox

0 comments on commit c22d052

Please sign in to comment.