5

I tried everything in the virtual environment, but again and again "no module named opencv". I checked the site packages folder and found that there is no cv2 folder, like I found numpy, pip folders; but there was 1 folder named:

cv2-1.0-py2.7.egg-info

Now what should I do? Open CV 3.0 is not compatible with python 3.5.1 while there is this problem hindering me again and again for python 2.7. Please help!!

P.S. None of the solutions mentioned in the previous answers worked :/

Screenshot of Site-Packages

8 Answers8

4

cv2-1.0 is a spurious, garbage module with nothing in it.

See for yourself: https://pypi.python.org/pypi/cv2/1.0

Once installed, cv2-1.0 will mask opencv and keep it from working.

... so delete it:

rm -rf /usr/local/lib/python2.7/dist-packages/cv2-1.0*

If you're trying to import cv2, you of course ACTUALLY want the opencv python module.

  • On ubuntu, this won't be available from pip-- you'll need to install a system package:

    apt-get install python-opencv

  • If you're using virtualenv, you'll need to symlink opencv into your virtualenv manually.

    cd venv/lib/python2.7 && ln -s /usr/local/lib/python2.7/dist-packages/cv2.so
    cd venv/lib/python2.7 && ln -s /usr/local/lib/python2.7/dist-packages/cv.py
    
  • It's unlikely, but sometimes pip will encounter a package that depends on cv2. Without action, this (or running pip install cv2 -- not recommended!) will install the imposter cv2-1.0 package from pypi. To placate pip, make a metadata file that describes the system's cv2 installation to pip:

    #/usr/local/lib/python2.7/dist-packages/cv2-2.4.8.egg-info
    Metadata-Version: 1.2
    Name: cv2
    Version: 2.4.8
    

    The version number in the filename and in the file should match the major version in apt-cache show python-opencv.

A note on python-opencv

If you build the the opencv debian package from source, you'll end up with opencv-python, which describes fewer dependencies (notably it's missing opencv-libs), and I don't use it. Perhaps someone can enlighten us as to why all of this is such a mess.

brentiumbrent
  • 415
  • 5
  • 12
2

When installing OpenCV to a virtual environment, I also found that cv2.so was missing. The fix is to copy it manually: cp /home/user/opencv/lib/cv2.so /home/user/venv/lib/python2.7/site-packages/cv2.so.

So for completeness, the following worked for me:

git clone https://github.com/Itseez/opencv
cd /home/user/opencv
mkdir release && cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE \
   -D WITH_QT=ON \
   -D WITH_OPENMP=ON \
   -D CMAKE_BUILD_TYPE=RELEASE \
   -D CMAKE_INSTALL_PREFIX=/home/user/venv/local \
   -D PYTHON2_PACKAGES_PATH=/home/user/venv/local/lib/python2.7/site-packages \
   -D INSTALL_C_EXAMPLES=ON \
   -D INSTALL_PYTHON_EXAMPLES=ON \
   -D BUILD_EXAMPLES=ON ..
make
make install
cp /home/user/opencv/lib/cv2.so /home/user/venv/lib/python2.7/site-packages/cv2.so
hjweide
  • 11,893
  • 9
  • 45
  • 49
1

Try this inside your venv. It will work

pip install opencv-python

Nishank Lakkakula
  • 376
  • 1
  • 2
  • 7
0

I have been stucked on the message "no module named opencv" for a while on opencv installation on OSX in a virtualenv with pip.

Two workarounds which works on my settings were:

1 - Bypass the virtualenv structure and add the syspath of opencv directly in the python code (not elegant way)

import sys
sys.path.append('/usr/local/lib/python3.4/site-packages')

or

2 - Make usage of conda instead of pip for opencv installation

conda install -c https://conda.binstar.org/menpo opencv

more info here (how could we install opencv on anaconda?)

Community
  • 1
  • 1
aberna
  • 5,594
  • 2
  • 28
  • 33
0

The solution that worked for me was to install the cv2wrap package using pip:

pip install cv2wrap

Luca
  • 1,270
  • 1
  • 18
  • 34
0

Its worked for me(in yours virtual env):

brew install opencv
fuwiak
  • 721
  • 1
  • 8
  • 25
-1

If you're using MacOS, the instructions detailed below and taken from here can help (Thanks Daniel Shiffman & David Haylock!). In case that link dies, this an alternate location. The post by brentiumbrent explains why the install error occurs but is written in Linux so you can't use the apt-get commands if you're on MacOS. The post by casper touches on the use of CMAKE, but if you don't have it installed and/or never used it before, you'll likely get errors when running it in the terminal...

Anyway, I just copied the HTML over from that first site. Click "run snippit" and you'll get a halfway decent version. Click on one of the links above to get all the same information but better formatted. It presents all the same information but in better format (I just copied the HTML over).

At the end of the instructions, it doesn't explicitely say so, but you need to follow casper's lead, and copy the ...opencv/SharedLibs/lib/cv2.so file to your directory of python packages: ...lib/python2.7/site-packages/. Based on the advice found here, I installed the cv2.so file from the SharedLibs folder instead of the StaticLibs folder and it worked.

<div class="entry-content">
  <h2>Overview</h2>
  <p style="margin-top:-30px">Despite the wealth of information on the internet, installation guides for openCV are far and few between. Whilst we have used openCV packages in previous projects they have always been wrapped in an addon format, which obviously makes it easier to
    use but, for a forthcoming project we needed the ability to access the library directly. Briefly, openCV is a library of functions which mainly focuses on image analysis, processing and evaluation. In lamen’s terms, it allows computers to process
    and understand images and other forms of visual data.</p>
  <p>In this post we will explain how we managed to work around the mysterious installation process, and provide a simple set of instructions that will enable you to install, build and use the openCV libraries and binaries on your system.</p>
  <p>First you will need:</p>
  <ul>
    <li>Mac OSX Yosemite 10.10</li>
    <li><a href="https://itunes.apple.com/gb/app/xcode/id497799835?mt=12" target="_blank">XCode</a>
    </li>
    <li>Command Line Tools (This is done from inside XCode)</li>
    <li><a href="http://www.cmake.org/download/" target="_blank">CMake</a>
    </li>
  </ul>
  <p>Ready … lets begin!
  </p>
  <h2>Building OpenCV</h2>
  <h2>Update:</h2>
  <p style="margin-top:-30px">
    Having rebuilt openCV using the XCode again the makefile for some reason is not generated, will try and find out why. For now use the Terminal method as outlined below.
    <br>
    <strong>Attention: </strong>Steps 2 a and b document methods of installing both the static and shared libraries.
  </p>
  <p><strong>Step 1:</strong>
    <br>Download <a href="http://opencv.org/downloads.html" target="_blank">openCV</a> and unzip it somewhere on your computer. Create two new folders inside of the openCV directory, one called StaticLibs and the other SharedLibs.</p>
  <p><strong>Step 2a: Build the Static Libraries with Terminal.</strong>
    <br>To build the libraries in Terminal.
    <br>
    <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.17.51.png">
      <img class="aligncenter size-full wp-image-1118" alt="Screen Shot 2014-10-23 at 10.17.51" src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.17.51.png" width="1920" height="1201">
    </a>
  </p>
  <ul>
    <li>Open CMake.</li>
    <li style="margin-top: 5px;">Click Browse Source and navigate to your openCV folder.</li>
    <li style="margin-top: 5px;">Click Browse Build and navigate to your StaticLib Folder.</li>
    <li style="margin-top: 5px;">Click the configure button. You will be asked how you would like to generate the files. Choose Unix-Makefile from the Drop Down menu and Click OK. CMake will perform some tests and return a set of red boxes appear in the CMake Window.</li>
  </ul>
  <p>
    <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.24.01.png">
      <img class="aligncenter size-full wp-image-1118" alt="Screen Shot 2014-10-23 at 10.24.01" src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-10.24.01.png" width="1920" height="1201">
    </a>
    <br>You will need to uncheck and add to the following options.</p>
  <ul>
    <li>Uncheck <code>BUILD_SHARED_LIBS</code>
    </li>
    <li>Uncheck <code>BUILD_TESTS</code>
    </li>
    <li>Add an SDK path to <code>CMAKE_OSX_SYSROOT</code>, it will look something like this “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk”.</li>
    <li>Add x86_64 to <code>CMAKE_OSX_ARCHITECTURES</code>, this tells it to compile against the current system</li>
    <li>Uncheck <code>WITH_1394</code>
    </li>
    <li>Uncheck <code>WITH_FFMPEG</code>
    </li>
  </ul>
  <p>Click Configure again, then Click Generate.</p>
  <p>When the application has finished generating, Open Terminal and type the following commands.</p>
  <pre>- cd &lt;path/to/your/opencv/staticlibs/folder/&gt;
- make (This will take awhile)
- sudo make install</pre>
  <p>Enter your password.
    <br>This will install the static libraries on your computer.</p>
  <p><strong>Step 2c: Build the Shared Libraries with Terminal.</strong>
  </p>
  <ul>
    <li>Open CMake.</li>
    <li style="margin-top: 5px;">Click Browse Source and navigate to your openCV folder.</li>
    <li style="margin-top: 5px;">Click Browse Build and navigate to your SharedLib Folder.</li>
    <li style="margin-top: 5px;">Click the configure button. You will be asked how you would like to generate the files. Choose Unix-Makefile from the Drop Down menu and Click OK. CMake will perform some tests and return a set of red boxes appear in the CMake Window.</li>
  </ul>
  <p>You will need to uncheck and add to the following options.</p>
  <ul>
    <li>Check <code>BUILD_SHARED_LIBS</code>
    </li>
    <li>Uncheck <code>BUILD_TESTS</code>
    </li>
    <li>Add an SDK path to <code>CMAKE_OSX_SYSROOT</code>, it will look something like this “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk”.</li>
    <li>Add x86_64 to <code>CMAKE_OSX_ARCHITECTURES</code>, this tells it to compile against the current system</li>
    <li>Uncheck <code>WITH_1394</code>
    </li>
    <li>Uncheck <code>WITH_FFMPEG</code>
    </li>
  </ul>
  <p>Click Configure again, then Click Generate.</p>
  <p>When the application has finished generating, Open Terminal.</p>
  <pre>- cd &lt;path/to/your/opencv/SharedLibs/folder/&gt;
- make (This will take awhile)
- sudo make install</pre>
  <p>Enter your password.
    <br>This will install the shared libraries on your computer.</p>
  <h2>Make an Application </h2>
  <p style="margin-top:-30px">
    This is a very basic example, but the similar principles can be applied to other code.
    <br>For this post, lets make an application that shows two images, one normal and one that has been put through a blur filter.
    <br>
    <strong> Step 1: </strong>
  </p>
  <ul>
    <li>Create a new folder somewhere on the computer.</li>
    <li>Inside the folder, create a CMakeLists.txt file then create a BlurImage.cpp file.</li>
    <li>Then add an image file.</li>
  </ul>
  <p style="text-align: center;">For this example I’ll use this fruity number.
    <br>
    <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/fruits.jpg">
      <img class="aligncenter  wp-image-1180" alt="fruits" src="http://blogs.wcode.org/wp-content/uploads/2014/10/fruits.jpg" width="307" height="288">
    </a>
  </p>
  <p><strong>Step 2:</strong>
  </p>
  <p>Open the BlurImage.cpp in your favourite text editor and add the following text.</p>
  <pre>#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

Mat src; Mat dst;
char window_name1[] = "Unprocessed Image";
char window_name2[] = "Processed Image";

int main( int argc, char** argv )
{
    /// Load the source image
    src = imread( argv[1], 1 );

    namedWindow( window_name1, WINDOW_AUTOSIZE );
    imshow("Unprocessed Image",src);

    dst = src.clone();
    GaussianBlur( src, dst, Size( 15, 15 ), 0, 0 );

    namedWindow( window_name2, WINDOW_AUTOSIZE );
    imshow("Processed Image",dst);

    waitKey();
    return 0;
}</pre>
  <p>Save the file.</p>
  <p><strong>Step 3:&nbsp;</strong>
  </p>
  <p>Open the CMakeLists.txt file then add the following text.</p>
  <pre>cmake_minimum_required(VERSION 2.8)
project( BlurImage )
find_package( OpenCV )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( BlurImage BlurImage.cpp )
target_link_libraries( BlurImage ${OpenCV_LIBS} )
</pre>
  <p>Save the File.</p>
  <p><strong>Step 4:&nbsp;</strong>
    <br>Open Terminal and navigate to your applications directory.</p>
  <pre>- cd &lt;path/to/application/folder&gt;
- /Applications/CMake.app/Contents/bin/cmake .
- make
</pre>
  <p>This will generate both the makefile and the executable file.</p>
  <p>Then type <code>./BlurImage image.jpg </code>
  </p>
  <p>
    <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.44.56.png">
      <img src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.44.56.png" alt="Screen Shot 2014-10-23 at 13.44.56" width="1920" height="1201" class="aligncenter size-full wp-image-1184">
    </a>
  </p>
  <p>
    <a href="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.45.52.png">
      <img src="http://blogs.wcode.org/wp-content/uploads/2014/10/Screen-Shot-2014-10-23-at-13.45.52.png" alt="Screen Shot 2014-10-23 at 13.45.52" width="1920" height="1196" class="aligncenter size-full wp-image-1185">
    </a>
  </p>
  <p>Yay … it works all you need to do is add your own .cpp file and alter the .txt file. Then follow the above commands.</p>
  <p>For more detailed examples go to the <a href="http://docs.opencv.org/doc/tutorials/tutorials.html" style="color: #920;" target="_blank">openCV Tutorial Page</a>, or check out the sample folder inside the opencv folder.</p>
  <p>Huge thank you to Daniel Shiffman, whose guide put us on the right track : <a style="color: #920;" href="http://shiffman.net/2011/01/23/how-to-build-opencv-static-libraries-mac-os-x/" target="_blank">http://shiffman.net/2011/01/23/how-to-build-opencv-static-libraries-mac-os-x/</a>
  </p>
</div>
Community
  • 1
  • 1
Afflatus
  • 2,302
  • 5
  • 25
  • 40
-1

I found that once I activated the virtual environment, I had to select the virtual environment's Python Interpreter from the Command Palette and then it was able to locate OpenCV. Apparently, I hadn't selected any of the Python interpreters prior to this.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 07 '22 at 12:02