用anaconda python3安装opencv 3.1?

时间:2016-08-05 11:08:00

标签: python opencv anaconda ubuntu-16.04

如何用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

我使用以下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" ..

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

如何为anaconda python3构建?

7 个答案:

答案 0 :(得分:38)

我认为你不需要为anaconda构建OpenCV,这非常方便 安装后,终端中可以使用名为“conda”的工具 Anaconda python发行版。

我发现这个网站提供了有关如何安装opencv3

的说明
https://anaconda.org/menpo/opencv3

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

如果您的系统中安装了Anaconda python发行版,您可以发出此命令(假设您正在使用Linux)启动终端:

conda install -c menpo opencv

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

conda install -c menpo opencv3

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

import cv2 # import the opencv library

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

希望有帮助=)

答案 1 :(得分:22)

如果您收到如下错误:

    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.

只需安装python 3.5并再试一次。

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

答案 2 :(得分:8)

尝试这种方法对我有用。 Anaconda3-4

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

答案 3 :(得分:6)

上述方法都不适合我。但是,我发现这个衬垫对我有用:

conda install -c conda-forge opencv

答案 4 :(得分:3)

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

conda install -c anaconda opencv

答案 5 :(得分:0)

如果要针对特定​​的anaconda环境编译opencv,可以在PYTHON_EXECUTABLE, PYTHON_INCLUDE中指定PYTHON_LIBRARY, PYTHON_PACKAGES_PATH, PYTHON_NUMPY_INCLUDE_DIRcmake个变量。

在下面的示例中,我在/ 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

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

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

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

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

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

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

然后(根据你的情况和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/.

你应该可以从python控制台import cv2

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

答案 6 :(得分:0)

conda: 4.7.10 , python: 3.7.3 , 频道:默认

False频道安装opencv对我有用。我安装在干净的环境中,没有冲突。

default
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