Python:如何在谷歌云功能上安装OpenCV



我使用的是谷歌云功能Gen2和Python 3.10以及最新版本的gcloud-cli和opencv-python==4.6.0.66functions-framework==3.2.0

我无法在谷歌云上部署,因为import cv2导致了这个错误

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 )

如果我使用opencv-python-headless,我会得到这个错误

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是必备的要求。这个错误可以修复吗?还是应该将项目部署到其他地方?

问题的总结

libgl的第一个错误可以通过安装opencv-python headless(这只是CPU,不需要libgl(来解决

pyzbar同样需要安装zbar。(在云功能环境中无法完成(