Python: How to Install OpenCV on Google cloud functions?

Question:

I’m using Google cloud functions Gen2 and Python 3.10 and the latest version of gcloud cli and opencv-python==4.6.0.66 and functions-framework==3.2.0

I can’t deploy on google cloud, because import cv2 is causing this error

ImportError: libGL.so.1: cannot open shared object file: No such file or directory
at .import_module ( /opt/python3.10/lib/python3.10/importlib/__init__.py:126 )
at .bootstrap ( /layers/google.python.pip/pip/lib/python3.10/site-packages/cv2/__init__.py:153 )
at .<module> ( /layers/google.python.pip/pip/lib/python3.10/site-packages/cv2/__init__.py:181 )
at .<module> ( /workspace/functions.py:20 )
at .<module> ( /workspace/endpoints.py:9 )
at .<module> ( /workspace/main.py:2 )
at ._call_with_frames_removed ( <frozen importlib._bootstrap>:241 )
at .exec_module ( <frozen importlib._bootstrap_external>:883 )
at .create_app ( /layers/google.python.pip/pip/lib/python3.10/site-packages/functions_framework/__init__.py:289 )
at ._cli ( /layers/google.python.pip/pip/lib/python3.10/site-packages/functions_framework/_cli.py:37 )
at .invoke ( /layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py:760 )
at .invoke ( /layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py:1404 )
at .main ( /layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py:1055 )
at .__call__ ( /layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py:1130 )
at .<module> ( /layers/google.python.pip/pip/bin/functions-framework:8 )

If I use opencv-python-headless instead, I get this error

ImportError: Unable to find zbar shared library
at .load ( /layers/google.python.pip/pip/lib/python3.10/site-packages/pyzbar/zbar_library.py:65 )
at .load_libzbar ( /layers/google.python.pip/pip/lib/python3.10/site-packages/pyzbar/wrapper.py:127 )
at .zbar_function ( /layers/google.python.pip/pip/lib/python3.10/site-packages/pyzbar/wrapper.py:148 )
at .<module> ( /layers/google.python.pip/pip/lib/python3.10/site-packages/pyzbar/wrapper.py:151 )
at .<module> ( /layers/google.python.pip/pip/lib/python3.10/site-packages/pyzbar/pyzbar.py:7 )
at .<module> ( /workspace/functions.py:22 )
at .<module> ( /workspace/endpoints.py:9 )
at .<module> ( /workspace/main.py:2 )
at ._call_with_frames_removed ( <frozen importlib._bootstrap>:241 )
at .exec_module ( <frozen importlib._bootstrap_external>:883 )
at .create_app ( /layers/google.python.pip/pip/lib/python3.10/site-packages/functions_framework/__init__.py:289 )
at ._cli ( /layers/google.python.pip/pip/lib/python3.10/site-packages/functions_framework/_cli.py:37 )
at .invoke ( /layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py:760 )
at .invoke ( /layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py:1404 )
at .main ( /layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py:1055 )
at .__call__ ( /layers/google.python.pip/pip/lib/python3.10/site-packages/click/core.py:1130 )
at .<module> ( /layers/google.python.pip/pip/bin/functions-framework:8 )

OpenCV is a must-have requirement. Can this error be fixed or should I deploy the project elsewhere?

Asked By: Lynob

||

Answers:

Sum up of the problem

First error with libgl can be solved by installing opencv-python-headless (which is CPU only, and doesn’t require libgl)

pyzbar in the same way requires zbar to be installed.
(can’t be done in cloud functions environment)

Answered By: aramcpp