Opencv and AWS Lambda

自古美人都是妖i 提交于 2020-01-01 14:24:19

问题


I am writing a lambda function in Java ad want to use the opencv library. I am having a hard time understanding how to set it up. Please help. I can set it up just fine locally but I am really confused about the lambda part.

Edit: To be precise I am having trouble with System.LoadLibrary() function. The dll is in the project files but lambda cannot find it.


回答1:


I recommend using Node.js instead of Java because Lambdas work better with Node.js.

Your code will look like this:

cv = require('opencv');
//do something with cv...

But before being able to run the code you should install OpenCV and its node package:

npm install opencv

This will create a node_modules folder. Once ran your code locally, you need to do one extra step and then zip up everything (i.e. your-code.js and node_modules folder) into lambda-package.zip and upload it to an AWS Lambda.

Extra step for Lambda:

Lambda code runs in Amazon Linux environment (not your local environment). That means you should build the node packages in that environment. You may launch an EC2 instance and do the npm install part in that machine or you may use a docker image.

Here's a nice article about this:

https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/




回答2:


Opencv is dependent on underlying OS library files. For windows we need to have opencv_javaXXX.dll file and for windows we need libOpencv_javaXXX.so file (where XXX is the opencv version). In case if you don't have these files please generate using this link http://docs.opencv.org/2.4/doc/tutorials/introduction/desktop_java/java_dev_intro.html

AWS lambda basically uses an AMI name: amzn-ami-hvm-2016.03.3.x86_64-gp2. This is a 64 bit linux machine. To get the libOpencv_javaXXX.so I spinned up an EC2 instance using AMI name: amzn-ami-hvm-2016.03.3.x86_64-gp2 (This is a public AMI on amazon) and installed opencv for java on this machine.

Once you have libOpencv_javaXXX.so , add it to classpath (I added it to src/main/resources) and then use following code to load it from JVM process.

System.load(new ClassPathResource("/libopencv_javaXXX.so").getFile().getAbsolutePath());

System.LoadLibrary() loads the library from java lib path and System.Load() will load the native library from the absolute path.



来源:https://stackoverflow.com/questions/38624370/opencv-and-aws-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!