Ubuntu, how to install OpenCV for python3?

前端 未结 8 1841
谎友^
谎友^ 2020-12-13 14:51

I want to install OpenCV for python3 in ubuntu 16.04. Fist I tried running sudo apt-get install python3-opencv which is how I pretty much install all of my pyt

相关标签:
8条回答
  • 2020-12-13 15:05

    Assuming that you installed Python3.x, I resolved it using the following:

    1: Install side packages required for OpenCV with Ubuntu (only validated for: Ubuntu 16.04):

    apt-get update
    apt-get install -y libglib2.0.0 libsm6
    apt-get install libxext6
    apt-get install -y libxrender-dev
    

    2: Install OpenCV on python3.x:

    pip3 install opencv-contrib-python
    
    0 讨论(0)
  • 2020-12-13 15:13

    This is because you have multiple installations of python in your machine.You should make python3 the default, because by default is the python2.7

    0 讨论(0)
  • 2020-12-13 15:15

    I found this:

    https://pypi.python.org/pypi/opencv-python

    OpenCV on wheels

    'Unofficial OpenCV packages for Python.'

    Installation was painless for Ubuntu 16.04

    pip3 install opencv-python
    

    Check the installation

    python3
    Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
    [GCC 5.4.0 20160609] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import cv2
    >>> cv2.__version__
    '3.2.0'
    

    Not sure why this wasn't mentioned. Perhaps it is newly available?

    0 讨论(0)
  • 2020-12-13 15:17
    sudo pip3 install opencv-python opencv-contrib-python
    
    0 讨论(0)
  • 2020-12-13 15:24

    The popular suggested solution worked miracles for me, where sudo apt install of opencv failed to install properly. Another note on step 4 (which may make life easier for others). It seems simple now but it took me quite a while to troubleshoot (because I am new to this).

    Step 4a: Run the following code to find out where the cv2 file is installed. Your filename may also be slightly different. Just because you can't find it where the OP said it would be, that doesn't mean the installation did not work.

    > $ find /usr/ -iname cv2.*
    > /usr/lib/python3/dist-packages/cv2.cpython-38-x86_64-linux-gnu.so
    

    Step 4b: SymLink OpenCV 3.0 as above

    > $ cd ~/anaconda3/virtualenv/lib/python3.8/site-packages/
    > $ ln -s /usr/lib/python3/dist-packages/cv2.cpython-38-x86_64-linux-gnu.so cv2.so
    
    0 讨论(0)
  • 2020-12-13 15:28

    Using conda inside a python3 environment:

    First install conda in a python3 environment and activate it if you haven't yet:

    conda create --name py3k python=3
    source activate py3k
    

    Now you can install opencv in the conda environment:

    pip install pillow
    conda install -c menpo opencv3=3.1.0
    

    To import in Python:

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