无法在 AWS Lambda 中使用 OpenCV-Python
我一直在尝试将 OpenCV 放入 S3 存储桶中,然后将其分配给 lambda 层.
I've been trying to get OpenCV into an S3 bucket and then assign it to a lambda layer.
网上关于这方面的内容很少,我所看到的没有奏效.
Theres very little about this online and what I have seen hasn't worked.
我已经成功地在亚马逊 linux 环境中使用了 docker,并遵循了本教程.https://aws.amazon.com/premiumsupport/knowledge-center/lambda-layer-simulated-docker/
I've managed to use docker with the amazon linux environment, and followed this tutorial. https://aws.amazon.com/premiumsupport/knowledge-center/lambda-layer-simulated-docker/
我已将 setuptools、wheel 和 opencv-python==4.4.0.42 添加到 requirements.txt 文件中.
I've added setuptools, wheel and opencv-python==4.4.0.42 to the requirements.txt file.
setuptools 和 wheel 因为之前的错误,建议在需要更新时包含这些,即使我已经更新了它们.但它适用于他们,所以谁知道呢.
setuptools and wheel because of an earlier error where the recommendation was to include these as they need updating, even though I have updated them. But it works with them, so who knows.
创建了我已压缩并放入 S3 存储桶中的 docker 映像.
Created the docker image which I've zipped and put in an S3 bucket.
我一直在{errorMessage":无法导入模块‘lambda_function’:libGL.so.1:无法打开共享对象文件:没有这样的文件或目录",errorType":Runtime.ImportModuleError"}当我运行它时.
I keep getting { "errorMessage": "Unable to import module 'lambda_function': libGL.so.1: cannot open shared object file: No such file or directory", "errorType": "Runtime.ImportModuleError" } when I run it though.
我似乎无法弄清楚出了什么问题.
I can't seem to figure out what is wrong.
有什么想法吗?
您将需要向您的层添加一堆依赖项.以下是我使用过的步骤用于 lambda 上的 opencv_python.
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.
mkdir /tmp/mylayer && cd /tmp/mylayer
echo opencv-python==4.4.0.42 > ./requirements.txt
2.在本地工作站上(终端窗口 2)
docker run -it -v /tmp/mylayer:/mylayer lambci/lambda:build-python3.8 bash
上面的命令会将你放入 docker 容器中.
The above command will put you into the docker 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/
3.再次在本地工作站上(终端窗口 1)
将python
文件夹打包到mylayer.zip
中.
zip -r -9 mylayer.zip python
在 AWS 控制台中
在 AWS 控制台中基于
mylayer.zip
创建 lambda 层.不要忘记将Compatible runtimes
指定为python3.8
.
Create lambda layer based on
mylayer.zip
in the AWS Console. Don't forget to specifyCompatible runtimes
topython3.8
.
将 AWS 提供的 SciPy 层 AWSLambda-Python38-SciPy1x
和您自己的带有 cv2 的层添加到您的函数中.
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.
- 使用以下 lambda 函数在 lambda 中执行层的基本测试:
import cv2
def lambda_handler(event, context):
print(dir(csv))
函数正确执行(显示部分打印输出).
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