opencv报错及解决:AttributeError: module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipeline‘

更新opencv版本后运行代码报错,报错内容如下

File "E:/code/***.py", line 9, in <module>
    import cv2
  File "D:\Program Files (x86)\Anaconda3\envs\y\lib\site-packages\cv2\__init__.py", line 181, in <module>
    bootstrap()
  File "D:\Program Files (x86)\Anaconda3\envs\y\lib\site-packages\cv2\__init__.py", line 175, in bootstrap
    if __load_extra_py_code_for_module("cv2", submodule, DEBUG):
  File "D:\Program Files (x86)\Anaconda3\envs\y\lib\site-packages\cv2\__init__.py", line 28, in __load_extra_py_code_for_module
    py_module = importlib.import_module(module_name)
  File "D:\Program Files (x86)\Anaconda3\envs\y\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "D:\Program Files (x86)\Anaconda3\envs\y\lib\site-packages\cv2\gapi\__init__.py", line 301, in <module>
    cv.gapi.wip.GStreamerPipeline = cv.gapi_wip_gst_GStreamerPipeline
AttributeError: module 'cv2' has no attribute 'gapi_wip_gst_GStreamerPipeline'

解决方案

第一步:查看所有安装的opencv
conda list | findstr opencv

查看显示结果

opencv-python             4.8.1.78                 pypi_0    pypi
opencv-python-headless    4.5.2.52                 pypi_0    pypi

可以看到有两个版本的opencv-python,一个opencv-python和一个无头的opencv-python(opencv-python-headless)这两个opencv之间会有冲突,可能会造成上述报错。

第二步:先卸载opencv-python
pip uninstall opencv-python 
第三步:查看环境中是否可以调用cv2
python
import cv2

若无报错,则可以间接表明opencv-python和opencv-python-headless之间存在冲突。

第四步:卸载opencv-python-headless
pip uninstall opencv-python-headless

卸载时显示的过程

Found existing installation: opencv-python-headless 4.5.2.52
Uninstalling opencv-python-headless-4.5.2.52:
  Would remove:
    d:\program files (x86)\anaconda3\envs\y\lib\site-packages\cv2\cv2.cp37-win_amd64.pyd
    d:\program files (x86)\anaconda3\envs\y\lib\site-packages\cv2\opencv_videoio_ffmpeg452_64.dll
    d:\program files (x86)\anaconda3\envs\y\lib\site-packages\opencv_python_headless-4.5.2.52.dist-info\*
Proceed (Y/n)? y
  Successfully uninstalled opencv-python-headless-4.5.2.52
第五步:安装opencv-python
pip install opencv-python -i  https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting opencv-python
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/38/d2/3e8c13ffc37ca5ebc6f382b242b44acb43eb489042e1728407ac3904e72f/opencv_python-4.8.1.78-cp37-abi3-win_amd64.whl (38.1 MB)
Requirement already satisfied: numpy>=1.17.0 in d:\program files (x86)\anaconda3\envs\y\lib\site-packages (from opencv-python) (1.21.6)
Installing collected packages: opencv-python
Successfully installed opencv-python-4.8.1.78

安装完成后重新运行代码,报错成功解决。
当然,也可以尝试直接逃过第二三步,直接卸载opencv-python-headless,由于本机的环境问题已经解决,并未进行上述尝试。所示不确定是否可以解决问题。

一点点意外经验:更新opencv版本后,代码的性能有所提升(运行处理时间减少

猜你喜欢

转载自blog.csdn.net/weixin_45994963/article/details/133783825