DavidFernandezChaves / Detectron2_ros

A ROS Node for detecting objects using Detectron2.
MIT License
51 stars 23 forks source link

cv2 import error #2

Closed syc7446 closed 4 years ago

syc7446 commented 4 years ago

Hello,

I believe I followed the instruction on README exactly but I am getting the following error messages. I double-checked that I didn't use the python virtual environment when doing catkin_make. As explained in the instruction, I ran two terminals: one for roscore and the other for running workon detectron2_ros and then roslaunch detectron2_ros detectron2_ros.launch. Could you please help me on this issue?

Traceback (most recent call last):
  File "~/detectron2_ros/src/detectron2_ros/src/detectron2_ros", line 5, in <module>
    import cv2 as cv
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
DavidFernandezChaves commented 4 years ago

Dear syc7446, the problem is that it does not find open cv, and this is sometimes incompatible with ROS. I recommend you to add and compile this version in your ROS repository: https://github.com/ros-perception/vision_opencv/tree/0bbebb73bea65eca856e732b919439853a04986b

syc7446 commented 4 years ago

Thanks for the suggestion. Actually I've already tried that according to this but I am still getting the same error. I can import cv_bridge but not cv2. Could you tell me if I'm missing something here?

Another related question is how would you source two packages (i.e. vision_opencv and detectron2_ros)? If I source vision_opencv using source install/setup.bash --extend first and then source detectron2_ros using source devel/setup.bash, I am losing the effect of sourcing vision_opencv and can't use cv in python 3 anymore. If you could give me a little bit more details on how to do, that'd be greatly appreciated. Thanks!

DavidFernandezChaves commented 4 years ago

I tried to use that but it didn't work, but I haven't configured ROS with Python3, in my case, ROS still uses Python2 except for the cnn node. You can see the file structure I use here: https://github.com/DavidFernandezChaves/ViMantic-Client

In the catkin directory you have to have the CV and the Detectron2_ros package. You compile that with catking_make using a console with the python2 environment. The source is only needed to the catkin directory, not the individual packages. Then, you open a console with the python3 environment as explained and run the roslaunch from there. ROS "magically" works with python2, except for that node which uses python3.

syc7446 commented 4 years ago

I see, thanks! I think I did exactly as you explained but I'm still getting that cv2 error. It shouldn't make a difference but I'm using ros kinetic and the following is what I did after doing catkin_make in the catkin workspace which contains both Detectron2_ros and vision_opencv. As a default, I have source /opt/ros/kinetic/setup.bash in a bashrc file.

  1. I opened the first terminal, and then ran roscore.
  2. I opened the second terminal, and ran workon detectron2_ros. Then, I ran source devel/setup.bash in the root directory of the catkin workspace (doing this first and then running workon detectron2_ros gave me the same result). Running roslaunch detectron2_ros detectron2_ros.launch gave me the cv2 error.

Any thoughts on what I'm missing?

DavidFernandezChaves commented 4 years ago

Well, in my case I have the source in the .bachrc, so I don't have to launch the source. In your case, open the second terminal, do the source, then the work on, and finally the node. But if you say the same thing happens to you it's a problem that python is not properly finding the route to the CV. By discarding options, have you run Detectron2 in the virtual environment without ROS? Did you set up the dependencies just like it says here?: https://github.com/facebookresearch/detectron2/blob/master/INSTALL.md

syc7446 commented 4 years ago

Yes, Detectron2 works when entirely discarding ROS-related paths. However, I managed to get this package working on ROS. I detoured the issue by adding several sys.path.remove() and sys.path.append() in the source code directly, which I don't feel right to do.. Anyways, thanks for the help!