Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -27,15 +27,9 @@ logging.basicConfig(
|
|
27 |
)
|
28 |
|
29 |
# Directories
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
os.makedirs(CAPTURED_FRAMES_DIR, exist_ok=True)
|
34 |
-
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
35 |
-
os.makedirs(FLIGHT_LOG_DIR, exist_ok=True)
|
36 |
-
os.chmod(CAPTURED_FRAMES_DIR, 0o777)
|
37 |
-
os.chmod(OUTPUT_DIR, 0o777)
|
38 |
-
os.chmod(FLIGHT_LOG_DIR, 0o777)
|
39 |
|
40 |
# Global variables
|
41 |
log_entries: List[str] = []
|
@@ -79,7 +73,7 @@ def zip_directory(folder_path: str, zip_path: str) -> str:
|
|
79 |
return ""
|
80 |
|
81 |
def generate_map(gps_coords: List[List[float]], items: List[Dict[str, Any]]) -> str:
|
82 |
-
map_path = os.path.join(
|
83 |
plt.figure(figsize=(4, 4))
|
84 |
plt.scatter([x[1] for x in gps_coords], [x[0] for x in gps_coords], c='blue', label='GPS Points')
|
85 |
plt.title("Issue Locations Map")
|
@@ -111,7 +105,7 @@ def write_geotag(image_path: str, gps_coord: List[float]) -> bool:
|
|
111 |
return False
|
112 |
|
113 |
def write_flight_log(frame_count: int, gps_coord: List[float], timestamp: str) -> str:
|
114 |
-
log_path = os.path.join(
|
115 |
try:
|
116 |
with open(log_path, 'w', newline='') as csvfile:
|
117 |
writer = csv.writer(csvfile)
|
@@ -151,7 +145,7 @@ def generate_line_chart() -> Optional[str]:
|
|
151 |
plt.ylabel("Count")
|
152 |
plt.grid(True)
|
153 |
plt.tight_layout()
|
154 |
-
chart_path = os.path.join(
|
155 |
plt.savefig(chart_path)
|
156 |
plt.close()
|
157 |
return chart_path
|
@@ -188,7 +182,7 @@ def process_video(video, resize_width=4000, resize_height=3000, frame_skip=5):
|
|
188 |
print(f"Input video: {frame_width}x{frame_height} ({input_resolution/1e6:.2f}MP), {fps} FPS, {total_frames} frames, {expected_duration:.2f} seconds, Frame skip: {frame_skip}")
|
189 |
|
190 |
out_width, out_height = resize_width, resize_height
|
191 |
-
output_path = os.path.join(
|
192 |
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (out_width, out_height))
|
193 |
if not out.isOpened():
|
194 |
log_entries.append("Error: Failed to initialize mp4v codec")
|
@@ -265,7 +259,7 @@ def process_video(video, resize_width=4000, resize_height=3000, frame_skip=5):
|
|
265 |
if frame_detections:
|
266 |
detection_frame_count += 1
|
267 |
if detection_frame_count % SAVE_IMAGE_INTERVAL == 0:
|
268 |
-
captured_frame_path = os.path.join(
|
269 |
if cv2.imwrite(captured_frame_path, annotated_frame):
|
270 |
if write_geotag(captured_frame_path, gps_coord):
|
271 |
detected_issues.append(captured_frame_path)
|
@@ -323,7 +317,7 @@ def process_video(video, resize_width=4000, resize_height=3000, frame_skip=5):
|
|
323 |
data_lake_submission["frame_count"] = frame_count
|
324 |
data_lake_submission["gps_coordinates"] = gps_coordinates[-1] if gps_coordinates else [0, 0]
|
325 |
|
326 |
-
submission_json_path = os.path.join(
|
327 |
try:
|
328 |
with open(submission_json_path, 'w') as f:
|
329 |
json.dump(data_lake_submission, f, indent=2)
|
@@ -357,9 +351,8 @@ def process_video(video, resize_width=4000, resize_height=3000, frame_skip=5):
|
|
357 |
chart_path = generate_line_chart()
|
358 |
map_path = generate_map(gps_coordinates[-5:], all_detections)
|
359 |
|
360 |
-
# Zip
|
361 |
-
|
362 |
-
logs_zip = zip_directory(FLIGHT_LOG_DIR, os.path.join(OUTPUT_DIR, "flight_logs.zip"))
|
363 |
|
364 |
return (
|
365 |
output_path,
|
@@ -369,8 +362,8 @@ def process_video(video, resize_width=4000, resize_height=3000, frame_skip=5):
|
|
369 |
chart_path,
|
370 |
map_path,
|
371 |
submission_json_path,
|
372 |
-
|
373 |
-
|
374 |
output_path # For video download
|
375 |
)
|
376 |
|
@@ -398,9 +391,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as iface:
|
|
398 |
gr.Markdown("## Download Results")
|
399 |
with gr.Row():
|
400 |
json_download = gr.File(label="Download Data Lake JSON")
|
401 |
-
|
402 |
-
logs_zip_download = gr.File(label="Download Flight Logs (ZIP)")
|
403 |
-
video_download = gr.File(label="Download Processed Video")
|
404 |
|
405 |
process_btn.click(
|
406 |
fn=process_video,
|
@@ -413,11 +404,11 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange")) as iface:
|
|
413 |
chart_output,
|
414 |
map_output,
|
415 |
json_download,
|
416 |
-
|
417 |
-
|
418 |
video_download
|
419 |
]
|
420 |
)
|
421 |
|
422 |
if __name__ == "__main__":
|
423 |
-
iface.launch()
|
|
|
27 |
)
|
28 |
|
29 |
# Directories
|
30 |
+
UNIFIED_OUTPUT_DIR = "unified_output"
|
31 |
+
os.makedirs(UNIFIED_OUTPUT_DIR, exist_ok=True)
|
32 |
+
os.chmod(UNIFIED_OUTPUT_DIR, 0o777)
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
# Global variables
|
35 |
log_entries: List[str] = []
|
|
|
73 |
return ""
|
74 |
|
75 |
def generate_map(gps_coords: List[List[float]], items: List[Dict[str, Any]]) -> str:
|
76 |
+
map_path = os.path.join(UNIFIED_OUTPUT_DIR, "map_temp.png")
|
77 |
plt.figure(figsize=(4, 4))
|
78 |
plt.scatter([x[1] for x in gps_coords], [x[0] for x in gps_coords], c='blue', label='GPS Points')
|
79 |
plt.title("Issue Locations Map")
|
|
|
105 |
return False
|
106 |
|
107 |
def write_flight_log(frame_count: int, gps_coord: List[float], timestamp: str) -> str:
|
108 |
+
log_path = os.path.join(UNIFIED_OUTPUT_DIR, f"flight_log_{frame_count:06d}.csv")
|
109 |
try:
|
110 |
with open(log_path, 'w', newline='') as csvfile:
|
111 |
writer = csv.writer(csvfile)
|
|
|
145 |
plt.ylabel("Count")
|
146 |
plt.grid(True)
|
147 |
plt.tight_layout()
|
148 |
+
chart_path = os.path.join(UNIFIED_OUTPUT_DIR, "chart_temp.png")
|
149 |
plt.savefig(chart_path)
|
150 |
plt.close()
|
151 |
return chart_path
|
|
|
182 |
print(f"Input video: {frame_width}x{frame_height} ({input_resolution/1e6:.2f}MP), {fps} FPS, {total_frames} frames, {expected_duration:.2f} seconds, Frame skip: {frame_skip}")
|
183 |
|
184 |
out_width, out_height = resize_width, resize_height
|
185 |
+
output_path = os.path.join(UNIFIED_OUTPUT_DIR, "processed_output.mp4")
|
186 |
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (out_width, out_height))
|
187 |
if not out.isOpened():
|
188 |
log_entries.append("Error: Failed to initialize mp4v codec")
|
|
|
259 |
if frame_detections:
|
260 |
detection_frame_count += 1
|
261 |
if detection_frame_count % SAVE_IMAGE_INTERVAL == 0:
|
262 |
+
captured_frame_path = os.path.join(UNIFIED_OUTPUT_DIR, f"detected_{frame_count:06d}.jpg")
|
263 |
if cv2.imwrite(captured_frame_path, annotated_frame):
|
264 |
if write_geotag(captured_frame_path, gps_coord):
|
265 |
detected_issues.append(captured_frame_path)
|
|
|
317 |
data_lake_submission["frame_count"] = frame_count
|
318 |
data_lake_submission["gps_coordinates"] = gps_coordinates[-1] if gps_coordinates else [0, 0]
|
319 |
|
320 |
+
submission_json_path = os.path.join(UNIFIED_OUTPUT_DIR, "data_lake_submission.json")
|
321 |
try:
|
322 |
with open(submission_json_path, 'w') as f:
|
323 |
json.dump(data_lake_submission, f, indent=2)
|
|
|
351 |
chart_path = generate_line_chart()
|
352 |
map_path = generate_map(gps_coordinates[-5:], all_detections)
|
353 |
|
354 |
+
# Zip all contents from the unified folder
|
355 |
+
zip_path = zip_directory(UNIFIED_OUTPUT_DIR, os.path.join(UNIFIED_OUTPUT_DIR, "all_files.zip"))
|
|
|
356 |
|
357 |
return (
|
358 |
output_path,
|
|
|
362 |
chart_path,
|
363 |
map_path,
|
364 |
submission_json_path,
|
365 |
+
zip_path, # Single zip file for all files
|
366 |
+
zip_path, # Same for logs and images as they are now in one folder
|
367 |
output_path # For video download
|
368 |
)
|
369 |
|
|
|
391 |
gr.Markdown("## Download Results")
|
392 |
with gr.Row():
|
393 |
json_download = gr.File(label="Download Data Lake JSON")
|
394 |
+
zip_download = gr.File(label="Download All Files (ZIP)")
|
|
|
|
|
395 |
|
396 |
process_btn.click(
|
397 |
fn=process_video,
|
|
|
404 |
chart_output,
|
405 |
map_output,
|
406 |
json_download,
|
407 |
+
zip_download,
|
408 |
+
zip_download, # Same zip for logs, images
|
409 |
video_download
|
410 |
]
|
411 |
)
|
412 |
|
413 |
if __name__ == "__main__":
|
414 |
+
iface.launch()
|