Skip to content

Instantly share code, notes, and snippets.

@elliotforbes
Created February 25, 2018 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elliotforbes/ae4d75d427d897b83d477ea60a01ddbf to your computer and use it in GitHub Desktop.
Save elliotforbes/ae4d75d427d897b83d477ea60a01ddbf to your computer and use it in GitHub Desktop.
import boto3
import os
import uuid
rekognition = boto3.client('rekognition', 'eu-west-1')
dynamo = boto3.resource('dynamodb', 'eu-west-1')
def tagImage(event, content):
key = uuid.uuid4().hex
location = event['Records'][0]['s3']['object']['key']
table = dynamo.Table(os.getenv('TABLE', ''))
labels = []
for label in detect_labels(location):
labels.append(label['Name'])
table.put_item(
Item={
'key': key,
'location': location,
'labels': labels
}
)
response = {
"statusCode": 200,
"body": "It Worked!"
}
return response
def detect_labels(key, max_labels=4, min_confidence=40):
response = rekognition.detect_labels(
Image={
"S3Object": {
"Bucket": os.getenv('BUCKET', ''),
"Name": key,
}
},
MaxLabels=max_labels,
MinConfidence=min_confidence
)
return response['Labels']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment