Skip to content

Commit

Permalink
added color converter package for semantic seg
Browse files Browse the repository at this point in the history
  • Loading branch information
joel-mb committed Oct 17, 2023
1 parent e412aea commit 65e04c4
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 0 deletions.
Empty file.
80 changes: 80 additions & 0 deletions color_converter/color_converter/color_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright (c) 2023 CVC.
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https:#opensource.org/licenses/MIT>.

import array

import rclpy
import rclpy.node

from sensor_msgs.msg import Image


CITYSCAPES_PALETTE_MAP = {
0: [ 0, 0, 0, 255], # unlabeled
1: [128, 64, 128, 255], # road
2: [244, 35, 232, 255], # sidewalk
3: [ 70, 70, 70, 255], # building
4: [102, 102, 156, 255], # wall
5: [190, 153, 153, 255], # fence
6: [153, 153, 153, 255], # pole
7: [250, 170, 30, 255], # traffic light
8: [220, 220, 0, 255], # traffic sign
9: [107, 142, 35, 255], # vegetation
10: [152, 251, 152, 255], # terrain
11: [ 70, 130, 180, 255], # sky
12: [220, 20, 60, 255], # pedestrian
13: [255, 0, 0, 255], # rider
14: [ 0, 0, 142, 255], # Car
15: [ 0, 0, 70, 255], # truck
16: [ 0, 60, 100, 255], # bus
17: [ 0, 80, 100, 255], # train
18: [ 0, 0, 230, 255], # motorcycle
19: [119, 11, 32, 255], # bicycle
20: [110, 190, 160, 255], # static
21: [170, 120, 50, 255], # dynamic
22: [ 55, 90, 80, 255], # other
23: [ 45, 60, 150, 255], # water
24: [157, 234, 50, 255], # road line
25: [ 81, 0, 81, 255], # ground
26: [150, 100, 100, 255], # bridge
27: [230, 150, 140, 255], # rail track
28: [180, 165, 180, 255] # guard raiL
}


class ColorConverter(rclpy.node.Node):
def __init__(self):
param = rclpy.Parameter("use_sim_time", rclpy.Parameter.Type.BOOL, True)
super().__init__(
"color_converter",
allow_undeclared_parameters=True,
automatically_declare_parameters_from_overrides=True,
parameter_overrides=[param]
)

# _input = self.get_parameter('input').get_parameter_value().string_value
# _output = self.get_parameter('ouput').get_parameter_value().string_value
_input = "/carla/hero/semantic_segmentation/image"
_output = "/carla/hero/semantic_segmentation/cityscapes/image"

self._subscriber = self.create_subscription(Image, _input, self._callback, 10)
self._publisher = self.create_publisher(Image, _output, 1)

def _callback(self, data):
cityscapes = data
for i in range(0, len(cityscapes.data), 4):
cityscapes.data[i:i+4] = array.array('B', CITYSCAPES_PALETTE_MAP.get(cityscapes.data[i+2], CITYSCAPES_PALETTE_MAP[0]))
self._publisher.publish(cityscapes)


def main(args=None):
rclpy.init(args=args)
node = ColorConverter()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()

if __name__ == '__main__':
main()
18 changes: 18 additions & 0 deletions color_converter/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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>color_converter</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="[email protected]">jmoriana</maintainer>
<license>TODO: License declaration</license>

<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>

<export>
<build_type>ament_python</build_type>
</export>
</package>
Empty file.
4 changes: 4 additions & 0 deletions color_converter/setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[develop]
script-dir=$base/lib/color_converter
[install]
install-scripts=$base/lib/color_converter
26 changes: 26 additions & 0 deletions color_converter/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from setuptools import setup

package_name = 'color_converter'

setup(
name=package_name,
version='0.0.0',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml'])
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='jmoriana',
maintainer_email='[email protected]',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'color_converter = color_converter.color_converter:main'
],
},
)
23 changes: 23 additions & 0 deletions color_converter/test/test_copyright.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2015 Open Source Robotics Foundation, 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.

from ament_copyright.main import main
import pytest


@pytest.mark.copyright
@pytest.mark.linter
def test_copyright():
rc = main(argv=['.', 'test'])
assert rc == 0, 'Found errors'
25 changes: 25 additions & 0 deletions color_converter/test/test_flake8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2017 Open Source Robotics Foundation, 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.

from ament_flake8.main import main_with_errors
import pytest


@pytest.mark.flake8
@pytest.mark.linter
def test_flake8():
rc, errors = main_with_errors(argv=[])
assert rc == 0, \
'Found %d code style errors / warnings:\n' % len(errors) + \
'\n'.join(errors)
23 changes: 23 additions & 0 deletions color_converter/test/test_pep257.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2015 Open Source Robotics Foundation, 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.

from ament_pep257.main import main
import pytest


@pytest.mark.linter
@pytest.mark.pep257
def test_pep257():
rc = main(argv=['.', 'test'])
assert rc == 0, 'Found code style errors / warnings'

0 comments on commit 65e04c4

Please sign in to comment.