-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVer1_fluxion.py
More file actions
126 lines (103 loc) · 4.05 KB
/
Copy pathVer1_fluxion.py
File metadata and controls
126 lines (103 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import time
import threading as t
import numpy as np
from ultralytics import YOLO
import torch
print("Is CUDA GPU available: ",torch.cuda.is_available())
# Load YOLO models
models = {
"north": YOLO("yolov10s.pt"),
"south": YOLO("yolov10s.pt"),
"east": YOLO("yolov10s.pt"),
"west": YOLO("yolov10s.pt"),
}
# Traffic light class
class TrafficLight:
def __init__(self, direction):
self.direction = direction
self.state = "Red" # Initial state
self.duration = 0 # Initial duration
def set_state(self, state, duration=None):
self.state = state
self.duration = duration
print(
f">> {self.direction} >>> {self.state} >>> {self.duration} seconds" if self.state in ["Green", "Yellow"] else f" {self.direction} >> {self.state} >> {self.duration} seconds")
# Initialize traffic lights
lights = {dir: TrafficLight(dir.capitalize()) for dir in models.keys()}
# Traffic data for each direction
traffic_data = {dir: 5 for dir in models.keys()}
def capture_traffic_data(direction, model, video_path, traffic_data):
predictions = model.predict(
video_path,
show=True,
stream=True,
classes=[1, 2, 3, 5, 7],
vid_stride=10,
conf=0.6,
show_labels=True,
verbose=False,
)
for result in predictions:
traffic_data[direction] = len(result.boxes)
def calculate_green_time(traffic_data):
"""Calculate the green time for each direction."""
total_vehicles = sum(traffic_data.values()) + 20
green_times = []
print(f"\nTotal Vehicles: {total_vehicles}\n")
for direction, count in traffic_data.items():
count += 5
print(f"{direction.capitalize()}: {count}")
green_time = max(7, int(np.log(12.5 * 60 * (np.exp(count) // (total_vehicles + 1)))))
green_times.append([direction, green_time, 0])
print(f"\nGreen Times: {green_times}\n")
return green_times
def calculate_red_times(green_times):
"""Calculate the red time for each direction."""
for i in range(1, len(green_times)):
green_times[i][2] = sum(green_times[j][1] + 5 for j in range(i))
return green_times
def change_lights(lights, final_schedule):
"""Change lights based on the calculated schedule."""
while final_schedule:
print(f"\nFinal Schedule: {final_schedule}\n")
current = final_schedule[0]
next_directions = final_schedule[1:4]
# Set green light for current direction
lights[current[0]].set_state("Green", current[1])
for dir in next_directions:
lights[dir[0]].set_state("Red", dir[2])
time.sleep(current[1])
# Set yellow light for current direction
lights[current[0]].set_state("Yellow", 5)
for dir in next_directions:
dir[2]=dir[2]-current[1]
lights[dir[0]].set_state("Red", dir[2])
time.sleep(5)
# Rotate the schedule and update red times
final_schedule.pop(0)
final_schedule = calculate_red_times(final_schedule)
# Add new green time schedule every cycle
if current[0] == "north":
final_schedule.extend(calculate_green_time(traffic_data))
final_schedule = calculate_red_times(final_schedule)
def start_traffic_system(video_paths):
"""Start the traffic management system."""
print("\n FLUXION by ERROR 404\n")
# Capture real-time traffic data
for direction, video_path in video_paths.items():
t.Thread(target=capture_traffic_data, args=(direction, models[direction], video_path, traffic_data)).start()
time.sleep(10) # Allow time for initial data capture
# Calculate initial green and red times
schedule = calculate_green_time(traffic_data)
schedule = calculate_red_times(schedule)
# Start the automated system
t.Thread(target=change_lights, args=(lights, schedule)).start()
# Video paths for each direction
video_paths = {
"north": "D:/North.mp4",
"south": "D:/South.mp4",
"east": "D:/East.mp4",
"west": "D:/West.mp4",
}
# Start the system
start_traffic_system(video_paths)