Skip to content

Commit

Permalink
[setup] create rekognition example with s3
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnaloo committed Mar 29, 2017
1 parent 19095c5 commit 92631fd
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aws-node-rekognition-analysis-s3-image/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.serverless
91 changes: 91 additions & 0 deletions aws-node-rekognition-analysis-s3-image/README.md
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).
28 changes: 28 additions & 0 deletions aws-node-rekognition-analysis-s3-image/handler.js
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 aws-node-rekognition-analysis-s3-image/lib/imageAnalyser.js
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;
10 changes: 10 additions & 0 deletions aws-node-rekognition-analysis-s3-image/package.json
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"
}
}
6 changes: 6 additions & 0 deletions aws-node-rekognition-analysis-s3-image/post.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"body": {
"bucket": "mycatphotos",
"imageName": "cat3.jpg"
}
}
27 changes: 27 additions & 0 deletions aws-node-rekognition-analysis-s3-image/serverless.yml
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

0 comments on commit 92631fd

Please sign in to comment.