0

This question has been asked many times here. but I tried all the answers so far but nothing worked. this is the code I am trying to run.

#!/usr/bin/env python3

import rospy
from sensor_msgs import msg
import cv2
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError


def takes_data_from_camera():
    pub = rospy.Publisher('TestOps/Camera', String, queue_size=10) 
    rate = rospy.Rate(10)
    video_bridge = "AKASH"
    video_out = cv2.imread('cat.jpg')
    gray = cv2.cvtColor(video_out, cv2.COLOR_BGR2GRAY)
    video_bridge = bridge.cv2_to_imgmsg(gray, "passthrough")
    while not rospy.is_shutdown():
        pub.publish(video_bridge)
    
    while (True):
        cv2.imshow('frame',video_out)
        #ret,frame = video.read()
        if cv2.waitKey(1) & 0xFF == ord('a'):
            #video.release()
            cv2.destroyAllWindows()
            break 


if __name__ == '__main__':
    bridge = CvBridge()
    rospy.init_node('Server',anonymous = True)
    takes_data_from_camera()

when I run the code I get the following error.

/bin/python3 /home/akash-j/catkin_ws/src/test_package/src/py-server.py
Traceback (most recent call last):
  File "/home/akash-j/catkin_ws/src/test_package/src/py-server.py", line 5, in <module>
    import cv2
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 129, in <module>
    bootstrap()
  File "/usr/local/lib/python3.8/dist-packages/cv2/__init__.py", line 112, in bootstrap
    import cv2
ImportError: libopencv_hdf.so.4.5: cannot open shared object file: No such file or directory

To give more details about the problem I faced a problem while installing OpenCV and I have installed it multiple times. First, my OpenCV on python was working fine. then I tried to install OpenCV for c++. using the link https://learnopencv.com/install-opencv-4-on-ubuntu-16-04/. even though i could not install some of the step2 on the install os libraries i skipped and followed the steps till step 5. then i could not figure the step2 "how to use opencv on c++". so just stopped there and came back to my python file.now i could not even import opencv on python also.

Akash
  • 91
  • 2
  • 16

1 Answers1

1

Basically as you said you tried installing opencv for c++ and after getting errors you came back... Try removing opencv completely from your system This resource may help. Afterwards install opencv using pip. You can install opencv in a virtual environment to keep both libraries aside. If you have such a usecase.

pip3 install opencv-python  #for python3

pip install opencv-python   #for python2

Reference for installing opencv in python

Then try importing the library in your code and execute your code. Your code will execute successfully and then try installing opencv separately for c++.

And for install opencv for c++, you can refer to this resource also.

Karamdeep Singh
  • 254
  • 2
  • 5
  • i almost removed all the files named opencv. some i could not. it shows the following command. find: ‘/run/user/1001/doc’: Permission denied find: ‘/run/user/1001/gvfs’: Permission denied rm: cannot remove '/usr/include/boost/compute/interop/opencv': Is a directory – Akash Sep 14 '21 at 11:53
  • i have followed the steps you mentioned and it worked well. I have trying to figure it out for 3 days. thanks for the help – Akash Sep 14 '21 at 16:33