├── .editorconfig
├── LICENSE
├── README.md
├── build.sh
└── template.py
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | end_of_line = lf
5 | insert_final_newline = true
6 | trim_trailing_whitespace = true
7 |
8 | indent_style = tab
9 | indent_size = tab
10 | tab_width = 4
11 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2016 - Antoine Eddi
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in
13 | all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 | THE SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Python OpenCV module for AWS Lambda
2 |
3 |
4 | 
5 | Simple Mustache Service is an AWS Lambda demo that uses this module
6 |
7 |
8 | ## Description
9 | This is a simple script that builds a deployment package including OpenCV compatible with the AWS Lambda Python runtime. The dynamic library is compiled with all extended instruction sets supported by Lambda CPU and binaries are stripped to save space. You simply need to add your code inside *lambda_function.py* and possibly your haar cascades files or additional Python modules. You can directly download a [pre-built archive](https://github.com/aeddi/aws-lambda-python-opencv/releases/download/Prebuilt/aws-lambda-python-opencv-prebuilt.zip).
10 |
11 | - Build duration: ~20 min on T2.micro / ~15 min on C4.2xlarge
12 | - Package size without haar cascades included: 26MB
13 | - OpenCV 3.2 by default but may work with newer
14 |
15 | **Needs to be built on an Amazon Linux instance.**
16 |
17 | ## Module building
18 | ### Option 1: with an existing instance
19 | - Download the repo `wget https://github.com/aeddi/aws-lambda-python-opencv/archive/master.zip`
20 | - Unzip the archive `unzip master.zip`
21 | - Launch the script `cd aws-lambda-python-opencv-master && ./build.sh`
22 |
23 | ### Option 2: without an existing instance
24 | In the EC2 console, launch a new instance with:
25 | - Amazon Linux AMI
26 | - Role with S3 write permission
27 | - Shutdown behavior: *Terminate*
28 | - Paste the script below in the user data text field
29 | ```bash
30 | #!/bin/bash
31 | yum update -y
32 | yum install -y git cmake gcc-c++ gcc python-devel chrpath
33 |
34 | cd /tmp
35 | wget https://github.com/aeddi/aws-lambda-python-opencv/archive/master.zip
36 | unzip master.zip
37 | chmod 777 aws-lambda-python-opencv-master
38 | cd aws-lambda-python-opencv-master
39 | su -c './build.sh' ec2-user
40 |
41 | aws s3 cp lambda-package.zip s3://
42 | shutdown -h now
43 | ```
44 | - Replace *my-target-bucket* by a bucket or your choice
45 | - Less than 30 min later the instance will be terminated and the archive will be available on your bucket
46 |
--------------------------------------------------------------------------------
/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 |
3 | # Setting up build env
4 | sudo yum update -y
5 | sudo yum install -y git cmake gcc-c++ gcc python-devel chrpath
6 | mkdir -p lambda-package/cv2 build/numpy
7 |
8 | # Build numpy
9 | pip install --install-option="--prefix=$PWD/build/numpy" numpy
10 | cp -rf build/numpy/lib64/python2.7/site-packages/numpy lambda-package
11 |
12 | # Build OpenCV 3.2
13 | (
14 | NUMPY=$PWD/lambda-package/numpy/core/include
15 | cd build
16 | git clone https://github.com/Itseez/opencv.git
17 | cd opencv
18 | git checkout 3.2.0
19 | cmake \
20 | -D CMAKE_BUILD_TYPE=RELEASE \
21 | -D WITH_TBB=ON \
22 | -D WITH_IPP=ON \
23 | -D WITH_V4L=ON \
24 | -D ENABLE_AVX=ON \
25 | -D ENABLE_SSSE3=ON \
26 | -D ENABLE_SSE41=ON \
27 | -D ENABLE_SSE42=ON \
28 | -D ENABLE_POPCNT=ON \
29 | -D ENABLE_FAST_MATH=ON \
30 | -D BUILD_EXAMPLES=OFF \
31 | -D BUILD_TESTS=OFF \
32 | -D BUILD_PERF_TESTS=OFF \
33 | -D PYTHON2_NUMPY_INCLUDE_DIRS="$NUMPY" \
34 | .
35 | make -j`cat /proc/cpuinfo | grep MHz | wc -l`
36 | )
37 | cp build/opencv/lib/cv2.so lambda-package/cv2/__init__.so
38 | cp -L build/opencv/lib/*.so.3.2 lambda-package/cv2
39 | strip --strip-all lambda-package/cv2/*
40 | chrpath -r '$ORIGIN' lambda-package/cv2/__init__.so
41 | touch lambda-package/cv2/__init__.py
42 |
43 | # Copy template function and zip package
44 | cp template.py lambda-package/lambda_function.py
45 | cd lambda-package
46 | zip -r ../lambda-package.zip *
47 |
--------------------------------------------------------------------------------
/template.py:
--------------------------------------------------------------------------------
1 | import cv2
2 |
3 | def lambda_handler(event, context):
4 | print "OpenCV installed version:", cv2.__version__
5 | return "It works!"
6 |
7 | if __name__ == "__main__":
8 | lambda_handler(42, 42)
9 |
--------------------------------------------------------------------------------