forked from danilop/AWS_Lambda_in_Action
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
202 changed files
with
8,044 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
exports.handler = function(event, context) { | ||
context.succeed(event.myip); | ||
exports.handler = (event, context, callback) => { | ||
callback(null, event.myip); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Sid": "", | ||
"Effect": "Allow", | ||
"Principal": "*", | ||
"Action": "s3:GetObject", | ||
"Resource": "arn:aws:s3:::<BUCKET-NAME>/*" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
var cv = require('opencv'); | ||
var util = require('util'); | ||
var request = require('request').defaults({ encoding: null }); | ||
var uuid = require('node-uuid'); | ||
var AWS = require('aws-sdk'); | ||
|
||
var s3 = new AWS.S3(); | ||
|
||
var dstBucket = 'danilop-eventdrivenapps'; | ||
var dstPrefix = 'tmp/'; | ||
var outputDomain = 'eventdrivenapps.com'; | ||
|
||
function getFormattedDate() { | ||
var now = new Date().toISOString(); // YYYY-MM-DDTHH:mm:ss.sssZ | ||
var formattedNow = now.substr(0,4) + now.substr(5,2) + now.substr(8,2) | ||
+ now.substr(11,2) + now.substr(14,2) + now.substr(17,2); | ||
return formattedNow; | ||
} | ||
|
||
exports.handler = (event, context, callback) => { | ||
console.log("Reading options from event:\n", util.inspect(event, {depth: 5})); | ||
var imageUrl = event.imageUrl; | ||
request.get(imageUrl, function (err, res, body) { | ||
if (err) { | ||
console.log(err); | ||
callback(err); | ||
} | ||
cv.readImage(body, function(err, im) { | ||
if (err) { | ||
console.log(err); | ||
callback(err); | ||
} | ||
if (im.width() < 1 || im.height() < 1) callback('Image has no size'); | ||
im.detectObject("node_modules/opencv/data/haarcascade_frontalface_alt.xml", {}, function(err, faces) { | ||
if (err) callback(err); | ||
for (var i = 0; i < faces.length; i++){ | ||
var face = faces[i]; | ||
im.rectangle([face.x, face.y], [face.width, face.height], [255, 255, 255], 2); | ||
} | ||
if (faces.length > 0) { | ||
var dstKey = dstPrefix + getFormattedDate() + '-' + uuid.v4() + '.jpg'; | ||
var contentType = 'image/jpeg'; | ||
s3.putObject({ | ||
Bucket: dstBucket, | ||
Key: dstKey, | ||
Body: im.toBuffer(), | ||
ContentType: contentType | ||
}, function(err, data) { | ||
if (err) console.log(err); | ||
if (err) callback(err); | ||
console.log(data); | ||
outputUrl = 'https://' + outputDomain + '/' + dstKey; | ||
var result = { | ||
faces: faces.length, | ||
outputUrl: outputUrl | ||
}; | ||
callback(null, result); | ||
}); | ||
} else { | ||
var result = { | ||
faces: 0, | ||
outputUrl: imageUrl | ||
}; | ||
callback(null, result); | ||
} | ||
}); | ||
}); | ||
}); | ||
} |
58 changes: 58 additions & 0 deletions
58
Chapter05/FaceDetection/fn/faceDetection-py/lambda_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from __future__ import print_function | ||
|
||
import numpy | ||
import cv2 | ||
import json | ||
import urllib2 | ||
import uuid | ||
import datetime | ||
import boto3 | ||
|
||
print('Loading function') | ||
|
||
dstBucket = 'danilop-eventdrivenapps' | ||
dstPrefix = 'tmp/' | ||
outputDomain = 'eventdrivenapps.com' | ||
|
||
cascPath = 'share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml' | ||
|
||
s3 = boto3.resource('s3') | ||
faceCascade = cv2.CascadeClassifier(cascPath) | ||
|
||
def lambda_handler(event, context): | ||
print('Received event: ' + json.dumps(event, indent=2)) | ||
imageUrl = event['imageUrl'] | ||
|
||
imageFile = urllib2.urlopen(imageUrl) | ||
|
||
imageBytes = numpy.asarray(bytearray(imageFile.read()), dtype=numpy.uint8) | ||
image = cv2.imdecode(imageBytes, cv2.CV_LOAD_IMAGE_UNCHANGED) | ||
|
||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | ||
faces = faceCascade.detectMultiScale( | ||
gray, | ||
scaleFactor=1.1, | ||
minNeighbors=5, | ||
minSize=(30, 30), | ||
flags = cv2.cv.CV_HAAR_SCALE_IMAGE | ||
) | ||
|
||
for (x, y, w, h) in faces: | ||
cv2.rectangle(image, (x, y), (x+w, y+h), (255, 255, 255), 2) | ||
|
||
r, outputImage = cv2.imencode('.jpg', image) | ||
if False==r: | ||
raise Exception('Error encoding image') | ||
|
||
dstKey = dstPrefix + datetime.datetime.now().strftime('%Y%m%d%H%M%S') + '-' + str(uuid.uuid4()) + '.jpg' | ||
|
||
s3.Bucket(dstBucket).put_object(Key=dstKey, | ||
Body=outputImage.tostring(), | ||
ContentType='image/jpeg' | ||
) | ||
|
||
outputUrl = 'https://' + outputDomain + '/' + dstKey | ||
|
||
result = { 'faces': len(faces), 'outputUrl': outputUrl } | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Initialize the Amazon Cognito credentials provider | ||
AWS.config.region = 'eu-west-1'; // Region | ||
AWS.config.credentials = new AWS.CognitoIdentityCredentials({ | ||
IdentityPoolId: 'eu-west-1:40f0910c-72f5-4f73-83bc-89f44d0afe92', | ||
}); | ||
|
||
var lambda = new AWS.Lambda(); | ||
|
||
function returnFaces() { | ||
var imageUrl = null; | ||
var input; | ||
if (document.getElementById('monalisa').checked) { | ||
imageUrl = 'https://eventdrivenapps.com/examples/FaceRecognition/monalisa.jpg'; | ||
} else if (document.getElementById('astronauts').checked) { | ||
imageUrl = 'https://eventdrivenapps.com/examples/FaceRecognition/astronauts.jpg'; | ||
} else { | ||
imageUrl = document.getElementById('customUrl').value; | ||
} | ||
if (imageUrl == null || imageUrl == '') { | ||
result.innerHTML = | ||
'<div class="alert alert-danger">Please provide a URL</div>'; | ||
return; | ||
} else { | ||
input = { | ||
imageUrl: imageUrl | ||
}; | ||
} | ||
lambda.invoke({ | ||
FunctionName: 'faceRecognition', | ||
Payload: JSON.stringify(input) | ||
}, function(err, data) { | ||
var result = document.getElementById('result'); | ||
if (err) { | ||
console.log(err, err.stack); | ||
result.innerHTML = | ||
'<div class="alert alert-danger">' + err + '</div>'; | ||
} else { | ||
var output = JSON.parse(data.Payload); | ||
var formattedOutput = '<div class="alert alert-success">'; | ||
if (output.faces < 1) { | ||
formattedOutput += 'There are no faces'; | ||
} else if (output.faces == 1) { | ||
formattedOutput += 'There is 1 face'; | ||
} else { | ||
formattedOutput += 'There are ' + output.faces + ' faces'; | ||
} | ||
formattedOutput += ' in the picture.</div>' + | ||
'<div class="row">' + | ||
'<div class="col-sm-6"><img src="' + imageUrl + '"/></div>' + | ||
'<div class="col-sm-6"><img src="' + output.outputUrl + '"/></div>'; | ||
'<div/>'; | ||
result.innerHTML = formattedOutput; | ||
} | ||
}); | ||
} | ||
|
||
var form = document.getElementById('inputForm'); | ||
form.addEventListener('submit', function(evt) { | ||
evt.preventDefault(); | ||
returnFaces(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<html lang="en"> | ||
<head> | ||
<title>Face Recognition - AWS Lambda in Action</title> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<!-- JQuery - required by Boostrap --> | ||
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> | ||
<!-- Bootstrap --> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous"> | ||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> | ||
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.2.32.min.js"></script> | ||
<style> | ||
img { | ||
width: 100%; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<div class="jumbotron"> | ||
<h2>AWS Lambda in Action</h2> | ||
<h1>Face Recognition</h1> | ||
<p>This is an example of calling an AWS Lambda function | ||
to apply face recognition to a picture using | ||
<a href="http://opencv.org">OpenCV</a>.</p> | ||
<p>Please choose one of the sample pictures | ||
or provide the URL of a picture of your choice.</p> | ||
</div> | ||
<form role="form" id="inputForm"> | ||
<div class="radio"> | ||
<label><input type="radio" name="imageRadio" id="monalisa" checked>Mona Lisa</label> | ||
</div> | ||
<div class="radio"> | ||
<label><input type="radio" name="imageRadio" id="astronauts">Astronauts</label> | ||
</div> | ||
<div class="radio"> | ||
<label><input type="radio" name="imageRadio" id="custom">Image URL | ||
<input type="text" class="form-control s" id="customUrl"></label> | ||
</div> | ||
<button type="submit" class="btn btn-default">Look for Faces</button> | ||
</form> | ||
<div id="result"> | ||
</div> | ||
</div> | ||
<!-- This is where AWS Lambda is invoked --> | ||
<script src="faceRecognition.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"Version": "2012-10-17", | ||
"Statement": [ | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"s3:ListBucket" | ||
], | ||
"Resource": [ | ||
"arn:aws:s3:::<BUCKET-NAME>/tmp/" | ||
], | ||
"Condition": { | ||
"StringLike": { "s3:prefix": [ "tmp/*" ] } | ||
} | ||
}, | ||
{ | ||
"Effect": "Allow", | ||
"Action": [ | ||
"s3:DeleteObject" | ||
], | ||
"Resource": [ | ||
"arn:aws:s3:::<BUCKET-NAME>/tmp/*" | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
var AWS = require('aws-sdk'); | ||
var util = require('util'); | ||
|
||
var s3 = new AWS.S3(); | ||
|
||
var dstBucket = 'danilop-eventdrivenapps'; | ||
var dstPrefix = 'tmp/'; | ||
var maxElapsedInSeconds = 3600; | ||
|
||
var dstPrefixLength = dstPrefix.length; | ||
|
||
function getObjectKeys(marker) { | ||
var params = { | ||
Bucket: dstBucket, | ||
Prefix: dstPrefix | ||
}; | ||
if (marker !== null) { | ||
params.Marker = marker; | ||
} | ||
console.log(params); | ||
s3.listObjects(params, function(err, data) { | ||
if (err) { | ||
console.log(err, err.stack); | ||
} else { | ||
if (data.IsTruncated) { | ||
getObjectKeys(data.NextMarker); | ||
} | ||
data.Contents.forEach(function(item) { | ||
var fileName = item.Key; | ||
var fileDate = new Date( | ||
fileName.substr(dstPrefixLength,4), | ||
fileName.substr(dstPrefixLength + 4,2) - 1, | ||
fileName.substr(dstPrefixLength + 6,2), | ||
fileName.substr(dstPrefixLength + 8,2), | ||
fileName.substr(dstPrefixLength + 10,2), | ||
fileName.substr(dstPrefixLength + 12,2) | ||
); | ||
var elapsedInSeconds = (now - fileDate) / 1000; | ||
if (elapsedInSeconds > maxElapsedInSeconds) { | ||
var params = { | ||
Bucket: dstBucket, | ||
Key: fileName | ||
}; | ||
s3.deleteObject(params, function(err, data) { | ||
if (err) { | ||
console.log(err, err.stack); | ||
} else { | ||
console.log('Deleted ' + fileName); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
exports.handler = (event, context) => { | ||
console.log("Reading options from event:\n", util.inspect(event, {depth: 5})); | ||
|
||
now = new Date(); | ||
console.log('Now is ' + now.toISOString()); | ||
|
||
getObjectKeys(null); | ||
}; |
Oops, something went wrong.