Check out this video for step by step guide on Image recognition using AWS Lambda , Rekognition and other AWS services:

Get our AWS Lambda Beginners course on Udemy: https://www.udemy.com/lambda-course/?couponCode=MAHALESS

Code here:

from __future__ import print_function

import boto3
from decimal import Decimal
import json
import urllib

print('Loading function')

rekognition = boto3.client('rekognition')
client = boto3.client('sns')

def detect_labels(bucket, key):
response = rekognition.detect_labels(Image={"S3Object": {"Bucket": bucket, "Name": key}})

return response

# --------------- Main handler ------------------

def lambda_handler(event, context):

print (event)

bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))
try:

# Calls rekognition DetectLabels API to detect labels in S3 object
response = detect_labels(bucket, key)

tosend=""

for Label in response["Labels"]:
# print (Label["Name"] + Label["Confidence"])

print ('{0} - {1}%'.format(Label["Name"], Label["Confidence"]))
tosend+= '{0} - {1}% '.format(Label["Name"], Label["Confidence"])

message = client.publish(TargetArn='arn:aws:sns:us-east-1:771452637355:Image-recognition-sns', Message=tosend ,Subject='Uploaded Image Labels')

return "hello"
except Exception as e:
print(e)
print("Error processing object {} from bucket {}. ".format(key, bucket) +
"Make sure your object and bucket exist and your bucket is in the same region as this function.")
raise e
Tags:

2
Leave a Reply

Please Login to comment
  Subscribe  
newest oldest most voted
Notify of

I’m impressed, I must say. Really not often do I encounter a weblog that’s both educative and entertaining, and let me inform you, you have got hit the nail on the head. Your idea is outstanding; the difficulty is something that not enough individuals are talking intelligently about. I am very joyful that I stumbled throughout this in my seek for something relating to this.

I have recently started a website, the information you provide on this site has helped me tremendously. Thanks for all of your time & work.