Fzina commited on
Commit
0ecd1f9
·
verified ·
1 Parent(s): 2479778

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -14
app.py CHANGED
@@ -136,23 +136,45 @@ def process_traffic_image(image):
136
  """
137
  Orchestrates the traffic analysis workflow.
138
  """
139
- try:
140
- # Save the uploaded image temp
141
- image_path = "temp_traffic_image.jpg"
142
- image.save(image_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
 
144
- vehicle_count, congestion_level, flow_rate, processed_image = analyze_traffic(image_path)
145
  signal_timing = optimize_signal_rl(congestion_level)
146
 
147
- os.remove(image_path) # Clean up
148
-
149
- return (f"Detected Vehicles: {vehicle_count}\n"
150
- f"Congestion Level: {congestion_level}\n"
151
- f"Traffic Flow: {flow_rate}\n"
152
- f"Signal Timing Suggestion: {signal_timing}", processed_image)
153
- except Exception as e:
154
- logging.error(f"Error processing traffic image: {e}")
155
- return "An error occurred while processing the image.", None
 
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):