Solution when you want to use cv_bridge with python3 (virtualenv)

Background

It was when I was messing around with ROS. Since RGB data is sent from the input node of the camera, I wanted to receive it at another node and was trying to use cv_bridge. It looks like below.

from cv_bridge import CvBridge

def prediction(msg):
    bridge = CvBridge()
    img = bridge.imgmsg_to_cv2(msg, "bgr8")

However, when I built it and ran it, I got the following error.

File "/opt/ros/melodic/lib/python2.7/dist-packages/cv_bridge/core.py", line 91, in encoding_to_cvtype2
    from cv_bridge.boost.cv_bridge_boost import getCvType
ImportError: dynamic module does not define module export function (PyInit_cv_bridge_boost)

When I looked it up, it seemed that the cause was that cv_bridge was built with python2. Since my execution environment was python3 (virtualenv), I had to build cv_bridge from the source code.

environment

First way

At first, I tried it by referring to the method of here. If my local python3 is the runtime environment, it might work, but it didn't work in my environment using virtualenv. I will share it for the time being.

$ cd catkin_ws
$ catkin config -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.5m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.5m.so
$ catkin config --install
$ git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv
$ apt-cache show ros-melodic-cv-bridge | grep Version
$ cd src/vision_opencv/
#I think it was probably the lower version. apt-It came out in the cache show.
$ git checkout 1.13.0
$ cd ../../
$ catkin build cv_bridge
$ source install/setup.bash --extend

In my environment using virtualenv, cv_bridge is built successfully, but when I build other nodes of my own with catkin_ws together after building, the following error occurs.

RLException: [sample.launch] is neither a launch file in package [sample_proc] nor is [sample_proc] a launch file name

I'm quite addicted to the swamp from here. Share how you have solved it to reduce casualties.

Final solution

The repository environment of here used cv_bridge with python3 on virtualenv like myself, so I got a hint from that. There are two important things. One is to build cv_bridge in a different workspace than your own. The other is to remove the python2.7 path before the line importing cv_bridge in my script. The specific procedure is as follows.

$ sudo apt install python-catkin-tools python3-dev python3-catkin-pkg-modules python3-numpy python3-yaml ros-melodic-cv-bridge
$ mkdir -p cv_bridge_ws/src
$ git clone https://github.com/ros-perception/vision_opencv.git src/vision_opencv
$ apt-cache show ros-melodic-cv-bridge | grep Version
$ cd src/vision_opencv/
# apt-The version value changes depending on the result of cache show
$ git checkout 1.13.0
$ cd ../../
#Depending on the environment, DPYTHON_LIBRARY aarch64-linux-The gnu part changes
$ catkin config -DCMAKE_BUILD_TYPE=Release -DPYTHON_EXECUTABLE=/usr/bin/python3 -DPYTHON_INCLUDE_DIR=/usr/include/python3.6m -DPYTHON_LIBRARY=/usr/lib/aarch64-linux-gnu/libpython3.6m.so
$ catkin build
$ source devel/setup.bash --extend
$ cd ../catkin_ws
#Build your own node
$ catkin build
$ source devel/setup.bash

Also change your own script.

import sys
sys.path.remove('/opt/ros/melodic/lib/python2.7/dist-packages')
from cv_bridge import CvBridge

def prediction(msg):
    bridge = CvBridge()
    img = bridge.imgmsg_to_cv2(msg, "bgr8")

This will cause python3's cv_bridge to be called at runtime instead of python2's cv_bridge.

Summary

I don't like melodic anymore (laughs). I want to use noetic or ROS2.

Recommended Posts

Solution when you want to use cv_bridge with python3 (virtualenv)
Use aggdraw when you want to draw beautifully with pillow
[Python] When you want to use all variables in another file
I know? Data analysis using Python or things you want to use when you want with numpy
If you want to use field names with hyphens when updating firestore data in python
I want to use MATLAB feval with python
Specify the Python executable to use with virtualenv
I want to use Temporary Directory with Python2
When you want to use it as it is when using it with lambda memo
Gist repository to use when you want to try a little with ansible
[Python] How to use virtualenv
Settings when you want to run python-mecab with travis
When you want to filter with Django REST framework
Site notes to help you use NetworkX with Python
How to build an environment when you want to use python2.7 after installing Anaconda3
Personal best practice template to use when you want to make MVP with Flask
If you want to make a discord bot with python, let's use a framework
[Django] A memorandum when you want to communicate asynchronously [Python3]
[AWS] What to do when you want to pip with Lambda
Python: How to use async with
[Python] I want to use only index when looping a list with a for statement
Knowledge you need to know when programming competitive programming with Python2
Nice to meet you with python
How to use virtualenv with PowerShell
[python] A note when trying to use numpy with Cython
Use rospy with virtualenv in Python3
[Python] I want to use the -h option with argparse
When you want to register Django's initial data with relationships
How to use FTP with Python
When you want to use python2.x on modern Gentoo Linux
I want to debug with Python
When you want to hit a UNIX command on Python
When using MeCab with virtualenv python
[Python] When you want to import and use your own package in the upper directory
[Subprocess] When you want to execute another Python program in Python code
Don't write Python if you want to speed it up with Python
A note on what you did to use Flycheck with Python
I want to use a wildcard that I want to shell with Python remove
When you want to print to standard output with print while testing with pytest
When you want to send an object with requests using flask
When you want to adjust the axis scale interval with APLpy
I want to use jar from python
[Introduction to Python] Let's use foreach with Python
I want to analyze logs with Python
I want to play with aws with python
Until you can use opencv with python
When you want to use multiple versions of the same Python library (virtual environment using venv)
ImportError when trying to use gcloud package with AWS Lambda Python version
If you want to include awsebcli with CircleCI, specify the python version
[Python] To get started with Python, you must first make sure you can use Python.
18 beautiful Python terms you want to read aloud. R18 with example sentences
When you want to replace a column with a missing value (NaN) column by column
Python Note: When you want to know the attributes of an object
If you want to count words in Python, it's convenient to use Counter.
Memorandum of means when you want to make machine learning with 50 images
I want to make a game with Python
When you want to update the chrome driver.
The easiest way to use OpenCV with python
[python] [vscode] When you get angry with space-tab-mixed
I want to use ceres solver from python
I don't want to use -inf with np.log