0
$\begingroup$

Summary:

I'm using new vision of opencv, opencv 4.2 with ubuntu 16.04 and python 2.7

After built cv_bridge, the output is:

import rospkg
ImportError: No module named rospkg

Here's the more detailed version: here

I tried to build cv_bridge in order to run OpenCV 4.2, Ubuntu 16.04 (ROS Kinetic) and python 2.7.

I will explain what I do for that.

I built it before with OpenCV 3.3.1 in order to use GPU and it was very fine and it is still working now. To best my knowledge, there is ros-kinetic-opencv3, it is working only on CPU. I removed it using sudo apt remove ros-kinetic-opencv3

After removing, I just downloaded your here and put in my ~/catkin_ws/src/, then making ~/catkin_ws/catkin_make on my PC without any modifications. I think that I was lucky.

Now, I'd like use some advantage of OpenCV 4.2 that is not with older versions like OpenCV 3.3.1. Meanwhile, I can't update robot's system, it is Ubuntu 16.04.

So, I tried to build cv_bridge, all modifications as following:

I copied and pasted module_opencv3.cpp, then change its name to module_opencv4.cpp so that became three models 4, 3 and 2.cpp instead of two 3 and 2.cpp.

Next , Sreevan mentioned here some changes, I did like Sreevan:

In cv_bridge/src/module_opencv3.cpp change signature of two functions

3.1) UMatData* allocate(int dims0, const int* sizes, int type, void* data, size_t* step, int flags, UMatUsageFlags usageFlags) const to UMatData* allocate(int dims0, const int* sizes, int type, void* data, size_t* step, AccessFlag flags, UMatUsageFlags usageFlags) const

3.2) bool allocate(UMatData* u, int accessFlags, UMatUsageFlags usageFlags) const to bool allocate(UMatData* u, AccessFlag accessFlags, UMatUsageFlags usageFlags) const

Next, I went to this file ~/catkin_ws/src/vision_opencv/cv_bridge$ CMakeLists.txt and change it as: from:

find_package(OpenCV 3 REQUIRED
  COMPONENTS
    opencv_core
    opencv_imgproc
    opencv_imgcodecs
  PATHS /usr/local
  NO_DEFAULT_PATH
  CONFIG
)

to:

if ( NOT (OpenCV_VERSION_MAJOR VERSION_LESS 4) )
find_package(OpenCV REQUIRED
  COMPONENTS
    opencv_core
    opencv_imgproc
    opencv_imgcodecs
  #PATHS /usr/local/include
  PATHS /usr/local
  NO_DEFAULT_PATH
  CONFIG
)
else()
find_package(OpenCV 3 REQUIRED
  COMPONENTS
    opencv_core
    opencv_imgproc
    opencv_imgcodecs
  PATHS /usr/local
  NO_DEFAULT_PATH
  CONFIG
)
endif()

Next, I went to this file ~/catkin_ws/src/vision_opencv/cv_bridge/src$ CMakeLists.txt and change it as: from:

if (OpenCV_VERSION_MAJOR VERSION_EQUAL 3)
  add_library(${PROJECT_NAME}_boost module.cpp module_opencv3.cpp)
else()
  add_library(${PROJECT_NAME}_boost module.cpp module_opencv2.cpp)
endif()

to

if (OpenCV_VERSION_MAJOR VERSION_EQUAL 4)
  add_library(${PROJECT_NAME}_boost module.cpp module_opencv4.cpp)
elseif(OpenCV_VERSION_MAJOR VERSION_EQUAL 3)
  add_library(${PROJECT_NAME}_boost module.cpp module_opencv3.cpp)
else()
  add_library(${PROJECT_NAME}_boost module.cpp module_opencv2.cpp)
endif()

Next, I think this step is not important, I went to this file ~/catkin_ws/src/vision_opencv/cv_bridge$ package.xml and change any opencv3 to opencv4

Finally, ~/catkin_ws$ catkin_make

everything was ok but when I run the code:

import rospy
import cv2
import numpy as np
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError

print(cv2.__version__)

class CVControl:
  def __init__(self):
    self.bridge = CvBridge()
    self.image_sub = rospy.Subscriber("/camera/rgb/image_raw", Image, self.img_callback)
  def img_callback(self, data):
    try:
      cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
      print e

    frame = cv_image
    cv2.imshow("Image window", frame)
    cv2.waitKey(3)
def main():
  ctrl = CVControl()
  rospy.init_node('image_converter')
  try:
    rospy.spin()

  except KeyboardInterrupt:
    print "Shutting down"
    cv2.destroyAllWindows()

if __name__ == '__main__':
  main()

It runs by OpenCV 3.3.1

If I need to run it by OpenCV 4.2 I added those lines in first the code:

import sys
ros_path2 = '/usr/local/lib/python2.7/site-packages'
ros_path3 = '/usr/lib/python2.7/dist-packages'
if  ros_path2 and ros_path3 in sys.path:
  sys.path.remove(ros_path2)
  sys.path.remove(ros_path3)

The output is:

import rospkg
ImportError: No module named rospkg

What is wrong?

$\endgroup$
1
$\begingroup$

Please try this. It would install rospkg to your computer. (I have faced this issue too in the past, and this would most likely solve it)

sudo apt-get install python-rospkg

| improve this answer | |
$\endgroup$
  • $\begingroup$ No, this command will install ros-kinetic-opencv3 that it runs on CPU. $\endgroup$ – Redhwan Jun 4 at 0:16
  • $\begingroup$ Are you using a virtual environment? If yes, then use pip install python-rospkg to install this to the end. This would ensure rospkg, ros, opencv all use the same python installation. $\endgroup$ – Ayush Gupta Jun 4 at 6:02
0
$\begingroup$

Python 2.7 has been sunsetted.

From the official documentation:

What will happen if I did not upgrade by January 1st, 2020?

If people find catastrophic security problems in Python 2, or in software written in Python 2, then most volunteers will not help fix them. If you need help with Python 2 software, then many volunteers will not help you, and over time fewer and fewer volunteers will be able to help you. You will lose chances to use good tools because they will only run on Python 3, and you will slow down people who depend on you and work with you.

Some of these problems started on January 1. Other problems will grow over time.

(emphasis added)

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thanks for your advice, I know that but I have to use it few months until finish my current work. The main issue for this question is between OpenCV 3 and 4. $\endgroup$ – Redhwan Jun 5 at 1:12
  • $\begingroup$ @Redhwan - You say that, but the error appears to be about ROS. What does ROS have to do with this? In your other post you stated, I removed [ROS] using sudo apt remove ros-kinetic-opencv3 - so, you removed ROS, and now you're getting an error that ROS doesn't exist... I'm not sure what you expected is going to happen or what this has to do with OpenCV. $\endgroup$ – Chuck Jun 5 at 13:44
  • $\begingroup$ @ Chuck-the main idea is ROS has a OpenCV version, for example, Ubuntu 16.04 ...> kinetic .....>opencv3.3.1 and Ubuntu 18.04 ...> Melodic.....>opencv3.x and so on. You can see here wiki.ros.org/Distributions. So, if we need to use the O-penCV version is independent on ROS, we need to build a cv_bridge that compiles new OpenCV with ROS. ROS has many libraries and OpenCV is one of them. $\endgroup$ – Redhwan Jun 6 at 5:03
  • $\begingroup$ @Redhwan - I understand you need to build a bridge, but what I'm saying is that it looks like you're having a ROS issue, notably that rospkg is not installed, and not an OpenCV issue. $\endgroup$ – Chuck Jun 8 at 12:47
  • $\begingroup$ @ Chuck--when we install ROS, we install rospkg, ros-xxxxxx-opencv, cv_bridge,.... but we remove ros-xxxxxx-opencv that also removes rospkg and cv_bridge in order to use a different OpenCV version. The issue is incompatibility. $\endgroup$ – Redhwan Jun 9 at 0:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.