Advanced Video Analysis with AWS DeepLens and Amazon Kinesis Video Streams

Sidra Saleem - Aug 25 - - Dev Community

Introduction

Overview of Advanced Video Analysis

In an age where real-time video data is abundant, organizations in industries such as retail, security, healthcare, and media need to extract valuable insights from video streams. Advanced video analysis provides a pathway for extracting actionable data, enhancing automation, and improving operational efficiency.

AWS offers a rich ecosystem for real-time video analysis through services like AWS DeepLens and Amazon Kinesis Video Streams. AWS DeepLens is a machine-learning-powered video camera capable of running pre-trained deep learning models locally on the device. When combined with Amazon Kinesis Video Streams for streaming video and AWS Lambda for real-time analysis, it becomes a robust platform for performing advanced video analysis.

Key AWS Services:

  • AWS DeepLens: Edge device for video capture and machine learning inference.
  • Amazon Kinesis Video Streams (KVS): Service for video ingestion, storage, and processing.
  • AWS Lambda: Serverless compute service for real-time analysis.
  • Amazon Rekognition: For image and video analysis tasks.

High-level Architecture and Workflow:

The workflow involves capturing video via DeepLens, streaming it to Kinesis Video Streams, and using Lambda functions to process the data in real-time, possibly integrating Rekognition for advanced analysis like object and facial recognition.

Prerequisites

  • AWS Account Setup: Ensure your AWS account is active.
  • IAM Roles and Permissions: Create roles with permissions for Kinesis Video Streams, Lambda, Rekognition, and S3.
  • Basic Knowledge: Familiarity with AWS DeepLens, Kinesis Video Streams, and Lambda will help you understand the setup better.

Setting Up AWS DeepLens for Video Analysis

Unboxing and Initial Setup

Physical Setup of AWS DeepLens:

  1. Unbox the AWS DeepLens device.
  2. Power it up using the provided adapter and connect an HDMI cable to a display to view the initial configuration.

Connecting AWS DeepLens to the AWS Management Console:

  1. Navigate to the AWS DeepLens dashboard on your AWS Console.
  2. Power on the device and connect it to the internet via Wi-Fi.

Configuring AWS DeepLens Device

Registering the Device with AWS Console:

  1. On the AWS Console, go to the DeepLens dashboard and click Register Device.
  2. Follow the instructions to download the device certificates and register the device.

Device Settings Configuration:

  • Set up Wi-Fi, IoT Core integration, and configure device settings.

Console-Based Steps for Greengrass Group Association:

  1. In the AWS Console, navigate to AWS IoT Greengrass.
  2. Create a new Greengrass group, attach your DeepLens device, and create a deployment.

CLI-Based Steps for Greengrass Group Association:

aws greengrass create-group --name MyGreengrassGroup

aws greengrass create-core-definition --name MyCore --initial-version '{"Cores": [{"Id": "MyCoreId", "ThingArn": "arn:aws:iot:region:account-id:thing/MyGreengrassCore"}]}'

Deploying a Pre-built Sample Project

Choosing a Sample Project (e.g., Object Detection):

  • In the AWS DeepLens dashboard, browse available sample projects and select Object Detection.

Console-Based Deployment:

  • Click Deploy to Device to deploy the pre-built model to your DeepLens device.

CLI-Based Deployment:

aws greengrass create-deployment --group-id MyGreengrassGroupId --deployment-id DeploymentId

Stream Video Data to Amazon Kinesis Video Streams

Creating a Kinesis Video Stream

Console-Based Steps:

  1. In the AWS Console, navigate to Kinesis Video Streams.
  2. Click Create Stream, provide a stream name, and set data retention settings.

CLI-Based Steps:

aws kinesisvideo create-stream --stream-name MyVideoStream --data-retention-in-hours 24

Integrating DeepLens with Kinesis Video Streams

Modifying DeepLens Project:

  • Edit the Lambda function running on AWS DeepLens to stream video data to Kinesis.

Console-Based Configuration of Lambda Function:

  • In the AWS DeepLens project settings, modify the Lambda function to include code that pushes video frames to Kinesis Video Streams.

CLI-Based Update of Lambda Function:

aws lambda update-function-code --function-name MyDeepLensFunction --zip-file fileb://my-deployment-package.zip

Testing Video Streaming

Validating the Video Stream:

  • Navigate to the Kinesis Video Streams dashboard and view your stream.

Monitoring Stream Health:

  • Check metrics such as data flow and stream status in Amazon CloudWatch.

Real-Time Video Processing with AWS Lambda and Rekognition

Setting Up AWS Lambda for Video Analysis

Console-Based Steps:

  1. In the AWS Lambda dashboard, click Create Function.
  2. Choose a runtime (e.g., Python 3.8), set up an execution role, and deploy the function.

CLI-Based Steps:

aws lambda create-function --function-name VideoAnalysisFunction --runtime python3.8 --role arn:aws:iam::account-id:role/execution_role --handler lambda_function.lambda_handler --zip-file fileb://my-deployment-package.zip

Integrating AWS Rekognition with Lambda

Configuring Rekognition:

  • Set up Rekognition to analyze frames from your Kinesis Video Streams.

Console-Based Rekognition Setup:

  • In the AWS Rekognition console, set up a stream processor to analyze the Kinesis Video Stream.

CLI-Based Rekognition Configuration:

aws rekognition start-stream-processor --name MyStreamProcessor

Deploying and Testing the Solution

Deploy the Lambda Function:

  • Ensure that your Lambda function is processing video streams.

Real-time Testing:

  • Analyze the live video for objects, faces, or other features using Rekognition.

Storing and Managing Analysis Results

Persisting Data to Amazon S3

Console-Based Steps:

  1. In the AWS Console, navigate to Amazon S3 and create a new bucket.
  2. Configure bucket policies and lifecycle settings for cost management.

CLI-Based Steps:

aws s3api create-bucket --bucket my-video-analysis-results --region us-west-2

Storing Analysis Results in DynamoDB

Console-Based Steps:

  1. In the AWS Console, navigate to DynamoDB and create a new table.
  2. Define key schema and provision throughput settings.

CLI-Based Steps:

aws dynamodb create-table --table-name VideoAnalysisResults --attribute-definitions AttributeName=Id,AttributeType=S --key-schema AttributeName=Id,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

Archiving and Managing Data

Using AWS Glue and Amazon Athena:

  • AWS Glue can be used for ETL tasks, and Athena for querying video analysis results stored in S3.

Monitoring and Scaling the Video Analysis Pipeline

Setting Up CloudWatch for Monitoring

Console-Based Steps:

  • In Amazon CloudWatch, create dashboards to monitor metrics like video stream throughput and Lambda invocation count.

CLI-Based Steps:

aws cloudwatch put-metric-alarm --alarm-name MyAlarm --metric-name IncomingBytes --namespace AWS/Kinesis --statistic Sum --period 60 --threshold 1000000 --comparison-operator GreaterThanOrEqualToThreshold --dimensions Name=StreamName,Value=MyVideoStream --evaluation-periods 1

Scaling the Solution with Auto Scaling

Scaling Kinesis Video Streams and Lambda:

  • Configure auto-scaling for Kinesis Video Streams and Lambda to handle fluctuating workloads.

Console-Based Steps:

  • In the AWS Console, configure Auto Scaling for both Kinesis Video Streams and Lambda.

CLI-Based Steps:

aws application-autoscaling register-scalable-target --service-namespace kinesis --resource-id stream/MyVideoStream --scalable-dimension kinesis:stream:WriteCapacityUnits --min-capacity 1 --max-capacity 5

Optimizing Cost and Performance

Cost Management Best Practices:

  • Implement S3 lifecycle policies, optimize data retention in Kinesis, and review usage of compute resources.

Conclusion

This article walked through setting up AWS DeepLens and Kinesis Video Streams, integrating video streams with AWS Lambda and Rekognition, and handling storage in S3 and DynamoDB.

Following are the key takeaways :

  • AWS offers a powerful ecosystem for advanced video analysis.
  • DeepLens, Kinesis Video Streams, Lambda, and Rekognition work seamlessly for real-time video processing.

Further Reading and Resources

Deploy the solution, explore additional services, and refine your video analysis pipeline to suit your needs.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .