使用 anaconda python3 安装 opencv 3.1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38787748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:25:43  来源:igfitidea点击:

Installing opencv 3.1 with anaconda python3?

pythonopencvanacondaubuntu-16.04

提问by Tamim Addari

How do I install opencv with anaconda python3 , opencv picked up my python3 executables

我如何使用 anaconda python3 安装 opencv,opencv 获取了我的 python3 可执行文件

--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.12)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython2.7.so (ver 2.7.12)
--     numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.11.0)
--     packages path:               lib/python2.7/dist-packages
-- 
--   Python 3:
--     Interpreter:                 /home/tamim/anaconda3/bin/python3 (ver 3.5.2)
--     Libraries:                   /usr/lib/x86_64-linux-gnu/libpython3.5m.so (ver 3.5.2)
--     numpy:                       /home/tamim/anaconda3/lib/python3.5/site-packages/numpy/core/include (ver 1.11.1)
--     packages path:               lib/python3.5/site-packages
-- 
--   Python (for build):            /usr/bin/python2.7

I installed opencv with the following make options

我使用以下 make 选项安装了 opencv

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D WITH_CUBLAS=1 -D CUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..

But after installing it I can't import cv2 within python3 of anaconda. I can however import cv2 from builtin python2 command. So I suppose it build for the python2 version as stated in the last line.

但是安装后我无法在anaconda的python3中导入cv2。但是,我可以从内置的 python2 命令导入 cv2。所以我想它是为最后一行中所述的 python2 版本构建的。

How do I build for anaconda python3 ?

如何为 anaconda python3 构建?

回答by stevenferrer

I think you don't need to build OpenCV for anaconda, there is this very handy tool called 'conda' that is available in your terminal once you have installed the Anaconda python distribution.

我认为您不需要为 anaconda 构建 OpenCV,一旦您安装了 Anaconda python 发行版,就有一个名为“conda”的非常方便的工具可以在您的终端中使用。

I found this site which gives instruction on how to install opencv3

我找到了这个网站,它提供了有关如何安装 opencv3 的说明

https://anaconda.org/menpo/opencv3

I personally installed it myself so just try follow along with these instructions.

我亲自安装了它,所以请尝试按照这些说明进行操作。

If you have the Anaconda python distribution installed in your system, you can issue this command (assuming you are working on linux) fire up the terminal:

如果您的系统中安装了 Anaconda python 发行版,您可以发出以下命令(假设您在 linux 上工作)启动终端:

conda install -c menpo opencv

If the version of python install in your Anaconda is 2.7, the command above should install OpenCV 3.1, but if the version of your python is 3.5, then you should change 'opencv' in the last line to 'opencv3'

如果你的 Anaconda 中安装的 python 版本是 2.7,上面的命令应该安装 OpenCV 3.1,但如果你的 python 版本是 3.5,那么你应该将最后一行中的 'opencv' 更改为 'opencv3'

conda install -c menpo opencv3

This should install OpenCV in your Anaconda. To see if you have installed it successfully, fire up your Python and issue the following command:

这应该在你的 Anaconda 中安装 OpenCV。要查看您是否已成功安装它,请启动您的 Python 并发出以下命令:

import cv2 # import the opencv library

cv2.__version__ # this will print the version of your opencv3

Hope that helps =)

希望有帮助=)

回答by mimoralea

If you are getting an error like the following:

如果您收到如下错误:

    UnsatisfiableError: The following specifications were found to be in conflict:
            opencv 2.4.11* -> numpy 1.9* -> python 2.6* -> openssl 1.0.1*
            python 3.6*
            Use "conda info " to see the dependencies for each package.

Just install python 3.5 and try again.

只需安装 python 3.5 并重试。

conda install python=3.5
conda install -c menpo opencv  # whichever
conda install -c menpo opencv3 # you need

回答by Nandan Lahurikar

Try this method it worked for me. Anaconda3-4

试试这个对我有用的方法。蟒蛇3-4

conda create -n opencv 
activate opencv
conda install -c https://conda.binstar.org/menpo opencv3

回答by Isopycnal Oscillation

None of the methods above worked for me. However, I found that this one liner works for me:

以上方法都不适合我。但是,我发现这个班轮对我有用:

conda install -c conda-forge opencv

回答by hcole

With conda v4.3.16 and python v3.6 this command worked for me:

使用 conda v4.3.16 和 python v3.6 这个命令对我有用:

conda install -c anaconda opencv

回答by calocedrus

If you want to compile opencv against a specific anaconda environment, you can specify the PYTHON_EXECUTABLE, PYTHON_INCLUDEand PYTHON_LIBRARY, PYTHON_PACKAGES_PATH, PYTHON_NUMPY_INCLUDE_DIRvariables in cmake.

如果你想编译OpenCV的针对特定蟒蛇的环境中,你可以指定PYTHON_EXECUTABLE, PYTHON_INCLUDEPYTHON_LIBRARY, PYTHON_PACKAGES_PATH, PYTHON_NUMPY_INCLUDE_DIR变量cmake

In the following example I have an opencv340 anaconda environment located in /home/yourself/anaconda3/envs/opencv340 and so I'll specifiy the above variables for cmakeas follows:

在以下示例中,我在 /home/yourself/anaconda3/envs/opencv340 中有一个 opencv340 anaconda 环境,因此我将指定上述变量cmake如下:

-DPYTHON_EXECUTABLE=/home/yourself/anaconda3/envs/opencv340/bin/python3 \
-DPYTHON_INCLUDE=/home/yourself/anaconda3/envs/opencv340/include \
-DPYTHON_LIBRARY=/home/yourself/anaconda3/envs/opencv340/lib/libpython3.6m.so \
-DPYTHON_PACKAGES_PATH=/home/yourself/anaconda3/envs/opencv340/lib/python3.6/site-packages \
-DPYTHON_NUMPY_INCLUDE_DIR=/home/yourself/anaconda3/envs/opencv340/lib/python3.6/site-packages/core/include

You'll then see that opencv pick the correct python, the one in the anaconda environment of which you gave the path of.

然后你会看到 opencv 选择了正确的 python,你给出了路径的 anaconda 环境中的那个。

You then copy the cv2*.so from your opencv build directory to the site-packagesof your anaconda environment.

然后将 cv2*.so 从 opencv 构建目录复制到site-packagesanaconda 环境的 。

Your site-packagesdirectory should be located somewhere like:

您的site-packages目录应该位于以下位置:

/home/yourself/anaconda3/envs/opencv340/lib/python3.6/site-packages

/home/yourself/anaconda3/envs/opencv340/lib/python3.6/site-packages

You can get the actual location from a python console with:

您可以使用以下命令从 python 控制台获取实际位置:

>>> import sys
>>> print(next(p for p in sys.path if 'site-packages' in p))

Then (adapt accordingly to your case and cv2*.so actual name):

然后(根据您的情况和 cv2*.so 实际名称进行相应调整):

cp /path/to/your/opencv/build/directory/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so /home/yourself/anaconda3/envs/opencv340/lib/python3.6/site-packages/.

cp /path/to/your/opencv/build/directory/lib/python3.6/site-packages/cv2.cpython-36m-x86_64-linux-gnu.so /home/yourself/anaconda3/envs/opencv340/lib/python3.6/site-packages/.

And you should be able to import cv2from a python console.

你应该能够import cv2从 python 控制台。

I've just done so (ubuntu 16.04).

我刚刚这样做了(ubuntu 16.04)。

回答by Ryan H.

conda: 4.7.10, python: 3.7.3, channel: default

畅达:4.7.10,蟒蛇:3.7.3,通道:默认

Installing opencvfrom the defaultchannel worked for me. I installed into a clean environment and had no conflicts.

opencvdefault频道安装对我有用。我安装到一个干净的环境中,没有冲突。

conda install opencv
The following NEW packages will be INSTALLED:

  blas               pkgs/main/osx-64::blas-1.0-mkl
  bzip2              pkgs/main/osx-64::bzip2-1.0.8-h1de35cc_0
  cairo              pkgs/main/osx-64::cairo-1.14.12-hc4e6be7_4
  ffmpeg             pkgs/main/osx-64::ffmpeg-4.0-h01ea3c9_0
  fontconfig         pkgs/main/osx-64::fontconfig-2.13.0-h5d5b041_1
  freetype           pkgs/main/osx-64::freetype-2.9.1-hb4e5f40_0
  gettext            pkgs/main/osx-64::gettext-0.19.8.1-h15daf44_3
  glib               pkgs/main/osx-64::glib-2.56.2-hd9629dc_0
  graphite2          pkgs/main/osx-64::graphite2-1.3.13-h2098e52_0
  harfbuzz           pkgs/main/osx-64::harfbuzz-1.8.8-hb8d4a28_0
  hdf5               pkgs/main/osx-64::hdf5-1.10.2-hfa1e0ec_1
  icu                pkgs/main/osx-64::icu-58.2-h4b95b61_1
  intel-openmp       pkgs/main/osx-64::intel-openmp-2019.4-233
  jasper             pkgs/main/osx-64::jasper-2.0.14-h636a363_1
  jpeg               pkgs/main/osx-64::jpeg-9b-he5867d9_2
  libgfortran        pkgs/main/osx-64::libgfortran-3.0.1-h93005f0_2
  libiconv           pkgs/main/osx-64::libiconv-1.15-hdd342a3_7
  libopencv          pkgs/main/osx-64::libopencv-3.4.2-h7c891bd_1
  libopus            pkgs/main/osx-64::libopus-1.3-h1de35cc_0
  libpng             pkgs/main/osx-64::libpng-1.6.37-ha441bb4_0
  libtiff            pkgs/main/osx-64::libtiff-4.0.10-hcb84e12_2
  libvpx             pkgs/main/osx-64::libvpx-1.7.0-h378b8a2_0
  libxml2            pkgs/main/osx-64::libxml2-2.9.9-hf6e021a_1
  mkl                pkgs/main/osx-64::mkl-2019.4-233
  mkl_fft            pkgs/main/osx-64::mkl_fft-1.0.12-py37h5e564d8_0
  mkl_random         pkgs/main/osx-64::mkl_random-1.0.2-py37h27c97d8_0
  numpy              pkgs/main/osx-64::numpy-1.16.4-py37hacdab7b_0
  numpy-base         pkgs/main/osx-64::numpy-base-1.16.4-py37h6575580_0
  opencv             pkgs/main/osx-64::opencv-3.4.2-py37h6fd60c2_1
  pcre               pkgs/main/osx-64::pcre-8.43-h0a44026_0
  pixman             pkgs/main/osx-64::pixman-0.38.0-h1de35cc_0
  py-opencv          pkgs/main/osx-64::py-opencv-3.4.2-py37h7c891bd_1
  zstd               pkgs/main/osx-64::zstd-1.3.7-h5bba6e5_0
$python                                                                                                
Python 3.7.3 (default, Mar 27 2019, 16:54:48)
[Clang 4.0.1 (tags/RELEASE_401/final)] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.4.2'
>>>