Installing OpenCV for Python on Ubuntu, getting ImportError: No module named cv2.cv

后端 未结 20 848
遥遥无期
遥遥无期 2020-11-28 23:48

I have an Ubuntu 14.04 system, on which I want to install OpenCV and use it with Python 2.x.

I installed OpenCV using the instructions here: https://help.ubuntu.com/

相关标签:
20条回答
  • 2020-11-29 00:28

    Try conda install -c conda-forge opencv if you are using anaconda, it works!

    0 讨论(0)
  • 2020-11-29 00:28

    You can build for source following the official OpenCV tutorial. The crucial part is to set the PYTHON3_EXECUTABLE, PYTHON_LIBRARY, PYTHON3_PACKAGES_PATH and PYTHON3_NUMPY_INCLUDE_DIRS parameters for python3.6. Here are all the steps:

    1. Clone the repo

      git clone https://github.com/opencv/opencv.git
      
    2. Create build directory

      cd ~/opencv
      mkdir build
      cd build
      
    3. Configure

      cmake -D CMAKE_BUILD_TYPE=RELEASE \
            -D CMAKE_INSTALL_PREFIX=/usr/local .. \
            -D PYTHON_INCLUDE_DIR=/usr/include/python3.6 \
            -D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.6m \
            -D BUILD_NEW_PYTHON_SUPPORT=ON \
            -D BUILD_opencv_python3=ON \
            -D HAVE_opencv_python3=ON \
            -D INSTALL_PYTHON_EXAMPLES=ON \
            -D PYTHON3_EXECUTABLE=/usr/bin/python3.6 \
            -D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3.6 \
            -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.6m.so \
            -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages .. \
            -D PYTHON3_NUMPY_INCLUDE_DIRS=/home/user/.local/lib/python3.6/site-packages/numpy/core/include/
      
    4. Build

      make -j8
      
    5. Install libraries

      sudo make install
      
    6. Test

      python3
      import cv2
      

    If you don't get the error "No module named cv2", then the installation was successful.

    Note: If you don't know the path to numpy for the PYTHON3_NUMPY_INCLUDE_DIRS parameter, you can find it by executing import numpy and then numpy.__file__ in a python3 shell.

    0 讨论(0)
  • 2020-11-29 00:29

    I found a solution in the guide here:

    http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/

    I resorted to compiling and installing from source. The process was very smooth, had I known, I would have started with that instead of trying to find a more simple way to install. Hopefully this information is helpful to someone.

    0 讨论(0)
  • 2020-11-29 00:29

    Try using: from cv2 import cv

    It works for me.

    0 讨论(0)
  • 2020-11-29 00:30

    If you want as simple as possible, install from the repository:

    sudo apt-get install python-opencv libopencv-dev python-numpy python-dev
    
    0 讨论(0)
  • 2020-11-29 00:34

    My environment:

    • Ubuntu 15.10
    • Python 3.5

    Since none of the previous answers worked for me, I downloaded OpenCV 3.0 from http://opencv.org/downloads.html and followed the installation manual. I used the following cmake command:

    $ ~/Programs/opencv-3.0.0$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D PYTHON3_EXECUTABLE=/usr/bin/python3.5 -D PYTHON_INCLUDE_DIR=/usr/include/python3.5 -D PYTHON_INCLUDE_DIR2=/usr/include/x86_64-linux-gnu/python3.5m -D PYTHON_LIBRARY=/usr/lib/x86_64-linux-gnu/libpython3.5m.so -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include/ -D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages ..
    

    Each step of the tutorial is important. Particularly, don't forget to call sudo make install.

    0 讨论(0)
提交回复
热议问题