Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,52 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
import time
|
| 4 |
|
| 5 |
-
# Function to
|
| 6 |
-
def
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
# Create the figure
|
| 11 |
-
fig = go.Figure()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
x0=0, y0=3, x1=1, y1=4,
|
| 17 |
-
line=dict(color="black", width=2),
|
| 18 |
-
fillcolor=colors['red'] if signal_color == 'red' else 'gray'
|
| 19 |
-
)
|
| 20 |
-
fig.add_shape(
|
| 21 |
-
type="rect",
|
| 22 |
-
x0=0, y0=2, x1=1, y1=3,
|
| 23 |
-
line=dict(color="black", width=2),
|
| 24 |
-
fillcolor=colors['yellow'] if signal_color == 'yellow' else 'gray'
|
| 25 |
-
)
|
| 26 |
-
fig.add_shape(
|
| 27 |
-
type="rect",
|
| 28 |
-
x0=0, y0=1, x1=1, y1=2,
|
| 29 |
-
line=dict(color="black", width=2),
|
| 30 |
-
fillcolor=colors['green'] if signal_color == 'green' else 'gray'
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
# Layout setup
|
| 34 |
-
fig.update_layout(
|
| 35 |
-
title="Animated Traffic Signal",
|
| 36 |
-
xaxis=dict(showgrid=False, zeroline=False),
|
| 37 |
-
yaxis=dict(showgrid=False, zeroline=False),
|
| 38 |
-
width=200,
|
| 39 |
-
height=400,
|
| 40 |
-
showlegend=False,
|
| 41 |
-
plot_bgcolor='white',
|
| 42 |
-
margin=dict(l=0, r=0, t=50, b=50)
|
| 43 |
-
)
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Streamlit UI
|
| 48 |
st.title("Animated Traffic Signal")
|
| 49 |
|
| 50 |
-
# Information
|
| 51 |
st.markdown("""
|
| 52 |
-
This Streamlit app
|
| 53 |
-
**
|
| 54 |
""")
|
| 55 |
|
| 56 |
-
#
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
# Run a loop that updates the signal color
|
| 60 |
while True:
|
| 61 |
-
for color in
|
| 62 |
-
#
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
# Wait for 3 seconds before
|
| 67 |
time.sleep(3)
|
| 68 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image, ImageDraw, ImageColor
|
| 3 |
import time
|
| 4 |
|
| 5 |
+
# Function to generate traffic signal image based on the current color
|
| 6 |
+
def generate_traffic_signal(signal_color):
|
| 7 |
+
# Create a blank image with a white background
|
| 8 |
+
img = Image.new("RGB", (200, 400), "white")
|
| 9 |
+
draw = ImageDraw.Draw(img)
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Define the rectangle dimensions for the lights
|
| 12 |
+
light_width, light_height = 160, 80
|
| 13 |
+
margin = 20
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Define the positions of the lights
|
| 16 |
+
positions = [(margin, margin), (margin, margin + light_height + margin), (margin, 2 * (light_height + margin))]
|
| 17 |
+
colors = ['red', 'yellow', 'green']
|
| 18 |
+
|
| 19 |
+
# Draw the lights based on the current signal color
|
| 20 |
+
for i, color in enumerate(colors):
|
| 21 |
+
# Set the fill color to the current light color if it's the active light
|
| 22 |
+
fill_color = signal_color if signal_color == color else "gray"
|
| 23 |
+
draw.rectangle([positions[i], (positions[i][0] + light_width, positions[i][1] + light_height)], fill=fill_color)
|
| 24 |
+
|
| 25 |
+
return img
|
| 26 |
|
| 27 |
# Streamlit UI
|
| 28 |
st.title("Animated Traffic Signal")
|
| 29 |
|
| 30 |
+
# Information text
|
| 31 |
st.markdown("""
|
| 32 |
+
This Streamlit app simulates an animated traffic signal.
|
| 33 |
+
The signal will cycle through **Red**, **Yellow**, and **Green** every few seconds.
|
| 34 |
""")
|
| 35 |
|
| 36 |
+
# Display the animation
|
| 37 |
+
signal_colors = ['red', 'yellow', 'green']
|
| 38 |
+
|
| 39 |
+
# Run the animation in a loop
|
| 40 |
+
st.write("The traffic light will cycle between red, yellow, and green every 3 seconds.")
|
| 41 |
|
|
|
|
| 42 |
while True:
|
| 43 |
+
for color in signal_colors:
|
| 44 |
+
# Generate the traffic signal image
|
| 45 |
+
traffic_signal_img = generate_traffic_signal(color)
|
| 46 |
+
|
| 47 |
+
# Display the image using Streamlit
|
| 48 |
+
st.image(traffic_signal_img, caption=f"Traffic Signal: {color.capitalize()}", use_column_width=True)
|
| 49 |
|
| 50 |
+
# Wait for 3 seconds before changing to the next signal
|
| 51 |
time.sleep(3)
|
| 52 |
|