run python code with ros melodic
Robot Operating Systems (ROS) are widely used in robotics. but, available separately ROS distribution Can cause software version conflicts.
Each Ubuntu version comes with a different ROS distribution. For example, Ubuntu 18.04 uses ROS Melodic, based on Python 2.7. Ubuntu 20.04 uses ROS Noetic, which is based on Python 3.
Often, our ROS master needs to be an older version of ROS for specific hardware driver support. In that case, using modern Python3-based software libraries – such as OpenCV 4, Tensorflow, and PyTorch – causes problems.
In this article, I will show you how to put together a ROS Melodic master on the same machine with a recent ROS distribution. We can do this by containerizing our software modules using Docker.
Docker is currently the industry standard for containerizing software. It is like a virtual machine but more lightweight as each container uses the host’s operating system.
“A Docker container image is a lightweight, standalone, executable package of software that contains everything needed to run an application: code, runtime, system tools, system libraries, and settings.” 
In a Docker container, we can install whatever ROS distribution and software libraries we want to use – without causing problems for our host system.
As an example use case in this article, we will use Ubuntu 18.04 with ROS Melodic as our master. For demonstration purposes, we will publish a webcam image topic. Using Docker, we will build and run a container with ROS Noetic and OpenCV 4 for computer vision tasks.
With the setup shown in the figure below, ROS Melodic nodes can co-exist and communicate with ROS Noetic nodes using Docker.
--network host Installation.
If you don’t already have a catkin workspace on your computer, create one with the following commands ,
mkdir -p ~/catkin_ws/src
First of all, we should start ROS Melodic Master on our host system. to do this type
roscore in a new terminal.
Next, we publish a webcam stream using the ROS package
cv_camera , To clone a package from GitHub, build the package with catkin_make and
cv_camera_nodeExecute the following commands:
git clone https://github.com/OTL/cv_camera.git
rosrun cv_camera cv_camera_node
Now you should be able to see the image theme
For our mockup computer vision node, we will create the following three files in the folder
To build and run a Docker container, we need to write a Dockerfile. Dockerfile is like a blueprint. This tells Docker how to build the image. Docker uses images to build and run containers.
in a Dockerfile We start with a base-image
FROM and then install all the required dependencies
RUN apt-get update && apt-get install,
For our ROS application, we need to create a catkin workspace , Then, we need to install cv_bridge from Vision_opencv  package with
RUN git clone,
# install packages with apt-get
RUN apt-get update && apt-get install -y --no-install-recommends \
libopencv-dev python3-opencv \
&& rm -rf /var/lib/apt/lists/*
# create and build a catkin_ws
SHELL ["/bin/bash", "-c", "-l"]
RUN mkdir src && source "/opt/ros/$ROS_DISTRO/setup.bash" && catkin_make
# get ROS packages from github
RUN git clone -b noetic https://github.com/ros-perception/vision_opencv.git
RUN source /home/catkin_ws/devel/setup.bash && catkin_make
# copy our scripts
COPY ./cv_node.py .
COPY ./start_ros_node.sh .
cv_nodeThe purpose is to subscribe to our webcam topic, do something with the image, and then publish a new image topic. To convert between ROS images and OpenCV images, we use cv_bridge.
The following code will subscribe to the webcam topic, append the text with OpenCV 4, and publish the new image to a new topic.
from sensor_msgs.msg import Image, CompressedImage
from cv_bridge import CvBridge
self.bridge = CvBridge()
image_sub = rospy.Subscriber("/cv_camera/image_raw", Image, self.callback, queue_size=1)
self.image_pub = rospy.Publisher("/cv_camera/image_out/compressed", CompressedImage, queue_size=1)
def callback(self, data):
cv_img = self.bridge.imgmsg_to_cv2(data)
cv_img = cv2.putText(cv_img, "Published with ROS Noetic", (30,30), cv2.FONT_HERSHEY_SIMPLEX, 1.1, (255,0,0), 2)
cv_img = self.bridge.cv2_to_compressed_imgmsg(cv_img)
node = cv_node()
rate = rospy.Rate(30) # frequency in Hz
while not rospy.is_shutdown():
if __name__ == '__main__':
This node can be adapted to perform more advanced functions. For example, we can use a deep neural network for object detection
If you want to import other libraries – for example, PyTorch or Tensorflow – be sure to install them in the Dockerfile
RUN pip install [...]
This script is the entry point to our Docker container. This simply starts our ROS node
To build the Docker image, execute the following command in Terminal:
docker build -t medium .
This will run the Dockerfile in the current directory and create a Docker image with tag medium.
After the build process is finished, run the following command in the terminal:
docker run -it --network host medium:latest
This will run the latest container with tag medium in interactive mode. Also, it will use the host’s network so that the Docker container and our ROS master can communicate.
this much only! You should now see a new topic /cv_camera/image_out/compressed on the host system. you can check
cv_node use command
rosnode info cv_node,