【问题标题】:How to install OpenCV for Python 3.x on Windows 8.1?如何在 Windows 8.1 上为 Python 3.x 安装 OpenCV?
【发布时间】:2015-11-11 12:40:48
【问题描述】:

我正在尝试在 Windows 8.1 中为 python 3.4.2 安装和运行 OpenCV 3.0.0。

  1. 我已经从http://opencv.org/下载了OpenCV文件。
  2. 解压C:中的文件夹。
  3. 我去了系统高级系统设置高级环境变量
  4. 在“用户变量框架”中添加一个新变量。将其命名为“OPENCV_DIR”,值为“c:\opencv\build\x64\vc12”。
  5. 在路径末尾添加:“;C:\opencv;C:\opencv\build;%OPENCV_DIR%\bin”
  6. 我从“C:\opencv\build\python\2.7\x64”中取出存档“cv2.pyd”并复制了它。
  7. 我已将其粘贴到C:\Python34\Lib\site-packages

最后我去Python 3.4.2的IDLE写了(这发生在将罚款复制到Python目录之前):

import cv2
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import cv2
ImportError: No module named 'cv2'

这发生在我将文件复制到 Python 目录之后:

import cv2
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    import cv2
ImportError: DLL load failed: The specified module could not be found.

我做错了什么?我不确定是否应该安装 CMake 和 NumPy。

另外我不知道如何使用这个网页(被这么多人推荐), http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv,这里有很多 Python 库,包括 PIL、OpenCV、VideoCapture 等...

我想尝试运行以下示例:

好的,我不应该在 Python 3.4 中使用来自 Python 2.7 的文件。它行不通。我删除了它,所以按照 otterb 的建议,我刚刚从http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv 安装了 opencv_python‑3.0.0‑cp34‑none‑win_amd64.whl 包,但是这个文件是 .whl 不是 EXE 文件,并且根据 How do I install a Python package with a .whl file? 我按照以下步骤操作:

  1. 转到命令提示符

  2. 使用“cd”将目录更改为我下载文件的位置

  3. 并写“pip install opencv_python-3.0.0-cp34-none-win_amd64.whl”

但我收到以下红色消息:

Installing collected packages: opencv-python
Cleaning up...
  Exception:
Traceback (most recent call last):
  File "C:\Python34\lib\shutil.py", line 370, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 32] The process cannot access the file because it     is
being used by another process:     'C:\\Users\\JOSEFG~1\\AppData\\Local\\Temp\\pip-n
kyo0c6u-build\\opencv_python-3.0.0.data\\data\\Lib\\site-packages\\cv2.pyd'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\pip\commands\install.py", line 283, in     run

    requirement_set.install(install_options, global_options,     root=options.root_p
ath)
  File "C:\Python34\lib\site-packages\pip\req.py", line 1444, in install
    requirement.remove_temporary_source()
  File "C:\Python34\lib\site-packages\pip\req.py", line 761, in     remove_temporary
_source
    rmtree(self._temp_build_dir)
  File "C:\Python34\lib\site-packages\pip\util.py", line 43, in rmtree
    onerror=rmtree_errorhandler)
  File "C:\Python34\lib\shutil.py", line 477, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Python34\lib\shutil.py", line 372, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Python34\lib\site-packages\pip\util.py", line 53, in rmtree_errorhandler
    (exctype is PermissionError and value.args[3] == 5) #python3.3
IndexError: tuple index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\shutil.py", line 370, in _rmtree_unsafe
    os.unlink(fullname)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\JOSEFG~1\\AppData\\Local\\Temp\\pip-nkyo0c6u-build\\opencv_python-3.0.0.data\\data\\Lib\\site-packages\\cv2.pyd'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\pip\basecommand.py", line 122, in main
    status = self.run(options, args)
  File "C:\Python34\lib\site-packages\pip\commands\install.py", line 302, in run

    requirement_set.cleanup_files(bundle=self.bundle)
  File "C:\Python34\lib\site-packages\pip\req.py", line 1319, in cleanup_files
    req.remove_temporary_source()
  File "C:\Python34\lib\site-packages\pip\req.py", line 761, in remove_temporary_source
    rmtree(self._temp_build_dir)
  File "C:\Python34\lib\site-packages\pip\util.py", line 43, in rmtree
    onerror=rmtree_errorhandler)
  File "C:\Python34\lib\shutil.py", line 477, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Python34\lib\shutil.py", line 367, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "C:\Python34\lib\shutil.py", line 372, in _rmtree_unsafe
    onerror(os.unlink, fullname, sys.exc_info())
  File "C:\Python34\lib\site-packages\pip\util.py", line 53, in rmtree_errorhandler
    (exctype is PermissionError and value.args[3] == 5) #python3.3
IndexError: tuple index out of range

  Storing debug log for failure in C:\Users\me\pip\pip.log

这意味着仔细阅读后,该文件无法安全地安装在我的 PC 上。

另外,根据How do I install pip on Windows?,似乎 pip 应该是运行上一个命令的最新命令。然后我只是在命令提示符下升级 pip

python -m pip install -U pip

现在我有 7.1 版。我通过写作就知道了:

pip --version

然后我又试了一次

pip install opencv_python-3.0.0-cp34-none-win_amd64.whl

但是当我收到这条消息时,它似乎正在工作:

C:\Users\Jose F Gomez D\Downloads>pip install opencv_python-3.0.0-cp34-none-win_amd64.whl
Requirement already satisfied (use --upgrade to upgrade): opencv-python==3.0.0 from file:///C:/Users/Jose%20F%20Gomez%20D/Downloads/opencv_python-3.0.0-cp34-none-win_amd64.whl in c:\python34\lib\site-packages

然后我在python shell中写“import cv2”但是有一个新的错误

ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import cv2
ImportError: numpy.core.multiarray failed to import

所以我安装了 numpy(和 scipy 以防万一)库

pip install numpy
pip install scipy

但它不工作,因为它需要 C++

错误 microsoft visual c++ 10 is required

我试图从https://www.visualstudio.com/的Visual Studio网页中找到“Visual C++ 2010 Express”,但是没有“Microsoft Visual C++ 2010 express”的链接,所以我去了页面http://microsoft-visual-cpp-express.soft32.com/free-download/,下载了文件,执行它,现在我得到了 C++。

我再次转到命令提示符并尝试安装 NumPy 和 SciPy,但没有成功。路径有错误:

ValueError: ['path']

好的,现在我尝试从以下位置安装 NumPy 和 SciPy 包:

http://sourceforge.net/projects/numpy/files/NumPy/

但是没用,于是我去了 Gohike,http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy,并以管理员模式从命令提示符安装了 NumPy 和 SciPy:

pip install scipy‑0.16.0‑cp34‑none‑win_amd64.whl
pip install numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl

现在我尝试运行 cv,但什么也没有。 Uups,它是 cv2,它正在工作。接下来我会写答案。

【问题讨论】:

  • 您似乎将python27的cv2复制到python34中。并且还要确保 python 和库都使用匹配的 32 位或 64 位版本。无论如何,我推荐来自 Gohlke 的安装程序。上次我使用 Gohkle 的安装程序在 win7 上的 64 位 python34 上安装 64 位 opencv,它对我有用。只需运行一个普通的windows exe,然后按照指示运行即可。
  • 我复制了cv2 2.7版本,因为我没有任何其他选择,我将尝试使用Gohkle在64位python34上安装64位opencv,但它不是exe是.whl,怎么办我愿意吗?

标签: python opencv numpy cmake


【解决方案1】:

在 Windows 8.1 上安装 OpenCV 3.0.0 for Python 3.4.2 的正确方法:

  1. 转到http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv
  2. 下载 OpenCV、NumPy 和 SciPy

对我来说,我需要:

  • opencv_python-3.0.0-cp34-none-win_amd64.whl

  • scipy‑0.16.0‑cp34‑none‑win_amd64.whl

  • numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl

    1. http://microsoft-visual-cpp-express.soft32.com/free-download/ 下载并安装 Visual C++ 2010 Express
    2. 以管理员身份打开命令提示符
    3. 将“cd C:\Users\me\Downloads”等目录更改为我下载文件的位置
    4. 复制并粘贴:“python -m pip install -U pip”
    5. 然后安装库。复制并粘贴:

      pip install opencv_python-3.0.0-cp34-none-win_amd64.whl

      pip install scipy-0.16.0-cp34-none-win_amd64.whl

      pip install numpy‑1.9.2+mkl‑cp34‑none‑win_amd64.whl

就是这样。

----------> 棘手的 Python

【讨论】:

    【解决方案2】:

    您可以使用 pip 安装 whl 文件,如:

    pip install path/to/whlfile
    

    是的,你是对的。 Gohlke 的包曾经是一个 EXE 文件文件,但 OpenCV 现在以 whl 的形式分发。对不起,我错过了。

    这是一个类似的 StackOverflow 问题:How do I install a Python package with a .whl file?

    【讨论】:

      【解决方案3】:

      我已经使用conda(安装 Anaconda 3 - 64 bit)通过以下命令(conda installation of OpenCV 3)成功安装了 OpenCV 3.1.0:

      conda install -c menpo opencv3=3.1.0
      

      请注意,唯一完整的安装是从 GitHub 存储库编译 OpenCV 库获得的。我发现 conda 安装和轮子中缺少某些功能(例如 SURF 检测)。

      【讨论】:

        【解决方案4】:

        我做了 Jose Fernando Gomez Davila 提到的所有事情,除了我在 scipy 之前安装了 numpy,因为安装 scipy 需要 numpy

        pip install opencv_python-4.1.2-cp37-cp37m-win_amd64.whl

        pip install scipy-1.4.1-cp37-cp37m-win_amd64.whl

        pip install numpy-1.16.6+mkl-cp37-cp37m-win_amd64.whl

        【讨论】:

          猜你喜欢
          • 2016-02-11
          • 1970-01-01
          • 2015-06-17
          • 1970-01-01
          • 2018-06-19
          • 1970-01-01
          • 1970-01-01
          • 2015-07-28
          • 1970-01-01
          相关资源
          最近更新 更多