Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -136,23 +136,45 @@ def process_traffic_image(image):
|
|
136 |
"""
|
137 |
Orchestrates the traffic analysis workflow.
|
138 |
"""
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
-
|
145 |
signal_timing = optimize_signal_rl(congestion_level)
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
156 |
|
157 |
# Gradio Interface
|
158 |
def gradio_interface(image):
|
|
|
136 |
"""
|
137 |
Orchestrates the traffic analysis workflow.
|
138 |
"""
|
139 |
+
# Save the uploaded image temporarily
|
140 |
+
image_path = "temp_traffic_image.jpg"
|
141 |
+
image.save(image_path)
|
142 |
+
|
143 |
+
# Analyze traffic and optimize signal timing
|
144 |
+
with open(image_path, "rb") as img_file:
|
145 |
+
response = requests.post(
|
146 |
+
f"{HOSTED_API_URL}/analyze_traffic/",
|
147 |
+
files={"file": img_file}
|
148 |
+
)
|
149 |
+
|
150 |
+
os.remove(image_path) # Clean up the temporary image
|
151 |
+
|
152 |
+
if response.status_code == 200:
|
153 |
+
data = response.json()
|
154 |
+
vehicle_count = data.get("vehicle_count", 0)
|
155 |
+
congestion_level = data.get("congestion_level", "Unknown")
|
156 |
+
flow_rate = data.get("flow_rate", "Unknown")
|
157 |
+
processed_image_base64 = data.get("processed_image", None)
|
158 |
+
|
159 |
+
# Decode the processed image
|
160 |
+
if processed_image_base64:
|
161 |
+
processed_image = Image.open(io.BytesIO(base64.b64decode(processed_image_base64)))
|
162 |
+
else:
|
163 |
+
processed_image = None
|
164 |
|
165 |
+
# Signal timing optimization
|
166 |
signal_timing = optimize_signal_rl(congestion_level)
|
167 |
|
168 |
+
# Return the results
|
169 |
+
return (
|
170 |
+
f"Detected Vehicles: {vehicle_count}\n"
|
171 |
+
f"Congestion Level: {congestion_level}\n"
|
172 |
+
f"Traffic Flow: {flow_rate}\n"
|
173 |
+
f"Signal Timing Suggestion: {signal_timing}",
|
174 |
+
processed_image
|
175 |
+
)
|
176 |
+
else:
|
177 |
+
return "Error in analyzing traffic.", None
|
178 |
|
179 |
# Gradio Interface
|
180 |
def gradio_interface(image):
|