Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After install ROS Kinetic, cannot import OpenCV

I have first installed openCV from source using this script. When I tested it was working well.

After I installed ROS kinetic, and open python3 and run import cv2, got the following error:

Python 3.5.2 (default, Nov 17 2016, 17:05:23)  [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import cv2 Traceback (most recent call last):   File "<stdin>", line 1, in <module> ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type 
like image 863
Alex Avatar asked Mar 25 '17 17:03

Alex


People also ask

Does ROS have OpenCV?

ROS provides a very simple integration with OpenCV, the most widely used open source Computer Vision library. However, it does not ship a specific Debian for ROS kinetic because that would force users to use a single version.


2 Answers

It looks like this problem is caused by ROS adding /opt/ros/kinetic/lib/python2.7/dist-packages to the python path. This actually happens when you activate ROS with the command source /opt/ros/kinetic/setup.bash. This line is often added at the end of your bashrc file, in /home/username/.bashrc.

A workaround is to remove this line from the bashrc file. This way the python3 opencv packages will be correctly used, and you can still run source /opt/ros/kinetic/setup.bash to use ROS. However, this does mean you cannot use ROS and python3 from the same environment.

Hopefully someone can come up with a better answer, but this should work until then.

like image 186
Paul Avatar answered Sep 22 '22 10:09

Paul


If you are working with anaconda, activate the environment you want to work from, and remove the culprit from sys.path.

To do so, open a python3 console, from which:

>>> import sys >>> print(sys.path) 

You will see several path, among which you should notice:

'/opt/ros/kinetic/lib/python2.7/dist-packages' 

Then remove it:

>>> sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages') 

Tested with python3.5 on anaconda3 with locally compiled opencv. This is likely applicable to virtualenvs as well.

For a permanent solution, remove the path '/opt/ros/kinetic/lib/python2.7/dist-packages' from ~/.bashrc as mentioned in @Paul's answer.

like image 40
calocedrus Avatar answered Sep 21 '22 10:09

calocedrus