State and local governments spend approximately $1.23 billion annually on the operation and maintenance of signalized intersections. Meanwhile, traffic congestion at intersections costs drivers approximately $22 billion annually. Implementing detection-based solutions that leverage artificial intelligence (AI) can significantly reduce intersection congestion and reduce operations and maintenance costs. In this blog post, we discuss how Amazon Rekognition, an AI technology, can help reduce intersection congestion and reduce operations and maintenance costs.
State and local governments rely on traffic signals to facilitate the safe flow of traffic for vehicles, pedestrians, and other users. There are two main types of traffic signals: fixed and dynamic. Fixed signals are timed signals controlled by electromechanical signals that switch and hold their lights based on set times. Dynamic signals are designed to adjust based on traffic conditions, using detectors both under the roadway surface and above the traffic light. However, as the population continues to grow, more cars, bicycles, and pedestrians use the roads. An increase in road users can have a negative impact on the efficiency of either of the two transportation systems.
Solution overview
At a high level, our solution uses Amazon Rekognition to automatically detect objects (cars, bikes, etc.) and scenes at intersections. After detection, Amazon Rekognition creates bounding boxes around each object (e.g. vehicles) and calculates the distance between each object (in this scenario, the distance between vehicles detected at the intersection). The calculated distance results are used programmatically to stop or allow traffic to flow, reducing congestion, all without human intervention.
Prerequisites
You can implement the proposed solution in your own AWS environment using the code we provide. However, there are some prerequisites that you must meet. Before you run the lab in this post, make sure you have the following:
- An AWS account. If necessary, create one.
- Appropriate AWS Identity and Access Management (IAM) permissions to access the services used in the lab. If you are setting up an AWS account for the first time, see the IAM documentation for instructions on setting up IAM.
- A SageMaker Studio Notebook, if necessary, create one.
Solution Architecture
The following diagram shows the lab architecture:
This solution uses the following AI and machine learning (AI/ML), serverless, and managed technologies:
- Amazon SageMaker is a fully managed machine learning service that enables data scientists and developers to build, train, and deploy machine learning applications.
- Amazon Rekognition supports adding image and video analysis to your applications.
- IAM grants the authentication and authorization that allows resources in your solution to communicate with each other.
To summarize how the solution works:
- Video footage of a traffic intersection is uploaded from an external device to the SageMaker environment.
- The Python function uses CV2 to split video footage into image frames.
- This function calls Amazon Rekognition when an image frame is complete.
- Amazon Rekognition analyzes each frame and creates a bounding box around each vehicle that it detects.
- This function counts the bounding boxes and changes the traffic lights based on the number of cars it detects using predefined logic.
Solution Walkthrough
Now let’s go through the steps to implement the solution.
Configure SageMaker.
- choose domain In the navigation pane, select the domain name.
- Find and copy SageMaker Execution Role.
- Go to the IAM console. role In the navigation pane, paste the SageMaker execution role that you copied in the previous step.
Enables SageMaker to interact with Amazon Rekognition.
Next, you use the SageMaker execution role to allow SageMaker to interact with Amazon Rekognition.
- In the SageMaker console, choose SageMaker execution role. Add permissions And choose Attach a policy.
- Type in the search bar and select AmazonRekognitionFullAccess policySee the following figure.
Setting up IAM permissions allows you to run notebooks in SageMaker with access to Amazon Rekognition for video analysis.
Download the Rekognition Notebook and intersection data to your local environment. Upload the downloaded notebook and data to Amazon Sagemaker Studio.
Code walkthrough:
In this lab, you prepare your SageMaker environment with OpenCv and Boto3. OpenCv is an open source library with over 250 algorithms for computer vision analysis. Boto3 is the AWS SDK for Python that helps you integrate AWS services with applications and scripts written in Python.
- First, import the OpenCv and Boto3 packages. In the next code cell, we build a function to analyze a video. Let’s go through the main components of the function. The function starts by creating a frame of the video to analyze.
- The frames are written to a new video writer file with an MP4 extension. The function also loops through the files and converts any frames that are not in the video to JPEG files. The code then uses bounding boxes to define and identify traffic lanes. Amazon Rekognition image operations place bounding boxes around detected images for later analysis.
- The function captures video frames and sends them to Amazon Rekognition to analyze the images in the video using bounding boxes. The model uses the bounding boxes to detect and classify images captured in the video (cars, pedestrians, etc.). The code then detects if there are cars in the video sent to Amazon Rekognition. A bounding box is generated for each car detected in the video.
- To accurately detect the car location, the size and location of the car are calculated. After calculating the size and location of the car, the model checks whether there is a car in the detected lanes. After determining whether there is a car in any of the detected lanes, the model counts the number of detected cars in the lane.
- The remainder of the function detects and calculates the size, position, and number of cars in the lane and writes the results to a new file.
- When we write the output to a new file, some geometry calculations are performed to determine the details of the detected objects, for example using polygons to determine the size of the objects.
- Once the function is fully built, the next step is to run the function using a test video to ensure a minimum of 95% confidence.
- The last line of code allows you to download the video from the SageMaker directory and check the results and confidence of the output.
cost
The cost estimate is based on four cameras capturing one frame per second using one SageMaker notebook per intersection, at $6,000 per intersection. One important note is that not all intersections are four-way intersections. Implementing this solution in high traffic areas will increase overall traffic volume.
Cost breakdown and details
service | explanation | First month fee | First 12 months costs |
Amazon SageMaker Studio Notebooks |
Instance name: ml.t3.medium Number of data scientists: 1 Number of Studio Notebooks instances per data scientist: 1 Studio note hours per day: 24 · Studio Note Days: 30 per month |
$36 | $432 |
Amazon Recognition | Number of images processed with labels per month API calls: 345,600/month | $345.60 | $4,147.20 |
Amazon Simple Storage Service (Amazon S3) (Standard storage class) |
S3 Standard Storage: 4,320 GB per month Monthly PUT, COPY, POST, and LIST requests to S3 Standard: 2,592,000 |
$112.32 | $1,347.84 |
Annual Total Estimate | $5,927.04 |
However, this is an estimate and some customizations may incur additional costs. For cost details, see the AWS pricing pages for the services covered in the solution architecture. If you have any questions, contact the AWS team for a more technical and focused discussion.
cleaning
To avoid future charges, delete all AWS resources created for this solution that you no longer need.
Conclusion
This post presents a solution that uses Amazon Rekognition to make traffic lights more efficient. The solution proposed in this post can reduce costs, support road safety, and reduce congestion at intersections, all of which makes traffic management more efficient. To learn more about how Amazon Rekognition can help you speed up other image recognition and video analysis tasks, see the Amazon Rekognition Developer Guide.
About the Author
Hao Lun Collin Chu is an innovative Solutions Architect at AWS, helping partners and customers solve complex business challenges leveraging cutting edge cloud technologies. With broad expertise in cloud migration, modernization, and AI/ML, Colin advises organizations on how to translate their needs into transformational AWS-based solutions. Driven by his passion for leveraging technology as a force for good, he is committed to delivering solutions that empower organizations and improve people’s lives. Outside of work, Colin enjoys playing drums, volleyball, and board games.
Joe Wilson Joe is a Solutions Architect at Amazon Web Services supporting nonprofits. He provides technical guidance to nonprofits looking to securely build, deploy, and scale applications in the cloud. He is passionate about using data and technology for social impact. Joe’s background is in data science and international development. Outside of work, he loves spending time with family and friends and discussing innovation and entrepreneurship.