【问题标题】:How to make OpenCV 3.2 package built from source available in Anaconda environment?如何使从源代码构建的 OpenCV 3.2 包在 Anaconda 环境中可用?
【发布时间】:2017-05-20 18:56:23
【问题描述】:

我在 Windows 10 下使用 Visual Studio 2017 从源代码构建了 OpenCV 3.2,它在构建目录中生成了一个文件 cv2.cp36-win_amd64.pyd,并将其复制到 C:\Users\fanta\Anaconda3\Lib\site-packages

但是现在进入 Python 3.6 的 Anaconda 环境后,python 找不到包,请看下面的脚本(底部有错误):

C:\Users\fanta>conda create -n py36 python=3.6
Fetching package metadata ...........
Solving package specifications: .

Package plan for installation in environment C:\Users\fanta\Anaconda3\envs\py36:

The following NEW packages will be INSTALLED:

    pip:            9.0.1-py36_1
    python:         3.6.1-2
    setuptools:     27.2.0-py36_1
    vs2015_runtime: 14.0.25123-0
    wheel:          0.29.0-py36_0

Proceed ([y]/n)?

#
# To activate this environment, use:
# > activate py36
#
# To deactivate this environment, use:
# > deactivate py36
#
# * for power-users using bash, you must source
#


C:\Users\fanta>activate py36

(py36) C:\Users\fanta>python
Python 3.6.1 |Continuum Analytics, Inc.| (default, May 11 2017, 13:25:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
>>>

如果我尝试从任何 Anaconda 环境之外导入 cv2 包,则会收到不同的错误,请参见下文:

(py36) C:\Users\fanta>deactivate py36

C:\Users\fanta>python
Python 3.6.0 |Anaconda 4.3.1 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed: The specified module could not be found.
>>>

如何使构建的cv2 包可以导入到 Anaconda 环境中?

谢谢!

【问题讨论】:

    标签: opencv anaconda conda


    【解决方案1】:

    原来cv2.cp36-win_amd64.pyd 文件的正确位置是(对于我的py36 Anaconda 环境):

    C:\Users\fanta\Anaconda3\envs\py36\Lib

    然后我收到了ImportError: DLL load failed: The specified module could not be found.

    修复 ImportError:

    • 将带有为 OpenCV 构建的 DLL 的目录添加到 PATH 环境变量(C:\opencv32\build\install\x64\vc15\bin 在我的 案例);
    • 设置 OPENCV_DIR (在我的 C:\opencv32\build\install\x64\vc15 案例)。

    最后我在 Anaconda 环境中安装了 numpy 和 matplotlib。

    现在使用 Visual Studio 2017 从源代码构建的 OpenCV 3.2 可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-18
      相关资源
      最近更新 更多