The Smart Traffic Management System involves passing real-time traffic camera data through a
machine learning model that determines the most efficient way to choose the signals at all lanes such that traffic is cleared in the most efficient way possible. Using dynamic traffic signal control, on an average would make the time taken to clear the traffic up to 56.25% faster for a 4-lane road in some test parameters. As it takes into consideration the real-time density of vehicles in each individual lane and also removes the outdated time constraint for lane switching, it is a significant improvement over old-school traffic management based on fixed timers.
We start by using real-time traffic camera data and using OpenCV, the computer vision library to detect cars and analyze the vehicle density by defining a 'Region of Interest' to look for vehicles in.
Here's an example snippet for the same
Here, the blue rectangle is the region where I want to determine the vehicle density by counting the number of cars currently in the space.
This is calculated using the vehicle centroid, for each vehicle detected, if the centroid is in the Range of Interest, it counts as a vehicle. So when a vehicle passes from the range or isn't in it, it isn't considered.
Assuming the left and right are 2 different lanes, the detection works perfectly.
This is done so that the program can be adjusted for different camera resolutions, angles, ranges and other parameters due to which the input feed may differ and also to customize the definition of "lanes" if the feed contains multiple.
This data is then sent to a trained Machine Learning model (A DQN) made with TensorFlow's API, stable baselines 3. We can customize the training based on policy and timesteps. I've implemented the MLP (Multi layered perception) Policy.
def step(self, action):
reward = 0.0
# Penalize for choosing a lane with 0 cars
if self.state[action] == 0:
reward -= 15.0 # Penalty for choosing an empty lane
if cars_before_action > 0 and self.state[action] == 0:
reward += 10.0
reward -= 1.0 # Step penalty
if np.all(self.state == 0):
reward += 25 # Reward for clearing all lanes
Initially it needs to learn not to choose lanes that are empty and to clear lanes as soon as possible, so for every action it takes, there's a "step" penalty. As lanes are cleared, it learns to choose lanes according to its benefit, lanes with lower vehicles as it means an easier reward which holds true for clearing traffic in lanes where the discrepancy is more uneven.
Threads were then utilized to make sure the model isn't overloaded; as for a real-time application, if the model receives values each frame of a video, it cannot understand the changes in the environment and hence gives recurring inexplicable values.
More info can be found in the code at
https://github.com/Sidsmartz/SIH---Traffic-Management-System
The deduction for the same input given to a (simulated) standard traffic signal vs the Model is shown below
(Consider the input as no. of vehicles in a 4-lane junction and assume 5 cars pass per "iteration" in both the model and the signal, with 20 being a soft-limit for the no. of vehicles detected in a lane by a camera.)
For the inputs :
The current Traffic System (simulated by a simple for loop as a cycle between signals):
Takes 16 steps (4 fixed-time cycles)
When the same was passed to our model:
It cleared the lanes in 7 steps.
A more comprehensive analysis for the model will be posted soon,
https://github.com/Sidsmartz/SIH---Traffic-Management-System
will be updated whenever there is a change.