Skip to content

This project contains source code and supporting files for a serverless application which can be used for Computer Vision inferencing using Amazon Lookout for Vision

License

Notifications You must be signed in to change notification settings

aws-samples/amazon-lookout-for-vision-batch-processing

Batch Image Processing with Amazon Lookout for Vision

Amazon Lookout for Vision is a machine learning (ML) service that spots defects and anomalies in visual representations using computer vision (CV). With Amazon Lookout for Vision, manufacturing companies can increase quality and reduce operational costs by quickly identifying differences in images of objects at scale. For example, Amazon Lookout for Vision can be used to identify missing components in products, damage to vehicles or structures, irregularities in production lines, miniscule defects in silicon wafers, and other similar problems.

Amazon Lookout for Vision uses ML to see and understand images from any camera as a person would, but with an even higher degree of accuracy and at a much larger scale. Amazon Lookout for Vision allows you to eliminate the need for costly and inconsistent manual inspection, while improving quality control, defect and damage assessment, and compliance. In minutes, you can begin using Amazon Lookout for Vision to automate inspection of images and objects–with no machine learning expertise required.

To use this solution you will:

  1. Collect images that show normal and defective products from your production line and load them in to the Amazon Lookout for Vision console.

  2. Label images as normal or anomalous and Lookout for Vision to automatically build a model for you. Tune your model to improve defect detection by adding images to the dataset.

  3. Use the Amazon Lookout for Vision dashboard to monitor defects and improve processes.

  4. Automate visual inspection processes in real-time or in-batch and receive notifications when defects are detected.

  5. Make continuous improvements by providing feedback on the identified product defects.

In this code sample, we show how you can build cost-optimal batch solution with Amazon Lookout for Vision which provision your custom model at scheduled times, process all your images, and then deprovision your resources to avoid incurring extra cost.

This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI or using the Cloudformation links below. It includes the following files and folders:

  • functions - Code for the application's Lambda functions to check the presence of messages in a Queue, start or stop a Amazon Lookout for Vision Model, Analyse Images using your Model.
  • template.yaml - An AWS CloudFormation template that defines the application's AWS resources.

This application creates a serverless Amazon Lookout for Vision Detection workflow which runs on a pre-defined schedule (note that the schedule is enabled by default at deployment). It demonstrates the power of Step Functions to orchestrate Lambda functions and other AWS resources to form complex and robust workflows, coupled with event-driven development using Amazon EventBridge.

The following architecture diagram shows how you can design a serverless workflow to process images in batches with Amazon Lookout for Vision Model.

Solution Architecture Diagram: The following architecture diagram shows how you can design a serverless workflow to process images in batches with Amazon Lookout for Vision.

Architecture Diagram

The following diagram shows the Step Functions state machine used to orchestrate the solution.

Architecture Diagram

Usage

Prerequisites

  1. To deploy the sample application, you will require an AWS account. If you don’t already have an AWS account, create one at https://aws.amazon.com by following the on-screen instructions. Your access to the AWS account must have IAM permissions to launch AWS CloudFormation templates that create IAM roles.

  2. Please refer here for instructions on getting started with Amazon Lookout for Vision. When deploying this application you will need to provide the following two parameters for your Lookout for Vision Project.

    • Amazon Lookout for Vision Project Name: The name of the Amazon Lookout for Vision project that contains the models you want to use. This project should be in the same region where you are deploying the solution.
    • Amazon Lookout for Vision Model Version: The number of the model version that you want to use.

Deployment

The demo application is deployed as an AWS CloudFormation template.

Note
You are responsible for the cost of the AWS services used while running this sample deployment. There is no additional cost for using this sample. For full details, see the following pricing pages for each AWS service you will be using in this sample. Prices are subject to change.

  1. Deploy the latest CloudFormation template by following the link below for your preferred AWS region:
Region Launch Template
US East (N. Virginia) (us-east-1) Launch the AnomalyDetection Stack with CloudFormation
US East (Ohio) (us-east-2) Launch the AnomalyDetection Stack with CloudFormation
US West (Oregon) (us-west-2) Launch the AnomalyDetection Stack with CloudFormation
EU (Ireland) (eu-west-1) Launch the AnomalyDetection Stack with CloudFormation
  1. If prompted, login using your AWS account credentials.

  2. You should see a screen titled "Create Stack" at the "Specify template" step. The fields specifying the CloudFormation template are pre-populated. Click the Next button at the bottom of the page.

  3. On the "Specify stack details" screen you may customize the following parameters of the CloudFormation stack:

    • Stack Name: (Default: AnomalyDetection) This is the name that is used to refer to this stack in CloudFormation once deployed.
    • lookoutforvisionProjectName: The Amazon Lookout for Vision Model Project Name
    • lookoutforvisionProjectModelVersion: The Amazon Lookout for Vision Model Project Version
    • minimumInferenceUnitsToUse: Minimum number of inference units to use
    • clientToken: Optional idempotency token string that ensures a call to StartModel completes only once

    When completed, click Next

  4. Configure stack options if desired, then click Next.

  5. On the review you screen, you must check the boxes for:

    • "I acknowledge that AWS CloudFormation might create IAM resources"
    • "I acknowledge that AWS CloudFormation might create IAM resources with custom names"
    • "I acknowledge that AWS CloudFormation might require the following capability: CAPABILITY_AUTO_EXPAND"

    These are required to allow CloudFormation to create a Role to allow access to resources needed by the stack and name the resources in a dynamic way.

  6. Click Create Change Set

  7. On the Change Set screen, click Execute to launch your stack.

    • You may need to wait for the Execution status of the change set to become "AVAILABLE" before the "Execute" button becomes available.
  8. Wait for the CloudFormation stack to launch. Completion is indicated when the "Stack status" is "CREATE_COMPLETE".

    • You can monitor the stack creation progress in the "Events" tab.
  9. Note the url displayed in the Outputs tab for the stack. This is used to access the application.

Testing the workflow

To test your workflow, complete the following steps:

  1. Upload sample images to the input S3 bucket that was created by the solution (Example: xxxx-sources3bucket-xxxx).
  2. Go to AWS Step Function console and select the state machine created by the solution (Example: CustomCVStateMachine-xxxx). You will see an execution triggered by the EventBridge at every hour.
  3. To test the solution, you can manually start the workflow by clicking on the “Start execution” button.
  4. Once images are processed you can go to the output S3 bucket (Example: xxxx-finals3bucket-xxxx) to see the JSON output for each image. The Final S3 bucket holds the images that have been processed along with the inferenced anomaly detection results in JSON format. Once an image is processed, it will be deleted from the source bucket.

Removing the application

To remove the application:

  1. Open the AWS CloudFormation Console.

  2. Select the name of the stack.

  3. Select "Delete Stack". Your stack will take some time to be deleted. You can track its progress in the "Events" tab. When it is done, the status will change from "DELETE_IN_PROGRESS" to "DELETE_COMPLETE". It will then disappear from the list.

Note: Please note that the provided configuration will ensure that the Amazon S3 buckets and their contents are retained when removing the application via the AWS Cloudformation console. This is to ensure that no data is accidently lost while removing the application. The buckets can be deleted using the S3 console.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

About

This project contains source code and supporting files for a serverless application which can be used for Computer Vision inferencing using Amazon Lookout for Vision

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •