无法在AWS Lambda中使用OpenCV-Python [英] Can't use OpenCV-Python in AWS Lambda

查看:129
本文介绍了无法在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.

我设法在docker 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/

我已经在setup.txt文件中添加了setuptools,wheel和opencv-python == 4.4.0.42.

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控制台中

  1. 在AWS Console中基于 mylayer.zip 创建lambda层.不要忘记为 python3.8 指定兼容运行时.

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

添加AWS为您提供的功能提供SciPy层 AWSLambda-Python38-SciPy1x 和您自己的cv2层.

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

因此您的功能将具有两层.

  1. 使用以下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

这篇关于无法在AWS Lambda中使用OpenCV-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆