forked from serverless/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[setup] create rekognition example with s3
- Loading branch information
Showing
7 changed files
with
199 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
.serverless |
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,91 @@ | ||
# Analyse Image from S3 with Amazon Rekognition Example | ||
|
||
This example shows how to analys an image in an S3 bucket with Amazon Rekognition and return a list of labels. | ||
|
||
## Use-cases | ||
|
||
- Determine if there is a cat in an image. | ||
|
||
## Setup | ||
|
||
You need to create an S3 bucket and upload at least one file. Be sure the permissions on the folder and file allow public access and that CORS is configured to allow access. | ||
|
||
Replace the values in `post.json` with the bucket and image name from S3. | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
## Deploy | ||
|
||
In order to deploy the function run: | ||
|
||
```bash | ||
serverless deploy | ||
``` | ||
|
||
The expected result should be similar to: | ||
|
||
```bash | ||
Serverless: Packaging service... | ||
Serverless: Uploading CloudFormation file to S3... | ||
Serverless: Uploading service .zip file to S3 (3.78 MB)... | ||
Serverless: Updating Stack... | ||
Serverless: Checking Stack update progress... | ||
.............. | ||
Serverless: Stack update finished... | ||
Service Information | ||
service: rekognition-analysis-s3-image | ||
stage: dev | ||
region: us-east-1 | ||
api keys: | ||
None | ||
endpoints: | ||
POST - https://6bbhhv5q22.execute-api.us-east-1.amazonaws.com/dev/analysis | ||
functions: | ||
imageAnalysis: rekognition-analysis-s3-image-dev-imageAnalysis | ||
``` | ||
|
||
## Usage | ||
|
||
You can now send an HTTP POST request directly to the endpoint using a tool like curl | ||
|
||
```json | ||
{ | ||
"bucket": "mycatphotos", | ||
"imageName": "cat.jpg" | ||
} | ||
``` | ||
|
||
```bash | ||
serverless invoke local -f imageAnalysis -p post.json | ||
``` | ||
|
||
The expected result should be similar to: | ||
|
||
```json | ||
{ | ||
"Labels": [ | ||
{ | ||
"Confidence": 96.59198760986328, | ||
"Name": "Animal" | ||
}, | ||
{ | ||
"Confidence": 96.59198760986328, | ||
"Name": "Cat" | ||
}, | ||
{ | ||
"Confidence": 96.59198760986328, | ||
"Name": "Pet" | ||
}, | ||
{ | ||
"Confidence": 96.59198760986328, | ||
"Name": "Siamese" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Scaling | ||
|
||
By default, AWS Lambda limits the total concurrent executions across all functions within a given region to 100. The default limit is a safety limit that protects you from costs due to potential runaway or recursive functions during initial development and testing. To increase this limit above the default, follow the steps in [To request a limit increase for concurrent executions](http://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html#increase-concurrent-executions-limit). |
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,28 @@ | ||
'use strict'; | ||
|
||
const ImageAnalyser = require('./lib/imageAnalyser'); | ||
|
||
/** | ||
Analyse an image on S3 using bucket and image name | ||
*/ | ||
module.exports.imageAnalysis = (event, context, callback) => { | ||
const data = event.body; | ||
|
||
const s3Config = { | ||
bucket: data.bucket, | ||
imageName: data.imageName, | ||
}; | ||
|
||
return ImageAnalyser | ||
.getImageLabels(s3Config) | ||
.then((labels) => { | ||
const response = { | ||
statusCode: 200, | ||
body: JSON.stringify({ Labels: labels }), | ||
}; | ||
callback(null, response); | ||
}) | ||
.catch((error) => { | ||
callback(error, null); | ||
}); | ||
}; |
35 changes: 35 additions & 0 deletions
35
aws-node-rekognition-analysis-s3-image/lib/imageAnalyser.js
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,35 @@ | ||
'use strict'; | ||
|
||
const AWS = require('aws-sdk'); | ||
|
||
const rek = new AWS.Rekognition(); | ||
|
||
class ImageAnalyser { | ||
|
||
static getImageLabels(s3Config) { | ||
const params = { | ||
Image: { | ||
S3Object: { | ||
Bucket: s3Config.bucket, | ||
Name: s3Config.imageName, | ||
}, | ||
}, | ||
MaxLabels: 10, | ||
MinConfidence: 50, | ||
}; | ||
|
||
console.log(`Analyzing file: https://s3.amazonaws.com/${s3Config.bucket}/${s3Config.imageName}`); | ||
|
||
return new Promise((resolve, reject) => { | ||
rek.detectLabels(params, (err, data) => { | ||
if (err) { | ||
return reject(new Error(err)); | ||
} | ||
console.log('Analysis labels:', data.Labels); | ||
return resolve(data.Labels); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = ImageAnalyser; |
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,10 @@ | ||
{ | ||
"name": "aws-node-rekognition-analysis-s3-image", | ||
"description": "Analyse an Image from an S3 Bucket with Amazon Rekognition", | ||
"version": "1.0.0", | ||
"author": "lynnaloo", | ||
"license": "MIT", | ||
"dependencies": { | ||
"aws-sdk": "^2.32.0" | ||
} | ||
} |
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,6 @@ | ||
{ | ||
"body": { | ||
"bucket": "mycatphotos", | ||
"imageName": "cat3.jpg" | ||
} | ||
} |
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,27 @@ | ||
service: rekognition-analysis-s3-image | ||
|
||
frameworkVersion: ">=1.8.0" | ||
|
||
provider: | ||
name: aws | ||
runtime: nodejs4.3 | ||
stage: dev | ||
region: us-east-1 | ||
|
||
iamRoleStatements: | ||
- Effect: Allow | ||
Action: | ||
- s3:* | ||
Resource: "*" | ||
- Effect: "Allow" | ||
Action: | ||
- "rekognition:*" | ||
Resource: "*" | ||
|
||
functions: | ||
imageAnalysis: | ||
handler: handler.imageAnalysis | ||
events: | ||
- http: | ||
path: analysis | ||
method: post |