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)

猜你喜欢

转载自blog.csdn.net/xwd127429/article/details/121492199