7

I have already found few questions at SO but i am unable to solve this problem using the answers there.

I am new to python. I am having python in Ubuntu 12.04. In my /usr/local/lib, there are two python folders python 2.7 and python 3.2. python 2.7 contains dist-packages and site-packages while python 3.2 contains only dist-packages.

I am trying to run a very simple opencv example with the following code:

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('image.JPG')

kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)

plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()

Error: No module named cv2

Community
  • 1
  • 1
skm
  • 5,015
  • 8
  • 43
  • 104
  • check if your dist_packages/ have cv2.so file in it? – Abid Rahman K Apr 13 '14 at 08:15
  • @AbidRahmanK: i just checked, all of these folders are empty. What should i do now? – skm Apr 13 '14 at 08:19
  • Then I think opencv is not installed successfully. How did you install opencv? – Abid Rahman K Apr 13 '14 at 08:33
  • i installed it long before using terminal. I have been using it for quite a long time but always coded in opencv C++ – skm Apr 13 '14 at 08:40
  • for python, you need to install python version specifically. Simply installing OpenCV won't give you python interface. – Abid Rahman K Apr 13 '14 at 08:42
  • i also tried to run the opencv's sample program for python `(camera.py and contours.py)`, they worked perfect. – skm Apr 13 '14 at 08:42
  • So is it working fine now? – Abid Rahman K Apr 13 '14 at 08:42
  • @AbidRahmanK: what should i do now? How do i install the opencv for python alongside my currect C++ opencv ? – skm Apr 13 '14 at 08:43
  • @AbidRahmanK: I said that those two samples are working but when i tried to run `squares.py`...it gave me error about `import cv2` – skm Apr 13 '14 at 08:44
  • how is that possible? if one of them is working, then should others. http://docs.opencv.org/trunk/doc/py_tutorials/py_setup/py_setup_in_fedora/py_setup_in_fedora.html#install-opencv-python-in-fedora – Abid Rahman K Apr 13 '14 at 08:46
  • oh, i just checked that i ran those two programs using terminal by `pyhton camera.py`and they worked but if i ran those programs using `SPE Stani's python editor`...they also give the error about `import cv2` – skm Apr 13 '14 at 08:47
  • @AbidRahmanK: i think that the problem is in `SPE` which is not recognizing `cv2`...what should i do? – skm Apr 13 '14 at 08:48
  • Then find where is your cv2.so is located. Add path to it in SPE. Or even copying cv2.so to dist_packages should work, I think. – Abid Rahman K Apr 13 '14 at 08:49
  • 1
    `import cv2` `print cv2.__file__` should return path of that file. For example, I got `/usr/local/lib/python2.7/site-packages/cv2.so` in my system. – Abid Rahman K Apr 13 '14 at 08:51
  • in my case, it is located in `/opt/ros/hydro/lib/python2.7/dist-packages` – skm Apr 13 '14 at 08:53
  • how do i add this path to `SPE`? – skm Apr 13 '14 at 08:54
  • 1
    Try this: `import sys` , `sys.path.append('/opt/ros/hydro/lib/python2.7/dist-packages')` , `import cv2`, `print cv2.__version__` – Abid Rahman K Apr 13 '14 at 08:55
  • @AbidRahmanK: Thanks a lot, it worked. But can i get rid of writing it every time? I tried to put in in my `.bashrc` by writing `export PYTHONPATH=/opt/ros/hydro/lib/python2.7/dist-packages:$PYTHONPATH` but again got the same error – skm Apr 13 '14 at 09:01
  • you need to restart the shell. – Abid Rahman K Apr 13 '14 at 09:02
  • I closed my treminal window as well as the SPE IDE and then again started SPE....but i got the same error – skm Apr 13 '14 at 09:03
  • 1
    `import sys` , `print sys.path` in SPE. If your cv2.so directory is not in the list, try copying the cv2.so to any of the directory in that list. – Abid Rahman K Apr 13 '14 at 09:07
  • @AbidRahmanK: Thanks a lot...i copied it and now its working. Do i need to copy only `cv2.so` or something might also require in future? – skm Apr 13 '14 at 09:19
  • I don't think so. While compiling the OpenCV, you have to specify whether it is dynamically linked or not. Normally, I used static linking, so that I need only cv2.so after installation, nothing else. Just try all the examples to make sure it is working. – Abid Rahman K Apr 13 '14 at 09:20
  • If you are OK with this, I will add these comments as answer, and you can accept it to mark this section as solved. – Abid Rahman K Apr 13 '14 at 09:21
  • i think that it will for rest also because the only problem i faced so far was related to `cv2 module` only. So, may be you can write an answer and i will accept it. If i face any other problem, i will start another thread (another problem should be written with suitable title). – skm Apr 13 '14 at 09:30
  • @AbidRahmanK: just a side question...with the above code, my red image is displayed as blue. Seems like rgb is taken as bgr...what could be the problem? – skm Apr 13 '14 at 09:35
  • Because, matplotlib uses RGB format while opencv uses BGR format. http://stackoverflow.com/a/15074748/1134940 – Abid Rahman K Apr 13 '14 at 10:34

3 Answers3

5

NB : This answer is a short compilation of comments above. For more details, please refer to the comments below question.

Background : OP is using SPE Stani's python editor. OP has installed OpenCV /opt/ros/hydro/lib/python2.7/dist-packages which is not detected by the above mentioned editor. Adding this path to PYTHONPATH doesn't solve the issue.

Solution (any one of the below):

  1. Add this path to sys.path and put it in every file.

import sys sys.path.append('/opt/ros/hydro/lib/python2.7/dist-packages')

  1. Copy cv2.so file to any directory in the sys.path.
Abid Rahman K
  • 51,886
  • 31
  • 146
  • 157
0

I am able to resolve this problem of not loading cv2 dll by following below steps on my Win 64 machine.

  1. Download the Unofficial Windows Binaries for Python Extension Packages from gohlke site.
  2. In my case it is opencv_python-3.2.0-cp35-cp35m-win_amd64.whl downloaded it to some folder e.g. c:\py\lib
  3. Install this .whl using below command

    pip install c:\py\lib\opencv_python-3.2.0-cp35-cp35m-win_amd64.whl

  4. Restart the kernel and error gone.
v4vjk
  • 1
0

I had this issue and I fixed it by:

  1. Finding where the openCV library was stored (did this through my IDE).
  2. Deleting it.
  3. Using "pip install opencv-python" again on the terminal in my IDE
  4. Imported as normal

Hopefully this fixes other peoples issues too!

Aaron56
  • 98
  • 2
  • 6