0% found this document useful (0 votes)
14 views3 pages

Vehicle Detection and Signal Timing System

Uploaded by

Shweta Bagade
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Vehicle Detection and Signal Timing System

Uploaded by

Shweta Bagade
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

#CU_CP_Team_2546

import cv2
import time
import numpy as np

# Load pre-trained vehicle detection model


vehicle_cascade = [Link]('[Link]')

# Video capture
cap1 = [Link]('traffic_video.mp4')
cap2 = [Link]('traffic_video2.mp4')
cap3 = [Link]('traffic_video3.mp4')
cap4 = [Link]('traffic_video1.mp4')

# Video dimensions
frame_width = int([Link](cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int([Link](cv2.CAP_PROP_FRAME_HEIGHT))

# Initialize signal timing parameters (in seconds)


total_time = 60 # Total time for one cycle of signals
green_time = 20 # Default green time for each signal
yellow_time = 5 # Default yellow time for each signal

# Function to calculate signal times based on vehicle density


def calculate_signal_times(vehicle_density):
# Adjust signal times based on vehicle density
# Example: Reduce green time and increase yellow time for higher vehicle
density
adjusted_green_time = max(5, green_time - vehicle_density)
adjusted_yellow_time = min(10, yellow_time + vehicle_density)
return adjusted_green_time, adjusted_yellow_time

# Initialize signal times for each quadrant


signal_times = [total_time] * 4

# Start time for signal timing


start_time = [Link]()

# Create window to display videos


[Link]("Multi-Window Display", cv2.WINDOW_NORMAL)
[Link]("Multi-Window Display", frame_width * 2, frame_height * 2)

# Middle line position for vehicle counting


middle_line_y = frame_height // 2

while True:
# Read frames from all four videos
ret1, frame1 = [Link]()
ret2, frame2 = [Link]()
ret3, frame3 = [Link]()
ret4, frame4 = [Link]()

if not (ret1 and ret2 and ret3 and ret4):


break

# Resize frames
frame1 = [Link](frame1, (frame_width, frame_height))
frame2 = [Link](frame2, (frame_width, frame_height))
frame3 = [Link](frame3, (frame_width, frame_height))
frame4 = [Link](frame4, (frame_width, frame_height))

# Combine frames into a single window


combined_frame = [Link]((frame_height * 2, frame_width * 2, 3),
dtype=np.uint8)

# Assign each frame to its corresponding quadrant in the combined frame


combined_frame[0:frame_height, 0:frame_width] = frame1
combined_frame[0:frame_height, frame_width:frame_width * 2] = frame2
combined_frame[frame_height:frame_height * 2, 0:frame_width] = frame3
combined_frame[frame_height:frame_height * 2, frame_width:frame_width * 2] =
frame4

# Detect vehicles and count them in each quadrant


total_vehicles = 0
for idx, quadrant in enumerate([frame1, frame2, frame3, frame4], start=1):
gray = [Link](quadrant, cv2.COLOR_BGR2GRAY)
vehicles = vehicle_cascade.detectMultiScale(gray, 1.1, 3)

# Count vehicles based on middle line position


for (x, y, w, h) in vehicles:
if y <= middle_line_y <= y + h:
total_vehicles += 1

# Display vehicle count and signal information


if signal_times[idx - 1] > 0:
signal_color = "Green"#(0, 255, 0) # Green
time_allotted = signal_times[idx - 1]
else:
signal_color = "Red" #(0, 0, 255) # Red
time_allotted = 0

# Draw black background rectangle for text


text = f'Quadrant {idx} - Vehicles: {total_vehicles} | Signal:
{signal_color} | Time Allotted: {time_allotted} sec'
text_size = [Link](text, cv2.FONT_HERSHEY_SIMPLEX, 0.6, 2)[0]
[Link](combined_frame, (10 + (idx % 2) * frame_width, frame_height *
(idx // 3 + 1) - 30),
(10 + (idx % 2) * frame_width + text_size[0], frame_height *
(idx // 3 + 1)), (0, 0, 0), -1)

# Display text in white color


[Link](combined_frame, text,
(10 + (idx % 2) * frame_width, frame_height * (idx // 3 + 1) -
10),
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 255, 255), 2)

# Display the combined frame


[Link]('Multi-Window Display', combined_frame)

# Calculate average vehicle density across all quadrants


avg_vehicle_density = total_vehicles / 4

# Calculate signal times based on vehicle density


green_time, yellow_time = calculate_signal_times(avg_vehicle_density)

# Update signal times for each quadrant


signal_times = [green_time + yellow_time] * 4
# Check if the allotted time for the current signal has elapsed
elapsed_time = [Link]() - start_time
if elapsed_time >= total_time:
start_time = [Link]() # Reset start time
# Log signal times for the next cycle
print("Signal Times for Next Cycle:")
for idx, time_allotted in enumerate(signal_times, start=1):
print(f"Quadrant {idx}: Green Time: {green_time} sec, Yellow Time:
{yellow_time} sec")
print("")

# Check for user input to exit


if [Link](1) & 0xFF == ord('q'):
break

# Release video capture objects and close windows


[Link]()
[Link]()
[Link]()
[Link]()
[Link]()

Common questions

Powered by AI

The decision to display signal information in text might be driven by the need for clarity and precision. Text provides unambiguous details on the signal status and remaining time, which can be more effective in conveying exact information compared to visual color cues alone, especially in a busy multi-video context .

The pre-trained vehicle detection model is crucial for identifying and counting vehicles in each quadrant of the displayed video frames. Using this model enables the system to determine vehicle density, which is a key parameter for dynamically adjusting traffic signal times and managing traffic flow efficiently .

The multi-window display operates by reading and combining video frames from four different traffic cameras. Each frame is assigned to a specific quadrant in a larger combined window, facilitating simultaneous monitoring of multiple traffic scenarios within a single display. The system uses cv2 functions to create and manage this visual output .

The system enhances readability by drawing a black rectangle as a background for the text displaying signal color and vehicle count. The text is overlaid in white color with calculated sizing to ensure clear readability over the dynamic video content on the combined display .

The system's modularity, with separate video captures and adaptable signal timing calculation based on detected vehicle density, suggests scalability. Additional cameras or quadrants can be integrated by extending the current multi-video setup and cascade model application, allowing adaptation to larger traffic networks with enhanced computation resources .

Combining frames into a single window facilitates comprehensive, real-time situational awareness for operators. The method reduces cognitive load by centralizing video feeds, enabling quicker decision-making and response to traffic anomalies, and optimizing space and resources compared to managing multiple separate video feeds .

The function 'calculate_signal_times' adjusts signal timings based on vehicle density by reducing green time and increasing yellow time in response to higher vehicle density. This adaptive approach ensures traffic signals respond dynamically to fluctuations in traffic flow by optimizing green and yellow times, aiming to reduce congestion .

Vehicle density is calculated by detecting and counting vehicles that cross the middle line in each video quadrant using a pre-trained CascadeClassifier model. The total number of detected vehicles is divided by the number of quadrants (4) to find the average vehicle density .

Using average vehicle density allows for a balanced approach to signal time allocation, potentially reducing congestion at heavily loaded intersections by dynamically adjusting signal times. However, this strategy assumes equal weight across all quadrants, which might not capture local variances in traffic effectively, challenging its effectiveness under highly uneven traffic distributions .

If start times are not reset in the traffic monitoring system, the signal timing cycle would not correctly initiate new cycles based on the current traffic densities. This could result in inconsistent traffic flow control, leading to inefficiencies such as prolonged red lights or excessive green duration during low vehicle density, thereby disrupting optimal traffic signal management .

You might also like