Real-Time Traffic Flow Management Using OpenCV
Real-Time Traffic Flow Management Using OpenCV
Abstract—Swift urbanization and industrialization, along traffic gridlock. With this method, every roadway segment is
with India’s large population, have pushed the country’s assigned an equivalent duration for the flow of traffic.
infrastructure to its limits. As a result, effective traffic However, this approach fails to reduce traffic gridlock
regulation has become a critical issue in contemporary cities,
where the vehicle count is growing at an unprecedented rate. effectively, as giving equal time to each part of the road
The timer/countdown system that governs traffic lights in does not consider the varying vehicular density [15] at
India is not only inefficient but also time-consuming in different times of the day.
managing the traffic gridlock, eventually worsening the traffic
issue, and escalating the danger of mishaps. This paper
The proposed approach aims to enhance the efficiency of
proposes a smart traffic light control system to address the regulating vehicular movement by upgrading the existing
problem of traffic gridlock in modern cities. The system traffic light system, which can be achieved by incorporating
employs computer vision and video surveillance for real-time real-time functionality into the system. To accomplish this,
traffic conditions. By analyzing video data from traffic the blob function [7] [11] is implemented to generate large
cameras, the system utilizes various functions of OpenCV, binary objects that can be processed by the neural network.
including background subtraction, blob detection, and object Furthermore, YOLOv5 [14], a state-of-the-art object
tracking, to estimate the vehicular count in each lane and their detection algorithm, is utilized to detect multiple objects in a
proportional density compared to the other lanes. The time frame in real-time [9]. After vehicular detection, the density
allotment module receives input on traffic density from the of vehicles is calculated in each frame, and a threshold value
system and calculates the most efficient time slot to manage is derived, which will determine when the traffic signal
traffic flow. This proposed system has the potential to reduce timings should be adjusted. Finally, traffic synchronization
traffic gridlock, travel times, accidents, and CO2 emissions in logic is employed to dynamically adjust traffic signal timings
modern cities by taking into account real-time traffic to manage the flow of traffic more effectively (fig. 1.).
conditions.
I. INTRODUCTION
The increase in motorization has significantly contributed
to the aggravation of traffic gridlock. With a rising number
of individuals owning cars and other modes of transportation,
there has been a surge in automobile quantity on the roads.
Moreover, insufficient road infrastructure and public
transportation systems have substantially boosted traffic
volumes, culminating in heightened levels of road
congestion. Since roads have a finite physical capacity, they
have limited potential to adapt to changing traffic demands,
thus making it more challenging to manage traffic Fig. 1. Sequential Steps in Implementing Proposed System
effectively. Instead of constructing additional bridges,
highways, and flyovers, upgrading the current system to II. LITERATURE REVIEW
operate more efficiently can address the problem of road The problem of traffic flow prediction and control has
congestion and ensure the movement of vehicles seamlessly. been addressed in several previous studies. Some of these
There are alternative methods for detecting traffic, such as studies utilized deep learning algorithms, such as neural
motion detection [11] or laser technology implementation on networks, to forecast traffic flow by analysing historical
either side of the road. However, these options can be labour- traffic data. In contrast, other studies concentrated on
intensive and require a significant amount of hardware. controlling the traffic flow through the application of control
The primary objective of developing a traffic light theory and optimization techniques. This involved adjusting
control system is to enhance traffic flow, boost intersection traffic signals and other relevant parameters to regulate
capacity, and minimize delays cost-effectively. In India, traffic flow.
fixed-time traffic light control is a significant contributor to
2
Authorized licensed use limited to: G Narayanamma Institute of Technology & Science. Downloaded on October 30,2024 at 06:13:12 UTC from IEEE Xplore. Restrictions apply.
A. Image Enhancement To achieve real-time vehicle detection, the YOLOv5m
model [9] was chosen to be used. This model strikes a
Image enhancement techniques aid us in comprehending
balance between speed and accuracy compared to larger
and scrutinizing the characteristics of an image, including the
models like YOLOv5l or YOLOv5x. The model can
objects present within it. Enhancing sharpness, blurring,
efficiently process image or video streams to detect vehicles
increasing brightness, and edge detection are some of the
accurately and quickly, making it ideal for time-sensitive
fundamental operations used in image processing. These
applications.
operations facilitate the deep learning model to comprehend
and process images more effectively.
To improve the accuracy of the vehicle detection model,
blob detection is applied to identify regions of interest that
meet certain size and shape criteria. This technique helps
reduce noise and identify regions likely to contain vehicles.
To implement this, first convert the input image to grayscale
and apply Gaussian blur to reduce noise. The image is then
segmented into distinct regions using adaptive threshold, and
morphological operations are applied to remove noise and
fill gaps. The blob detection algorithm is then applied to
identify regions of interest [7] [11], which are extracted from
the original image and returned as a list of vehicle-containing
regions.
3
Authorized licensed use limited to: G Narayanamma Institute of Technology & Science. Downloaded on October 30,2024 at 06:13:12 UTC from IEEE Xplore. Restrictions apply.
Fig. 7. Vehicle Density Detection for Traffic Analysis
D. Signal Synchronization
The process for determining appropriate waiting times at
traffic signals involved using a signal synchronization logic
capable of evaluating traffic volume in different lanes. For
evaluating the proportion of traffic in each lane divide the
vehicle count of a particular lane by the total vehicle count in
each of the lanes. Using this data, allowed us to multiply this
proportion with a base timer and establish an initial waiting
duration specific per lane. Then check whether these
Fig. 6. Non-Max Suppression [26] durations fell between predetermined upper and lower
bounds known as timeLimits. If they did fall into these
boundaries precisely or quite closely enough based on what
C. Vehicle Density Calculation was set under those constraints in place under timelimits then
To determine the vehicular density [18] in a given video it would serve as the final waiting duration for that lane.
stream, vehicle detection technique is employed along with However, if those initial durations exceeded those limits then
the CentroidTracker class to track the vehicles and create a adjusted them to the nearest limit given by timeLimits. After
dictionary of trackable objects. The countVehicles() function the completion of the process, the time durations obtained for
takes the input video path as a parameter and returns a list of each lane are saved in a list known as "t."
the count of vehicles detected in the video. The output of the traffic signal synchronization logic is a
The implementation of the CentroidTracker class is list of time durations for each lane and the sum of all time
crucial in this process, with optional arguments for durations. This list of time durations can be used to control
maxDisappeared and maxDistance determining the the traffic signals for more efficient traffic flow and reduced
maximum consecutive frames an object can be "disappeared" congestion. The input data for the logic is obtained from a
for and the maximum distance between centroids, file, say "traffic_data.txt", which contains the traffic counts
respectively. This ensures that only valid objects are tracked for different types of vehicles. The traffic counts are stored in
and that the tracking is accurate. a list called "traffic_count". The logic also uses two
variables: "baseTimer" and "timeLimits". After the logic is
The YOLOv5 model is utilised and loaded using the executed, the resulting time allocations for each type of
TensorNets library, and a dictionary of classes is defined to vehicle are stored in "t", and the sum of all time allocations is
simplify looping. This approach makes it easier to process printed.
and analyse the results of object detection.
4
Authorized licensed use limited to: G Narayanamma Institute of Technology & Science. Downloaded on October 30,2024 at 06:13:12 UTC from IEEE Xplore. Restrictions apply.
This approach has shown promise, especially in areas with
data scarcity. Furthermore, SUMO is capable of simulating
various traffic scenarios, including normal traffic flow,
accidents, and road closures, enabling us to evaluate the
impact of these events on traffic gridlock and mobility.
5
Authorized licensed use limited to: G Narayanamma Institute of Technology & Science. Downloaded on October 30,2024 at 06:13:12 UTC from IEEE Xplore. Restrictions apply.
variations in capture equipment were observed as a potential The results further demonstrated that the adaptive
cause for discrepancies within the model's predictions. approach was more effective in clearing the intersection than
Notwithstanding these challenges, results indicated that the conventional approach. The clear/wait time was reduced
YOLOv5m remained an adequate tool for providing credible to 30/110 seconds for Lane 1, 50/90 seconds for Lane 2,
vehicular density information in each lane. 25/125 seconds for Lane 3, and 20/125 seconds for Lane 4.
Additionally, the number of vehicles that cleared/left during
Furthermore, an assessment was conducted at an this time was higher for all lanes in the adaptive approach
intersection with moderate traffic at 8 am, and observations compared to the conventional approach. This led to an
were recorded for each of the four lanes. Vehicle density was average reduction in clear time of 45.2% and a reduction in
measured for each lane, and it was found that Lane 3 had the wait time of 6.75%.
highest vehicle density with 53 vehicles, while Lane 4 had
the lowest vehicle density with 19 vehicles. ANALYSIS OF REMAINING VEHICLES
25
TABLE II. COMPARISON OF CONVENTIONAL AND ADAPTIVE
APPROACHES FOR TRAFFIC MANAGEMENT: RESULTS USING 20
SYNCHRONIZATION LOGIC
15
LANE 1 LANE 2 LANE 3 LANE 4
10
VEHICLE DENSITY 35 23 53 19
CONVENTIONAL 5
CLEAR/ WAIT TIME 45/120 45/120 45/120 45/120
(sec) 0
CONVENTIONAL
CLEAR TIME (sec)/ 20/15 15/8 31/22 13/6 LANE 1 LANE 2 LANE 3 LANE 4
VEHICLES LEFT
ADAPTIVE
CLEAR/ WAIT TIME 30/110 50/90 25/125 20/125 CONVENTIONAL ADAPTIVE
(sec)
ADAPTIVE
CLEAR TIME (sec)/ 29/4 20/3 51/2 18/1 Fig. 13. Comparison of Conventional and Adaptive approaches based on
VEHICLES LEFT remaining vehicles
ADAPTIVE LANE
PRIORITY
II III I IV
Furthermore, the number of vehicles left were
In the conventional approach, a static clear/wait time of significantly reduced in the adaptive approach. In the
45/120 seconds was set for all lanes, regardless of their conventional approach, twenty vehicles were left during the
individual vehicle density. Outside of peak hours, a waiting clear/wait time, while in the adaptive approach, only four
time of 100 seconds was set for all lanes. However, during vehicles were left in Lane 1, indicating a reduction of 77.5%
peak hours, the waiting time increased to 120 seconds. This in the number of vehicles left on average.
approach lacked adaptability to varying traffic conditions and To sum up, the adaptive approach using synchronization
lead to congestion and inefficient traffic flow management. logic proved to be more effective in improving traffic flow at
To improve traffic flow, signal synchronization logic was the intersection. The clear/wait time was reduced, and the
implemented that gave lane priority based on the division of number of vehicles left was significantly decreased.
traffic count in each lane by the total traffic count. Different Moreover, the lane priority strategy succeeded in mitigating
priorities were assigned to each lane, with Lane 3 being the congestion in the most heavily trafficked lanes. The
given the highest priority due to its high vehicle density, outcomes of the experiment are encouraging regarding the
while lane 4 was given the lowest priority. The results integration of adaptive approach as a means of minimizing
showed that lane priority helped in reducing the number of traffic gridlock and promoting road safety.
vehicles left during the clear/wait time, with Lane 3 having
V. CONCLUSION AND FUTURE SCOPE
the least number of vehicles left (2 vehicles) and Lane 4
having only 1 vehicle left. This indicates that the adaptive In conclusion, real-time traffic flow management using
approach with lane priority was more efficient in clearing the OpenCV has shown great potential in tackling the challenges
intersection compared to the conventional approach. of congestion and safety on roads. Traffic congestion is a
significant issue in modern cities due to rapid urbanization
and industrialization. The proposed approach leverages
computer vision, YOLOv5m, blob detection, dark channel
technique, and object tracking to develop a smart traffic light
control system that considers real-time traffic conditions. By
analysing video data from traffic cameras, the system
estimates vehicular density and calculates optimal time slots
to manage traffic flow efficiently. Additionally, the system is
a cost-effective and scalable solution that has the potential to
revolutionize the way traffic is managed in cities enabling
traffic authorities to reduce gridlock and improve traffic flow
without incurring significant additional expenses. By
implementing this system, road safety can be improved,
travel times can be reduced, and carbon emissions caused by
idling vehicles can be minimized.
The findings of this research suggest that there is a need
Fig. 12. Comparison of Conventional and Adaptive approaches based on for further research to improve the management of real-time
Waiting Time
traffic flow by creating a more advanced system that can
6
Authorized licensed use limited to: G Narayanamma Institute of Technology & Science. Downloaded on October 30,2024 at 06:13:12 UTC from IEEE Xplore. Restrictions apply.
synchronize traffic across consecutive intersections and track [12] Schubert, R., Richter, E., & Wanielik, G. (2008, June). Comparison
vehicles as they traverse between intersections, allowing for and evaluation of advanced motion models for vehicle tracking. In
2008 11th international conference on information fusion (pp. 1-6).
adjustments to be made to traffic signals and flow patterns as IEEE.
necessary. Additionally, the system could prioritize [13] Prakash, M., Saravanakumar, C., Lakshmi, S. K., Rose, J. D., &
emergency vehicles by recognizing their sirens and lights in Praba, B. (2021, September). Automatic feature extraction and traffic
real-time, resulting in faster and more efficient navigation management using machine learning and open CV model. In 2021
through traffic. Pedestrian safety could also be enhanced by International Conference on Innovative Computing, Intelligent
implementing OpenCV to analyse behaviour at intersections, Communication and Smart Electrical Systems (ICSES) (pp. 1-5).
IEEE.
identifying potential hazards such as jaywalking and alerting
[14] Dong, X., Yan, S., & Duan, C. (2022). A lightweight vehicles
traffic managers to take corrective action. By incorporating detection network model based on YOLOv5. Engineering
real-time public transportation data and weather information, Applications of Artificial Intelligence, 113, 104914.
the system could forecast traffic demand and adapt traffic [15] Kuhlmorgen, S., Llatser, I., Festag, A., & Fettweis, G. (2015, May).
flow patterns accordingly. Finally, deep learning algorithms Performance evaluation of etsi geonetworking for vehicular ad hoc
could be employed to continuously analyse traffic patterns networks. In 2015 IEEE 81st Vehicular Technology Conference
and improve predictive accuracy, leading to optimal traffic (VTC Spring) (pp. 1-6). IEEE.
flow management. [16] Chen, Z., Cao, L., & Wang, Q. (2022). Yolov5-based vehicle
detection method for high-resolution UAV images. Mobile
REFERENCES Information Systems, 2022.
[1] Serrano, Á., Conde, C., Rodríguez-Aragón, L. J., Montes, R., & [17] Shinde, P. A., & Mane, Y. B. (2015, January). Advanced vehicle
Cabello, E. (2005). Computer vision application: real time smart monitoring and tracking system based on Raspberry Pi. In 2015 IEEE
traffic light. In Computer Aided Systems Theory–EUROCAST 2005: 9th International Conference on Intelligent Systems and Control
10th International Conference on Computer Aided Systems Theory, (ISCO) (pp. 1-6). IEEE.
Las Palmas de Gran Canaria, Spain, , 7–11, 2005, Revised Selected [18] Gong, S., Kumar, R., & Kumutha, D. (2021). Design of lighting
Papers 10 (pp. 525-530). Springer Berlin Heidelberg. intelligent control system based on OpenCV image processing
[2] Behrad, A., Shahrokni, A., Motamedi, S. A., & Madani, K. (2001, technology. International Journal of Uncertainty, Fuzziness and
November). A robust vision-based moving target detection and Knowledge-Based Systems, 29(Supp01), 119-139.
tracking system. In (Proceeding of Image and Vision Computing [19] Lopez, P. A., Behrisch, M., Bieker-Walz, L., Erdmann, J., Flötteröd,
conference (IVCNZ2001. (Proceeding of Image and Vision Y. P., Hilbrich, R., ... & Wießner, E. (2018, November). Microscopic
Computing conference (IVCNZ2001. traffic simulation using sumo. In 2018 21st international conference
[3] Vidhya, K., & Banu, A. B. (2014). Density based traffic signal on intelligent transportation systems (ITSC) (pp. 2575-2582). IEEE.
system. International Journal of Innovative Research in Science, [20] Rahman, R., Bin Azad, Z., & Bakhtiar Hasan, M. (2022). Densely-
Engineering and Technology, 3(3), 2218-2222. Populated Traffic Detection Using YOLOv5 and Non-maximum
[4] Ke, R., Li, Z., Tang, J., Pan, Z., & Wang, Y. (2018). Real-time traffic Suppression Ensembling. In Proceedings of the International
flow parameter estimation from UAV video based on ensemble Conference on Big Data, IoT, and Machine Learning: BIM 2021 (pp.
classifier and optical flow. IEEE Transactions on Intelligent 567-578). Springer Singapore.
Transportation Systems, 20(1), 54-64. [21] Sharma, S., & Guleria, K. (2022, April). Deep learning models for
[5] Das, D., Pargal, S., Chakraborty, S., & Mitra, B. (2022, June). DriBe: image classification: comparison and applications. In 2022 2nd
on-Road Mobile Telemetry for Locality-Neutral Driving Behavior International Conference on Advance Computing and Innovative
Annotation. In 2022 23rd IEEE International Conference on Mobile Technologies in Engineering (ICACITE) (pp. 1733-1738). IEEE.
Data Management (MDM) (pp. 159-168). IEEE. [22] Singh, A., Kukreja, V., & Gandomani, T. J. (Eds.). (2022). Machine
[6] Orozco, M. C. E., & Rebong, C. B. (2019). Vehicular detection and Learning for Edge Computing: Frameworks, Patterns and Best
classification for intelligent transportation system: A deep learning Practices. CRC Press.
approach using faster R-CNN model. Platero, 180(1), 36551. [23] Yeom, S., & Cho, I. J. (2019). Detection and tracking of moving
[7] Kini, S. (2021). Real time moving vehicle congestion detection and pedestrians with a small unmanned aerial vehicle. Applied Sciences,
tracking using OpenCV. Turkish Journal of Computer and 9(16),3359.
Mathematics Education (TURCOMAT), 12(10), 273-279. [24] https://towardsdatascience.com/yolov5-compared-to-faster-rcnn-who-
[8] He, K., Sun, J., & Tang, X. (2010). Single image haze removal using wins-a771cd6c9fb4
dark channel prior. IEEE transactions on pattern analysis and machine [25] Wang, K., & Liu, M. (2022). YOLOv3-MT: A YOLOv3 using multi-
intelligence, 33(12), 2341-2353.dd target tracking for vehicle visual detection. Applied Intelligence,
[9] Uzar, M., Öztürk, Ş., Bayrak, O. C., Arda, T., & Öcalan, N. T. (2021). 52(2), 2070-2091.
Performance analysis of YOLO versions for automatic vehicle [26] https://towardsdatascience.com/guide-to-car-detection-using-yolo-
detection from UAV images. Advanced Remote Sensing, 1(1), 16-30. 48caac8e4ded
[10] Schamm, T., von Carlowitz, C., & Zöllner, J. M. (2010, June). On- [27] Siddique, U., Weng, P., & Zimmer, M. (2020, November). Learning
road vehicle detection during dusk and at night. In 2010 IEEE fair policies in multi-objective (deep) reinforcement learning with
Intelligent Vehicles Symposium (pp. 418-423). IEEE. average and discounted rewards. In International Conference on
[11] Nagaraj, U., Rathod, J., Patil, P., Thakur, S., & Sharma, U. (2013). Machine Learning (pp. 8905-8915). PMLR.
Traffic jam detection using image processing. International Journal of
Engineering Research and Applications, 3(2), 1087-1091.
7
Authorized licensed use limited to: G Narayanamma Institute of Technology & Science. Downloaded on October 30,2024 at 06:13:12 UTC from IEEE Xplore. Restrictions apply.