Search code examples
pythonopencvkubernetesazure-aksazure-machine-learning-service

Import cv2 error when deploying in Azure Kubernetes Service - python


I have read similar questions regarding azure app service, but I still can't find an answer. I was trying to deploy a model in azure kubernetes service, but I came across an error when importing cv2 (which is essential to me).

Opencv-python is included in my environment .yaml file:

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

I am deploying as follows:

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

And I get this error:

    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)

Solution

  • This might go wrong at some point, but my workaround was installing opencv-python-headless instead of opencv.

    In the environment .yaml file, just replace:

    - opencv-python>=4.1.2
    

    with:

    - opencv-python-headless