在 Azure Kubernetes 服务中部署时导入 cv2 错误 - python

Import cv2 error when deploying in Azure Kubernetes Service - python

我已经阅读过有关 azure 应用程序服务的类似问题,但我仍然找不到答案。我试图在 azure kubernetes 服务中部署模型,但在导入 cv2 时遇到错误(这对我来说很重要)。

Opencv-python包含在我的环境.yaml文件中:

name: project_environment
dependencies:
  # The python interpreter version.
  # Currently Azure ML only supports 3.5.2 and later.
- python=3.6.2

- pip:
  # You must list azureml-defaults as a pip dependency
  - azureml-defaults>=1.0.45
  - Cython
  - matplotlib>=3.2.2
  - numpy>=1.18.5
  - opencv-python>=4.1.2
  - pillow
  - PyYAML>=5.3
  - scipy>=1.4.1
  - torch>=1.6.0
  - torchvision>=0.7.0
  - tqdm>=4.41.0
channels:
- conda-forge

我正在部署如下:

aks_service = Model.deploy(ws,
                       models=[model],
                       inference_config=inference_config,
                       deployment_config=gpu_aks_config,
                       deployment_target=aks_target,
                       name=aks_service_name)

我收到这个错误:

    Traceback (most recent call last):
  File "/azureml-envs/azureml_659b55e5b05510a45f41f0ca31d3ac02/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/azureml-envs/azureml_659b55e5b05510a45f41f0ca31d3ac02/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/azureml-envs/azureml_659b55e5b05510a45f41f0ca31d3ac02/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/azureml-envs/azureml_659b55e5b05510a45f41f0ca31d3ac02/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/azureml-envs/azureml_659b55e5b05510a45f41f0ca31d3ac02/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/azureml-envs/azureml_659b55e5b05510a45f41f0ca31d3ac02/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/azureml-envs/azureml_659b55e5b05510a45f41f0ca31d3ac02/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
  File "/var/azureml-server/wsgi.py", line 1, in <module>
    import create_app
  File "/var/azureml-server/create_app.py", line 3, in <module>
    from app import main
  File "/var/azureml-server/app.py", line 32, in <module>
    from aml_blueprint import AMLBlueprint
  File "/var/azureml-server/aml_blueprint.py", line 23, in <module>
    main_module_spec.loader.exec_module(main)
  File "/var/azureml-app/score.py", line 8, in <module>
    import cv2
  File "/azureml-envs/azureml_659b55e5b05510a45f41f0ca31d3ac02/lib/python3.6/site-packages/cv2/__init__.py", line 5, in <module>
    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory
Worker exiting (pid: 41)

能否访问要安装的节点的终端? 如果是,请执行以下步骤:

sudo apt update
sudo apt install libgl1-mesa-glx

有时这可能会出错,但我的解决方法是安装 opencv-python-headless 而不是 opencv。

在环境.yaml文件中,只需替换:

- opencv-python>=4.1.2

与:

- opencv-python-headless