Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -49,13 +49,39 @@ def zip_directory(folder_path: str, zip_path: str) -> str:
|
|
| 49 |
for file in files:
|
| 50 |
file_path = os.path.join(root, file)
|
| 51 |
arcname = os.path.relpath(file_path, folder_path)
|
| 52 |
-
zipf.write(file_path, arcname)
|
| 53 |
return zip_path
|
| 54 |
except Exception as e:
|
| 55 |
logging.error(f"Failed to zip {folder_path}: {str(e)}")
|
| 56 |
log_entries.append(f"Error: Failed to zip {folder_path}: {str(e)}")
|
| 57 |
return ""
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
def generate_map(gps_coords: List[List[float]], items: List[Dict[str, Any]]) -> str:
|
| 60 |
map_path = os.path.join(OUTPUT_DIR, "map_temp.png")
|
| 61 |
plt.figure(figsize=(4, 4))
|
|
@@ -147,7 +173,11 @@ def generate_report(
|
|
| 147 |
output_duration: float,
|
| 148 |
detection_frame_count: int,
|
| 149 |
chart_path: str,
|
| 150 |
-
map_path: str
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
) -> str:
|
| 152 |
report_path = os.path.join(OUTPUT_DIR, f"drone_analysis_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md")
|
| 153 |
report_content = [
|
|
@@ -199,50 +229,74 @@ def generate_report(
|
|
| 199 |
report_content.append(f" - {item['type']}: {item['count']} ({percentage:.2f}%)")
|
| 200 |
report_content.extend([
|
| 201 |
f"- Processing Time: {total_time:.2f} seconds",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
f"- Timestamp: {metrics.get('timestamp', 'N/A')}",
|
| 203 |
"- Summary: Potholes and cracks detected in high-traffic segments.",
|
| 204 |
"",
|
| 205 |
-
"## 5. Geotagged
|
| 206 |
f"- Total Images: {len(detected_issues)}",
|
| 207 |
-
f"- Storage: Data Lake `/project_xyz/images/{datetime.now().strftime('%Y-%m-%d')}`"
|
|
|
|
|
|
|
|
|
|
| 208 |
])
|
| 209 |
|
| 210 |
-
|
| 211 |
-
report_content.
|
| 212 |
-
"|
|
| 213 |
-
|
| 214 |
-
])
|
| 215 |
-
for i, detection in enumerate(all_detections[:5]):
|
| 216 |
-
report_content.append(
|
| 217 |
-
f"| {detection['frame']:06d} | {detection['label']} | ({detection['gps'][0]}, {detection['gps'][1]}) | {detection['timestamp']} | {detection['path']} |"
|
| 218 |
-
)
|
| 219 |
|
| 220 |
report_content.extend([
|
| 221 |
"",
|
| 222 |
-
"## 6. Flight
|
| 223 |
f"- Total Logs: {len(detected_issues)}",
|
| 224 |
-
"- Parameters: Frame, Timestamp, Latitude, Longitude, Speed (5 m/s), Satellites (12), Altitude (60 m)",
|
| 225 |
-
"- Sample Log:",
|
| 226 |
-
"```csv",
|
| 227 |
-
f"Frame,Timestamp,Latitude,Longitude,Speed_ms,Satellites,Altitude_m",
|
| 228 |
-
f"{all_detections[0]['frame'] if all_detections else 0},{all_detections[0]['timestamp'] if all_detections else 'N/A'},{all_detections[0]['gps'][0] if all_detections else 0},{all_detections[0]['gps'][1] if all_detections else 0},5.0,12,60",
|
| 229 |
-
"```",
|
| 230 |
f"- Storage: Data Lake `/project_xyz/flight_logs/{datetime.now().strftime('%Y-%m-%d')}`",
|
| 231 |
"",
|
| 232 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
f"- Detection Trend Chart: `/project_xyz/charts/chart_temp_{datetime.now().strftime('%Y%m%d')}.png`",
|
| 234 |
f"- Issue Locations Map: `/project_xyz/maps/map_temp_{datetime.now().strftime('%Y%m%d')}.png`",
|
| 235 |
"",
|
| 236 |
-
"##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
"- AE/IE Comments: [Pending]",
|
| 238 |
"- PD/RO Comments: [Pending]",
|
| 239 |
"",
|
| 240 |
-
"##
|
| 241 |
"- Repair potholes in high-traffic segments.",
|
| 242 |
"- Seal cracks to prevent degradation.",
|
| 243 |
"- Schedule follow-up survey.",
|
| 244 |
"",
|
| 245 |
-
"##
|
| 246 |
f"- Images: `/project_xyz/images/{datetime.now().strftime('%Y-%m-%d')}`",
|
| 247 |
f"- Flight Logs: `/project_xyz/flight_logs/{datetime.now().strftime('%Y-%m-%d')}`",
|
| 248 |
f"- Video: `/project_xyz/videos/processed_output_{datetime.now().strftime('%Y%m%d')}.mp4`",
|
|
@@ -424,9 +478,15 @@ def process_video(video, resize_width=4000, resize_height=3000, frame_skip=5):
|
|
| 424 |
output_duration,
|
| 425 |
detection_frame_count,
|
| 426 |
chart_path,
|
| 427 |
-
map_path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
)
|
| 429 |
|
|
|
|
|
|
|
| 430 |
return (
|
| 431 |
output_path,
|
| 432 |
json.dumps(last_metrics, indent=2),
|
|
@@ -434,7 +494,7 @@ def process_video(video, resize_width=4000, resize_height=3000, frame_skip=5):
|
|
| 434 |
detected_issues,
|
| 435 |
chart_path,
|
| 436 |
map_path,
|
| 437 |
-
|
| 438 |
)
|
| 439 |
|
| 440 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as iface:
|
|
@@ -459,7 +519,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as iface:
|
|
| 459 |
with gr.Row():
|
| 460 |
gr.Markdown("## Download Results")
|
| 461 |
with gr.Row():
|
| 462 |
-
|
| 463 |
|
| 464 |
process_btn.click(
|
| 465 |
fn=process_video,
|
|
@@ -471,7 +531,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as iface:
|
|
| 471 |
issue_gallery,
|
| 472 |
chart_output,
|
| 473 |
map_output,
|
| 474 |
-
|
| 475 |
]
|
| 476 |
)
|
| 477 |
|
|
|
|
| 49 |
for file in files:
|
| 50 |
file_path = os.path.join(root, file)
|
| 51 |
arcname = os.path.relpath(file_path, folder_path)
|
| 52 |
+
zipf.write(file_path, os.path.join(os.path.basename(folder_path), arcname))
|
| 53 |
return zip_path
|
| 54 |
except Exception as e:
|
| 55 |
logging.error(f"Failed to zip {folder_path}: {str(e)}")
|
| 56 |
log_entries.append(f"Error: Failed to zip {folder_path}: {str(e)}")
|
| 57 |
return ""
|
| 58 |
|
| 59 |
+
def zip_all_outputs(report_path: str, video_path: str, chart_path: str, map_path: str) -> str:
|
| 60 |
+
zip_path = os.path.join(OUTPUT_DIR, f"drone_analysis_outputs_{datetime.now().strftime('%Y%m%d_%H%M%S')}.zip")
|
| 61 |
+
try:
|
| 62 |
+
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 63 |
+
if os.path.exists(report_path):
|
| 64 |
+
zipf.write(report_path, os.path.basename(report_path))
|
| 65 |
+
if os.path.exists(video_path):
|
| 66 |
+
zipf.write(video_path, os.path.basename(video_path))
|
| 67 |
+
if os.path.exists(chart_path):
|
| 68 |
+
zipf.write(chart_path, os.path.basename(chart_path))
|
| 69 |
+
if os.path.exists(map_path):
|
| 70 |
+
zipf.write(map_path, os.path.basename(map_path))
|
| 71 |
+
for root, _, files in os.walk(CAPTURED_FRAMES_DIR):
|
| 72 |
+
for file in files:
|
| 73 |
+
file_path = os.path.join(root, file)
|
| 74 |
+
zipf.write(file_path, os.path.join("captured_frames", file))
|
| 75 |
+
for root, _, files in os.walk(FLIGHT_LOG_DIR):
|
| 76 |
+
for file in files:
|
| 77 |
+
file_path = os.path.join(root, file)
|
| 78 |
+
zipf.write(file_path, os.path.join("flight_logs", file))
|
| 79 |
+
return zip_path
|
| 80 |
+
except Exception as e:
|
| 81 |
+
logging.error(f"Failed to create output ZIP: {str(e)}")
|
| 82 |
+
log_entries.append(f"Error: Failed to create output ZIP: {str(e)}")
|
| 83 |
+
return ""
|
| 84 |
+
|
| 85 |
def generate_map(gps_coords: List[List[float]], items: List[Dict[str, Any]]) -> str:
|
| 86 |
map_path = os.path.join(OUTPUT_DIR, "map_temp.png")
|
| 87 |
plt.figure(figsize=(4, 4))
|
|
|
|
| 173 |
output_duration: float,
|
| 174 |
detection_frame_count: int,
|
| 175 |
chart_path: str,
|
| 176 |
+
map_path: str,
|
| 177 |
+
frame_times: List[float],
|
| 178 |
+
resize_times: List[float],
|
| 179 |
+
inference_times: List[float],
|
| 180 |
+
io_times: List[float]
|
| 181 |
) -> str:
|
| 182 |
report_path = os.path.join(OUTPUT_DIR, f"drone_analysis_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md")
|
| 183 |
report_content = [
|
|
|
|
| 229 |
report_content.append(f" - {item['type']}: {item['count']} ({percentage:.2f}%)")
|
| 230 |
report_content.extend([
|
| 231 |
f"- Processing Time: {total_time:.2f} seconds",
|
| 232 |
+
f"- Average Frame Time: {sum(frame_times)/len(frame_times):.2f} ms",
|
| 233 |
+
f"- Average Resize Time: {sum(resize_times)/len(resize_times):.2f} ms",
|
| 234 |
+
f"- Average Inference Time: {sum(inference_times)/len(inference_times):.2f} ms",
|
| 235 |
+
f"- Average I/O Time: {sum(io_times)/len(io_times):.2f} ms",
|
| 236 |
f"- Timestamp: {metrics.get('timestamp', 'N/A')}",
|
| 237 |
"- Summary: Potholes and cracks detected in high-traffic segments.",
|
| 238 |
"",
|
| 239 |
+
"## 5. Geotagged Images",
|
| 240 |
f"- Total Images: {len(detected_issues)}",
|
| 241 |
+
f"- Storage: Data Lake `/project_xyz/images/{datetime.now().strftime('%Y-%m-%d')}`",
|
| 242 |
+
"",
|
| 243 |
+
"| Frame | Issue Type | GPS (Lat, Lon) | Timestamp | Confidence | Image Path |",
|
| 244 |
+
"|-------|------------|----------------|-----------|------------|------------|"
|
| 245 |
])
|
| 246 |
|
| 247 |
+
for detection in all_detections:
|
| 248 |
+
report_content.append(
|
| 249 |
+
f"| {detection['frame']:06d} | {detection['label']} | ({detection['gps'][0]:.6f}, {detection['gps'][1]:.6f}) | {detection['timestamp']} | {detection['conf']:.2f} | {detection['path']} |"
|
| 250 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
report_content.extend([
|
| 253 |
"",
|
| 254 |
+
"## 6. Flight Logs",
|
| 255 |
f"- Total Logs: {len(detected_issues)}",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
f"- Storage: Data Lake `/project_xyz/flight_logs/{datetime.now().strftime('%Y-%m-%d')}`",
|
| 257 |
"",
|
| 258 |
+
"| Frame | Timestamp | Latitude | Longitude | Speed (m/s) | Satellites | Altitude (m) | Log Path |",
|
| 259 |
+
"|-------|-----------|----------|-----------|-------------|------------|--------------|----------|"
|
| 260 |
+
])
|
| 261 |
+
|
| 262 |
+
for detection in all_detections:
|
| 263 |
+
log_path = os.path.join(FLIGHT_LOG_DIR, f"flight_log_{detection['frame']:06d}.csv")
|
| 264 |
+
report_content.append(
|
| 265 |
+
f"| {detection['frame']:06d} | {detection['timestamp']} | {detection['gps'][0]:.6f} | {detection['gps'][1]:.6f} | 5.0 | 12 | 60 | {log_path} |"
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
report_content.extend([
|
| 269 |
+
"",
|
| 270 |
+
"## 7. Processed Video",
|
| 271 |
+
f"- Path: `/project_xyz/videos/processed_output_{datetime.now().strftime('%Y%m%d')}.mp4`",
|
| 272 |
+
f"- Frames: {output_frames}",
|
| 273 |
+
f"- FPS: {output_fps:.2f}",
|
| 274 |
+
f"- Duration: {output_duration:.2f} seconds",
|
| 275 |
+
"",
|
| 276 |
+
"## 8. Visualizations",
|
| 277 |
f"- Detection Trend Chart: `/project_xyz/charts/chart_temp_{datetime.now().strftime('%Y%m%d')}.png`",
|
| 278 |
f"- Issue Locations Map: `/project_xyz/maps/map_temp_{datetime.now().strftime('%Y%m%d')}.png`",
|
| 279 |
"",
|
| 280 |
+
"## 9. Processing Timestamps",
|
| 281 |
+
f"- Total Processing Time: {total_time:.2f} seconds",
|
| 282 |
+
f"- Log Entries (Last 10):"
|
| 283 |
+
])
|
| 284 |
+
|
| 285 |
+
for entry in log_entries[-10:]:
|
| 286 |
+
report_content.append(f" - {entry}")
|
| 287 |
+
|
| 288 |
+
report_content.extend([
|
| 289 |
+
"",
|
| 290 |
+
"## 10. Stakeholder Validation",
|
| 291 |
"- AE/IE Comments: [Pending]",
|
| 292 |
"- PD/RO Comments: [Pending]",
|
| 293 |
"",
|
| 294 |
+
"## 11. Recommendations",
|
| 295 |
"- Repair potholes in high-traffic segments.",
|
| 296 |
"- Seal cracks to prevent degradation.",
|
| 297 |
"- Schedule follow-up survey.",
|
| 298 |
"",
|
| 299 |
+
"## 12. Data Lake References",
|
| 300 |
f"- Images: `/project_xyz/images/{datetime.now().strftime('%Y-%m-%d')}`",
|
| 301 |
f"- Flight Logs: `/project_xyz/flight_logs/{datetime.now().strftime('%Y-%m-%d')}`",
|
| 302 |
f"- Video: `/project_xyz/videos/processed_output_{datetime.now().strftime('%Y%m%d')}.mp4`",
|
|
|
|
| 478 |
output_duration,
|
| 479 |
detection_frame_count,
|
| 480 |
chart_path,
|
| 481 |
+
map_path,
|
| 482 |
+
frame_times,
|
| 483 |
+
resize_times,
|
| 484 |
+
inference_times,
|
| 485 |
+
io_times
|
| 486 |
)
|
| 487 |
|
| 488 |
+
output_zip_path = zip_all_outputs(report_path, output_path, chart_path, map_path)
|
| 489 |
+
|
| 490 |
return (
|
| 491 |
output_path,
|
| 492 |
json.dumps(last_metrics, indent=2),
|
|
|
|
| 494 |
detected_issues,
|
| 495 |
chart_path,
|
| 496 |
map_path,
|
| 497 |
+
output_zip_path
|
| 498 |
)
|
| 499 |
|
| 500 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as iface:
|
|
|
|
| 519 |
with gr.Row():
|
| 520 |
gr.Markdown("## Download Results")
|
| 521 |
with gr.Row():
|
| 522 |
+
output_zip_download = gr.File(label="Download All Outputs (ZIP)")
|
| 523 |
|
| 524 |
process_btn.click(
|
| 525 |
fn=process_video,
|
|
|
|
| 531 |
issue_gallery,
|
| 532 |
chart_output,
|
| 533 |
map_output,
|
| 534 |
+
output_zip_download
|
| 535 |
]
|
| 536 |
)
|
| 537 |
|