无法在 AWS Lambda 中使用 OpenCV-Python

Rya*_*yan 8 opencv amazon-s3 amazon-web-services aws-lambda python-3.8

我一直在尝试将 OpenCV 放入 S3 存储桶中,然后将其分配给 lambda 层。

网上关于这个的很少,我所看到的没有奏效。

我已经成功地在亚马逊 linux 环境中使用了 docker,并遵循了本教程。https://aws.amazon.com/premiumsupport/knowledge-center/lambda-layer-simulated-docker/

我已将 setuptools、wheel 和 opencv-python==4.4.0.42 添加到 requirements.txt 文件中。

setuptools 和 wheel 因为之前的错误,建议在需要更新时包含这些,即使我已经更新了它们。但它适用于他们,所以谁知道。

创建了我已压缩并放入 S3 存储桶中的 docker 映像。

我一直收到 { "errorMessage": "Unable to import module 'lambda_function': libGL.so.1: cannot open shared object file: No such file or directory", "errorType": "Runtime.ImportModuleError" } 当我运行它尽管。

我似乎无法弄清楚出了什么问题。

有任何想法吗?

Mar*_*cin 14

You will need to add a bunch of dependencies to your layer. Below are the steps that I've used for opencv_python on lambda.

1. On local workstation (terminal window 1)

mkdir /tmp/mylayer && cd /tmp/mylayer

echo opencv-python==4.4.0.42 > ./requirements.txt
Run Code Online (Sandbox Code Playgroud)

2. On local workstation (terminal window 2)


docker run -it -v /tmp/mylayer:/mylayer  lambci/lambda:build-python3.8 bash
Run Code Online (Sandbox Code Playgroud)

The above command will put you into the docker container.

Inside the container:

cd /mylayer

pip install --no-deps -t python/lib/python3.8/site-packages/ -r requirements.txt

yum install -y mesa-libGL

cp -v /usr/lib64/libGL.so.1 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libGL.so.1.7.0 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libgthread-2.0.so.0 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libgthread-2.0.so.0 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libglib-2.0.so.0 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libGLX.so.0 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libX11.so.6 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libXext.so.6 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libGLdispatch.so.0 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libGLESv1_CM.so.1.2.0 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libGLX_mesa.so.0.0.0 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libGLESv2.so.2.1.0 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libxcb.so.1 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libXau.so.6 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /usr/lib64/libXau.so.6 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
cp -v /lib64/libGLdispatch.so.0.0.0 /mylayer/python/lib/python3.8/site-packages/opencv_python.libs/
Run Code Online (Sandbox Code Playgroud)

3. On local workstation again (terminal window 1)

Pack the python folder into mylayer.zip.

zip -r -9 mylayer.zip python
Run Code Online (Sandbox Code Playgroud)

In AWS console

  1. Create lambda layer based on mylayer.zip in the AWS Console. Don't forget to specify Compatible runtimes to python3.8.

  2. Add AWS provide SciPy layer AWSLambda-Python38-SciPy1x and your own layer with cv2 into your function.

So you will have two layers in your function.

  1. Perform basic test of the layer in lambda using the following lambda function:
import cv2

def lambda_handler(event, context):    
    print(dir(csv))
Run Code Online (Sandbox Code Playgroud)

The function executes correctly (partial printout shown).

slation3D', 'exp', 'extractChannel', 'fastAtan2', 'fastNlMeansDenoising', 'fastNlMeansDenoisingColored', 'fastNlMeansDenoisingColoredMulti', 'fastNlMeansDenoisingMulti', 'fillConvexPoly', 'fillPoly', 'filter2D', 'filterHomographyDecompByVisibleRefpoints', 'filterSpeckles', 'find4QuadCornerSubpix', 'findChessboardCorners', 'findChessboardCornersSB', 'findChessboardCornersSBWithMeta', 'findCirclesGrid', 'findContours', 'findEssentialMat', 'findFundamentalMat', 'findHomography', 'findNonZero', 'findTransformECC', 'fisheye', 'fitEllipse', 'fitEllipseAMS', 'fitEllipseDirect', 'fitLine', 'flann', 'flann_Index', 'flip', 'floodFill', 'gemm', 'getAffineTransform', 'getBuildInformation', 'getCPUFeaturesLine', 'getCPUTickCount', 'getDefaultNewCameraMatrix', 'getDerivKernels', 'getFontScaleFromHeight', 'getGaborKernel', 'getGaussianKernel', 'getHardwareFeatureName', 'getNumThreads', 'g
Run Code Online (Sandbox Code Playgroud)

  • 马尔辛,我爱你!!你不知道我为此苦苦挣扎了多少天。刚刚使用了这个,它工作正常,没有任何错误。谢谢你! (2认同)