docker容器中使用opencv-python报错

在构建机器学习环境的docker镜像时,安装了paddlehub,paddlehub的依赖有opencv-python,使用时报错如下:

Traceback (most recent call last):
  File "woodpecker/etl_main.py", line 2, in <module>
    from etl import etl_paper_html, tl_paper_paragraph_label, tl_model_inputs
  File "/ws/woodpecker/etl/tl_paper_paragraph_label.py", line 13, in <module>
    from bai.project_common.woodpecker.preprocess import (
  File "<frozen zipimport>", line 259, in load_module
  File "/usr/local/lib/python3.8/site-packages/bai-0.3.0-py3.8.egg/bai/project_common/woodpecker/preprocess.py", line 4, in <module>
  File "<frozen zipimport>", line 259, in load_module
  File "/usr/local/lib/python3.8/site-packages/bai-0.3.0-py3.8.egg/bai/feature_extraction/text/__init__.py", line 4, in <module>
  File "/usr/local/lib/python3.8/site-packages/paddlehub/__init__.py", line 31, in <module>
    from paddlehub import datasets
  File "/usr/local/lib/python3.8/site-packages/paddlehub/datasets/__init__.py", line 15, in <module>
    from paddlehub.datasets.canvas import Canvas
  File "/usr/local/lib/python3.8/site-packages/paddlehub/datasets/canvas.py", line 23, in <module>
    from paddlehub.vision.utils import get_img_file
  File "/usr/local/lib/python3.8/site-packages/paddlehub/vision/utils.py", line 18, in <module>
    import cv2
  File "/usr/local/lib/python3.8/site-packages/cv2/__init__.py", line 8, in <module>
    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

这是导入opencv报错了,经过研究,发现opencv-python文档提供了4种安装包,我的理解如下:

包名适用环境说明
opencv-python桌面环境,例如Windows,macOS,GNU/Linux包含核心模块
opencv-contrib-python桌面环境,例如Windows,macOS,GNU/Linux包含所有模块
opencv-python-headless服务器环境,无GUI library依赖,例如Docker,云环境包含核心模块
opencv-contrib-python-headless服务器环境,无GUI library依赖,例如Docker,云环境包含所有模块

这四个包,只能选择一个进行安装,paddlehub依赖opencv-python,构建docker镜像时,替换成opencv-python-headless即可。

opencv-python原文如下:

Select the correct package for your environment:

There are four different packages (see options 1, 2, 3 and 4 below) and you should SELECT ONLY ONE OF THEM. Do not install multiple different packages in the same environment. There is no plugin architecture: all the packages use the same namespace (cv2). If you installed multiple different packages in the same environment, uninstall them all with pip uninstall and reinstall only one package.

a. Packages for standard desktop environments (Windows, macOS, almost any GNU/Linux distribution)

    Option 1 - Main modules package: pip install opencv-python
    Option 2 - Full package (contains both main modules and contrib/extra modules): pip install opencv-contrib-python (check contrib/extra modules listing from OpenCV documentation)

b. Packages for server (headless) environments (such as Docker, cloud environments etc.), no GUI library dependencies

These packages are smaller than the two other packages above because they do not contain any GUI functionality (not compiled with Qt / other GUI components). This means that the packages avoid a heavy dependency chain to X11 libraries and you will have for example smaller Docker images as a result. You should always use these packages if you do not use cv2.imshow et al. or you are using some other package (such as PyQt) than OpenCV to create your GUI.

    Option 3 - Headless main modules package: pip install opencv-python-headless
    Option 4 - Headless full package (contains both main modules and contrib/extra modules): pip install opencv-contrib-python-headless (check contrib/extra modules listing from OpenCV documentation)
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值