我的 python 版本是 3.6 的,当需要安装 opencv 时,使用pip install opencv-python
命令进行安装时,出现错误,如下所示:
Building Custom Rule C:/Users/liqian/AppData/Local/Temp/pip-install-6jpavinn/opencv-python_4bf9b16580254d7190cb574ec2fc8790/opencv/modules/imgproc/CMakeLists.txt
imgwarp.avx2.cpp
resize.avx2.cpp
accum.avx2.cpp
bilateral_filter.avx2.cpp
box_filter.avx2.cpp
filter.avx2.cpp
color_hsv.avx2.cpp
color_rgb.avx2.cpp
color_yuv.avx2.cpp
median_blur.avx2.cpp
morph.avx2.cpp
smooth.avx2.cpp
sumpixels.avx2.cpp
opencv_imgproc_AVX2.vcxproj -> C:\Users\liqian\AppData\Local\Temp\pip-install-6jpavinn\opencv-python_4bf9b16580254d7190cb574ec2fc8790\_skbuild\win-amd64-3.6\cmake-build\modules\imgproc\opencv_imgproc_AVX2.dir\Release\opencv_imgproc_AVX2.lib
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(517,5): warning MSB8029: 中间目录或输出目录无法驻留在临时目录下,因为这可能会导致增量生成出现问题。 [C:\Users\liqian\AppData\Local\Temp\pip-install-6jpavinn\opencv-python_4bf9b16580254d7190cb574ec2fc8790\_skbuild\win-amd64-3.6\cmake-build\modules\imgproc\opencv_imgproc_AVX512_SKX.vcxproj]
Building Custom Rule C:/Users/liqian/AppData/Local/Temp/pip-install-6jpavinn/opencv-python_4bf9b16580254d7190cb574ec2fc8790/opencv/modules/imgproc/CMakeLists.txt
sumpixels.avx512_skx.cpp
opencv_imgproc_AVX512_SKX.vcxproj -> C:\Users\liqian\AppData\Local\Temp\pip-install-6jpavinn\opencv-python_4bf9b16580254d7190cb574ec2fc8790\_skbuild\win-amd64-3.6\cmake-build\modules\imgproc\opencv_imgproc_AVX512_SKX.dir\Release\opencv_imgproc_AVX512_SKX.lib
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(517,5): warning MSB8029: 中间目录或输出目录无法驻留在临时目录下,因为这可能会导致增量生成出现问题。 [C:\Users\liqian\AppData\Local\Temp\pip-install-6jpavinn\opencv-python_4bf9b16580254d7190cb574ec2fc8790\_skbuild\win-amd64-3.6\cmake-build\modules\imgproc\opencv_imgproc_SSE4_1.vcxproj]
Building Custom Rule C:/Users/liqian/AppData/Local/Temp/pip-install-6jpavinn/opencv-python_4bf9b16580254d7190cb574ec2fc8790/opencv/modules/imgproc/CMakeLists.txt
imgwarp.sse4_1.cpp
resize.sse4_1.cpp
accum.sse4_1.cpp
box_filter.sse4_1.cpp
filter.sse4_1.cpp
color_hsv.sse4_1.cpp
color_rgb.sse4_1.cpp
color_yuv.sse4_1.cpp
median_blur.sse4_1.cpp
morph.sse4_1.cpp
smooth.sse4_1.cpp
opencv_imgproc_SSE4_1.vcxproj -> C:\Users\liqian\AppData\Local\Temp\pip-install-6jpavinn\opencv-python_4bf9b16580254d7190cb574ec2fc8790\_skbuild\win-amd64-3.6\cmake-build\modules\imgproc\opencv_imgproc_SSE4_1.dir\Release\opencv_imgproc_SSE4_1.lib
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(517,5): warning MSB8029: 中间目录或输出目录无法驻留在临时目录下,因为这可能会导致增量生成出现问题。 [C:\Users\liqian\AppData\Local\Temp\pip-install-6jpavinn\opencv-python_4bf9b16580254d7190cb574ec2fc8790\_skbuild\win-amd64-3.6\cmake-build\modules\python_bindings_generator\gen_opencv_python_source.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(382,5): error MSB3491: 未能向文件“x64\Release\gen_opencv_python_source\gen_open.7733C44B.tlog\gen_opencv_python_source.lastbuildstate”写入命令行。路径: x64\Release\gen_opencv_python_source\gen_open.7733C44B.tlog\gen_opencv_python_source.lastbuildstate 超过 OS 最大路径限制。完全限定的文件名必须少于 260 个字符。 [C:\Users\liqian\AppData\Local\Temp\pip-install-6jpavinn\opencv-python_4bf9b16580254d7190cb574ec2fc8790\_skbuild\win-amd64-3.6\cmake-build\modules\python_bindings_generator\gen_opencv_python_source.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(517,5): warning MSB8029: 中间目录或输出目录无法驻留在临时目录下,因为这可能会导致增量生成出现问题。 [C:\Users\liqian\AppData\Local\Temp\pip-install-6jpavinn\opencv-python_4bf9b16580254d7190cb574ec2fc8790\_skbuild\win-amd64-3.6\cmake-build\modules\.firstpass\videoio\opencv_videoio_plugins.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160\Microsoft.CppBuild.targets(382,5): error MSB3491: 未能向文件“x64\Release\opencv_videoio_plugins\opencv_v.B9CC6B5E.tlog\opencv_videoio_plugins.lastbuildstate”写入命令行。路径: x64\Release\opencv_videoio_plugins\opencv_v.B9CC6B5E.tlog\opencv_videoio_plugins.lastbuildstate 超过 OS 最大路径限制。完全限定的文件名必须少于 260 个字符。 [C:\Users\liqian\AppData\Local\Temp\pip-install-6jpavinn\opencv-python_4bf9b16580254d7190cb574ec2fc8790\_skbuild\win-amd64-3.6\cmake-build\modules\.firstpass\videoio\opencv_videoio_plugins.vcxproj]
Traceback (most recent call last):
File "C:\Users\liqian\AppData\Local\Temp\pip-build-env-onh0v2r3\overlay\Lib\site-packages\skbuild\setuptools_wrap.py", line 642, in setup
cmkr.make(make_args, install_target=cmake_install_target, env=env)
File "C:\Users\liqian\AppData\Local\Temp\pip-build-env-onh0v2r3\overlay\Lib\site-packages\skbuild\cmaker.py", line 679, in make
self.make_impl(clargs=clargs, config=config, source_dir=source_dir, install_target=install_target, env=env)
File "C:\Users\liqian\AppData\Local\Temp\pip-build-env-onh0v2r3\overlay\Lib\site-packages\skbuild\cmaker.py", line 711, in make_impl
"An error occurred while building with CMake.\n"
An error occurred while building with CMake.
Command:
'C:\Users\liqian\AppData\Local\Temp\pip-build-env-onh0v2r3\overlay\Lib\site-packages\cmake\data\bin/cmake.exe' --build . --target install --config Release --
Install target:
install
Source directory:
C:\Users\liqian\AppData\Local\Temp\pip-install-6jpavinn\opencv-python_4bf9b16580254d7190cb574ec2fc8790
Working directory:
C:\Users\liqian\AppData\Local\Temp\pip-install-6jpavinn\opencv-python_4bf9b16580254d7190cb574ec2fc8790\_skbuild\win-amd64-3.6\cmake-build
Please check the install target is valid and see CMake's output for more information.
----------------------------------------
ERROR: Failed building wheel for opencv-python
Failed to build opencv-python
ERROR: Could not build wheels for opencv-python which use PEP 517 and cannot be installed directly
开始以为网络的问题,尝试了几次都没有成功。
第一种方案是使用 conda 命令安装成功了, conda install py-opencv
.
测试一下,安装成功。
查了半天资料,发现是python版本比较低,直接安装的话,高版本的opencv和低版本的python难以匹配。
因此,还有一种解决方案
安装较低版本的 opencv:
pip install opencv-python==3.4.9.31
包括 opencv-contrib-python :
pip install opencv-contrib-python==3.4.9.31
这样也是能够安装成功的。
参考链接:https://blog.csdn.net/z704630835/article/details/124564044
https://blog.csdn.net/u013548568/article/details/108885490
日常学习记录,一起交流讨论吧!侵权联系~