Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Build a Serverless Application for Image Label Detection

Sign upSign InSign upSign InAbhishek GuptaFollowITNEXT--ListenShareIn this blog post, you will learn how to build a Serverless solution to process images using Amazon Rekognition, AWS Lambda and the Go programming language. Images uploaded to Amazon Simple Storage Service (S3) will trigger a Lambda function which will detect labels (using the AWS Go SDK) and persist the image label data to an Amazon DynamoDB table.You will be using the Go programming language for the business logic (thanks to aws-lambda-go library) as well as the infrastructure component (Go bindings for AWS CDK) to deploy the solution.The code is available on GitHubAmazon Rekognition is a service that lets you analyze images and videos in your applications. You can identify objects, people, text, scenes, and activities, and detect inappropriate content. You can also do facial analysis, face comparison, and face search for various use cases like user verification and public safety. Amazon Rekognition is built on deep learning technology that doesn’t require machine learning expertise to use. It has an easy-to-use API that can analyze any image or video file in Amazon S3.Common use cases for using Amazon Rekognition include:Let’s learn Amazon Rekognition with a hands-on tutorial.Before you proceed, make sure you have the following installed:Clone the project and change to the right directory:The AWS Cloud Development Kit (AWS CDK) is a framework that lets you define your cloud infrastructure as code in one of its supported programming and provision it through AWS CloudFormation.To start the deployment, simply invoke cdk deploy and wait for a bit. You will see a list of resources that will be created and will need to provide your confirmation to proceed.Enter y to start creating the AWS resources required for the application.If you want to see the AWS CloudFormation template which will be used behind the scenes, run cdk synth and check the cdk.out folderYou can keep track of the stack creation progress in the terminal or navigate to AWS console: CloudFormation > Stacks > RekognitionLabelDetectionGolangStack.Once the stack creation is complete, you should have:You will also see the following output in the terminal (resource names will differ in your case). In this case, these are the names of the S3 buckets created by CDK:You can now try out the end to end solution!To try the solution, you can either use an image of your own or use the sample files provided in the GitHub repository. I will be used the S3 CLI to upload the file, but you can use the AWS console as well.This Lambda function will extract labels from the image and store them in a DynamoDB table.Upload another file:Check the DynamoDB table in the AWS console — you should see results of the label detection for both the images.DynamoDB table is designed with source file name as the partition key and (detected) label name as the sort key. This allows for a couple of query patterns:You can also use the CLI to scan the table:Once you’re done, to delete all the services, simply use:You were able to setup and try the complete solution. Before we wrap up, let’s quickly walk through some of important parts of the code to get a better understanding of what’s going on behind the scenes.We will only focus on the important parts — some of the code has been omitted for brevity.You can refer to the complete CDK code hereWe start by creating the source S3 bucket.Then, we create a DynamoDB table to store the label data for each image.Next, we create the Lambda function, passing the DynamoDB table name as an environment variable to the function. We also grant the function access to the DynamoDB table and the S3 bucket. We also grant the function access to the AmazonRekognitionReadOnlyAccess managed policy.We add an event source to the Lambda function to trigger it when a new file is uploaded to the source bucket.Finally, we export the bucket and DynamoDB table names as CloudFormation output.You can refer to the complete Lambda Function code hereThe Lambda function is triggered when a new image is uploaded to the source bucket. The function iterates through the list of files and calls the labelDetection function for each image.Let’s go through it.In this post, you saw how to create a serverless solution that detect image labels using Amazon Rekognition. The entire infrastructure life-cycle was automated using AWS CDK. All this was done using the Go programming language, which is well supported in AWS Lambda and AWS CDK.Here are a few things you can try out to extend this solution:Happy building!----ITNEXTPrincipal Developer Advocate at AWS | I ❤️ Databases, Go, KubernetesHelpStatusWritersBlogCareersPrivacyTermsAboutText to speechTeams



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Build a Serverless Application for Image Label Detection

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×