Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: No module named cv2

Tags:

python

opencv

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

like image 262
skm Avatar asked Apr 13 '14 08:04

skm


1 Answers

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.
like image 74
Abid Rahman K Avatar answered Sep 29 '22 20:09

Abid Rahman K