Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -264,25 +264,62 @@ def estimate_eps(points_for_estimate, sample_size=3000, k=8):
|
|
| 264 |
return float(np.percentile(kth, 75))
|
| 265 |
|
| 266 |
# ---- Main processing function for Gradio ----
|
| 267 |
-
def handle_file_upload(uploaded_file):
|
| 268 |
"""
|
| 269 |
Handles file upload by converting the .pts file to .npy for faster processing.
|
|
|
|
| 270 |
"""
|
| 271 |
if uploaded_file is None:
|
| 272 |
-
return "No file uploaded.", gr.update(interactive=False), gr.update(visible=False), [None, None]
|
| 273 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
temp_root = os.path.join(tempfile.gettempdir(), f"temp_pts_{uuid.uuid4().hex}")
|
| 275 |
os.makedirs(temp_root, exist_ok=True)
|
| 276 |
|
| 277 |
pts_npy_path = os.path.join(temp_root, "cloud_points.npy")
|
| 278 |
cols_npy_path = os.path.join(temp_root, "cloud_colors.npy")
|
| 279 |
|
| 280 |
-
pts_to_npy(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
return (f"File loaded successfully. Click 'Start Analysis' to continue.",
|
| 283 |
gr.update(interactive=True),
|
| 284 |
gr.update(visible=False),
|
| 285 |
-
[pts_npy_path, cols_npy_path]
|
|
|
|
| 286 |
|
| 287 |
def start_analysis(file_state, progress=gr.Progress(track_tqdm=True)):
|
| 288 |
"""
|
|
@@ -475,6 +512,7 @@ with gr.Blocks(title="Detection") as demo:
|
|
| 475 |
gr.Markdown("Upload a `.pts` file to analyze the point cloud data.")
|
| 476 |
|
| 477 |
file_state = gr.State(value=[None, None])
|
|
|
|
| 478 |
|
| 479 |
with gr.Row():
|
| 480 |
file_input = gr.File(label="Upload .pts (X Y Z R G B)")
|
|
@@ -487,8 +525,8 @@ with gr.Blocks(title="Detection") as demo:
|
|
| 487 |
|
| 488 |
file_input.upload(
|
| 489 |
fn=handle_file_upload,
|
| 490 |
-
inputs=[file_input],
|
| 491 |
-
outputs=[output_text, analysis_button, download_button, file_state],
|
| 492 |
queue=False
|
| 493 |
)
|
| 494 |
|
|
|
|
| 264 |
return float(np.percentile(kth, 75))
|
| 265 |
|
| 266 |
# ---- Main processing function for Gradio ----
|
| 267 |
+
def handle_file_upload(uploaded_file, last_file_info):
|
| 268 |
"""
|
| 269 |
Handles file upload by converting the .pts file to .npy for faster processing.
|
| 270 |
+
Now uses Gradio State for persistent caching.
|
| 271 |
"""
|
| 272 |
if uploaded_file is None:
|
| 273 |
+
return "No file uploaded.", gr.update(interactive=False), gr.update(visible=False), [None, None], {}
|
| 274 |
|
| 275 |
+
file_path = uploaded_file.name
|
| 276 |
+
|
| 277 |
+
if not os.path.exists(file_path):
|
| 278 |
+
return "Error: Uploaded file not found.", gr.update(interactive=False), gr.update(visible=False), [None, None], {}
|
| 279 |
+
|
| 280 |
+
current_file_size = os.path.getsize(file_path)
|
| 281 |
+
current_mod_time = os.path.getmtime(file_path)
|
| 282 |
+
|
| 283 |
+
# Check if the file has been processed before using the cached state
|
| 284 |
+
if (last_file_info and
|
| 285 |
+
last_file_info.get('file_path') == file_path and
|
| 286 |
+
last_file_info.get('file_size') == current_file_size and
|
| 287 |
+
last_file_info.get('mod_time') == current_mod_time):
|
| 288 |
+
|
| 289 |
+
pts_npy_path = last_file_info.get('pts_npy_path')
|
| 290 |
+
cols_npy_path = last_file_info.get('cols_npy_path')
|
| 291 |
+
|
| 292 |
+
if os.path.exists(pts_npy_path) and os.path.exists(cols_npy_path):
|
| 293 |
+
print("Detected same file. Skipping conversion.")
|
| 294 |
+
return (f"File reloaded instantly from cache. Click 'Start Analysis' to continue.",
|
| 295 |
+
gr.update(interactive=True),
|
| 296 |
+
gr.update(visible=False),
|
| 297 |
+
[pts_npy_path, cols_npy_path],
|
| 298 |
+
last_file_info)
|
| 299 |
+
|
| 300 |
+
# If new file or a different version, proceed with conversion
|
| 301 |
temp_root = os.path.join(tempfile.gettempdir(), f"temp_pts_{uuid.uuid4().hex}")
|
| 302 |
os.makedirs(temp_root, exist_ok=True)
|
| 303 |
|
| 304 |
pts_npy_path = os.path.join(temp_root, "cloud_points.npy")
|
| 305 |
cols_npy_path = os.path.join(temp_root, "cloud_colors.npy")
|
| 306 |
|
| 307 |
+
pts_to_npy(file_path, pts_npy_path, cols_npy_path)
|
| 308 |
+
|
| 309 |
+
# Store info for future re-uploads in the state dictionary
|
| 310 |
+
new_file_info = {
|
| 311 |
+
'file_path': file_path,
|
| 312 |
+
'file_size': current_file_size,
|
| 313 |
+
'mod_time': current_mod_time,
|
| 314 |
+
'pts_npy_path': pts_npy_path,
|
| 315 |
+
'cols_npy_path': cols_npy_path
|
| 316 |
+
}
|
| 317 |
|
| 318 |
return (f"File loaded successfully. Click 'Start Analysis' to continue.",
|
| 319 |
gr.update(interactive=True),
|
| 320 |
gr.update(visible=False),
|
| 321 |
+
[pts_npy_path, cols_npy_path],
|
| 322 |
+
new_file_info)
|
| 323 |
|
| 324 |
def start_analysis(file_state, progress=gr.Progress(track_tqdm=True)):
|
| 325 |
"""
|
|
|
|
| 512 |
gr.Markdown("Upload a `.pts` file to analyze the point cloud data.")
|
| 513 |
|
| 514 |
file_state = gr.State(value=[None, None])
|
| 515 |
+
last_file_info_state = gr.State(value={})
|
| 516 |
|
| 517 |
with gr.Row():
|
| 518 |
file_input = gr.File(label="Upload .pts (X Y Z R G B)")
|
|
|
|
| 525 |
|
| 526 |
file_input.upload(
|
| 527 |
fn=handle_file_upload,
|
| 528 |
+
inputs=[file_input, last_file_info_state],
|
| 529 |
+
outputs=[output_text, analysis_button, download_button, file_state, last_file_info_state],
|
| 530 |
queue=False
|
| 531 |
)
|
| 532 |
|