Update src/streamlit_app.py
Browse files- src/streamlit_app.py +58 -8
src/streamlit_app.py
CHANGED
|
@@ -2,12 +2,62 @@ import altair as alt
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
from traffic_logic import get_next_green
|
| 8 |
|
| 9 |
st.set_page_config(page_title="Smart Traffic Light System", layout="wide")
|
| 10 |
-
st.title("π¦ Smart Traffic Light Simulation")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
st.sidebar.header("Vehicle Count per Road")
|
| 13 |
north = st.sidebar.slider("North Road", 0, 50, 10)
|
|
@@ -18,9 +68,9 @@ west = st.sidebar.slider("West Road", 0, 50, 15)
|
|
| 18 |
vehicle_counts = {'North': north, 'East': east, 'South': south, 'West': west}
|
| 19 |
next_green = get_next_green(vehicle_counts)
|
| 20 |
|
| 21 |
-
st.subheader("Traffic Light Status")
|
| 22 |
|
| 23 |
-
cols = st.columns(4)
|
| 24 |
-
for i, (road, count) in enumerate(vehicle_counts.items()):
|
| 25 |
-
|
| 26 |
-
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
import streamlit as st
|
| 5 |
+
import cv2
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from ultralytics import YOLO
|
| 8 |
from traffic_logic import get_next_green
|
| 9 |
|
| 10 |
st.set_page_config(page_title="Smart Traffic Light System", layout="wide")
|
| 11 |
+
# st.title("π¦ Smart Traffic Light Simulation")
|
| 12 |
+
|
| 13 |
+
# Load YOLO model
|
| 14 |
+
model = YOLO("yolov8n.pt")
|
| 15 |
+
|
| 16 |
+
# UI
|
| 17 |
+
st.title("π¦ Smart Traffic Light System for 4-Way Intersection (YOLO-Based)")
|
| 18 |
+
st.markdown("Upload traffic images from **4 directions** to simulate smart light control.")
|
| 19 |
+
# Upload images for 4 directions
|
| 20 |
+
cols = st.columns(4)
|
| 21 |
+
with cols[0]: n_img = st.file_uploader("North", type=["jpg", "png", "jpeg"], key="north")
|
| 22 |
+
with cols[1]: s_img = st.file_uploader("South", type=["jpg", "png", "jpeg"], key="south")
|
| 23 |
+
with cols[2]: e_img = st.file_uploader("East", type=["jpg", "png", "jpeg"], key="east")
|
| 24 |
+
with cols[3]: w_img = st.file_uploader("West", type=["jpg", "png", "jpeg"], key="west")
|
| 25 |
+
images = {'North': n_img, 'South': s_img, 'East': e_img, 'West': w_img}
|
| 26 |
+
counts = {}
|
| 27 |
+
results_imgs = {}
|
| 28 |
+
|
| 29 |
+
if all(images.values()):
|
| 30 |
+
for direction, uploaded_file in images.items():
|
| 31 |
+
img = Image.open(uploaded_file).convert("RGB")
|
| 32 |
+
np_img = np.array(img)
|
| 33 |
+
|
| 34 |
+
result = model.predict(np_img)[0]
|
| 35 |
+
detections = result.boxes
|
| 36 |
+
|
| 37 |
+
# Count cars, trucks, etc.
|
| 38 |
+
car_classes = [2, 3, 5, 7]
|
| 39 |
+
car_count = sum(1 for box in detections if int(box.cls) in car_classes)
|
| 40 |
+
counts[direction] = car_count
|
| 41 |
+
|
| 42 |
+
# Draw boxes
|
| 43 |
+
results_imgs[direction] = result.plot()
|
| 44 |
+
|
| 45 |
+
# Determine highest traffic
|
| 46 |
+
max_dir = max(counts, key=counts.get)
|
| 47 |
+
|
| 48 |
+
st.markdown("### πΈ YOLO Detection Results")
|
| 49 |
+
for direction in ['North', 'South', 'East', 'West']:
|
| 50 |
+
st.image(results_imgs[direction], caption=f"{direction} - Vehicles: {counts[direction]}")
|
| 51 |
+
|
| 52 |
+
# Show traffic light result
|
| 53 |
+
st.markdown("## π¦ Traffic Light Control Result:")
|
| 54 |
+
for direction in ['North', 'South', 'East', 'West']:
|
| 55 |
+
if direction == max_dir:
|
| 56 |
+
st.success(f"π’ {direction} β GREEN (Traffic: {counts[direction]})")
|
| 57 |
+
else:
|
| 58 |
+
st.error(f"π΄ {direction} β RED (Traffic: {counts[direction]})")
|
| 59 |
+
else:
|
| 60 |
+
st.warning("Please upload images for all 4 directions to simulate.")
|
| 61 |
|
| 62 |
st.sidebar.header("Vehicle Count per Road")
|
| 63 |
north = st.sidebar.slider("North Road", 0, 50, 10)
|
|
|
|
| 68 |
vehicle_counts = {'North': north, 'East': east, 'South': south, 'West': west}
|
| 69 |
next_green = get_next_green(vehicle_counts)
|
| 70 |
|
| 71 |
+
# st.subheader("Traffic Light Status")
|
| 72 |
|
| 73 |
+
# cols = st.columns(4)
|
| 74 |
+
# for i, (road, count) in enumerate(vehicle_counts.items()):
|
| 75 |
+
# light_color = "π’ Green" if road == next_green else "π΄ Red"
|
| 76 |
+
# cols[i].metric(label=f"{road} Road", value=f"{count} vehicles", delta=light_color)
|