Spaces:
Runtime error
Runtime error
Fix: save outputs to separate dir before cleaning workdir
Browse files126 unmodified lines1271281291301311321331341351361371383 unmodified lines142143144145146147148149150151152153154155156157158159160126 unmodified linescleaned.simplify(0.4))# save vectorgpkg_path = os.path.join(workdir, "crowns_out.gpkg")cleaned.to_file(gpkg_path, driver="GPKG")# save overlayoverlay_path = os.path.join(workdir, "crowns_out.png")with rasterio.open(infile) as src:fig, ax = plt.subplots(figsize=(10, 10))show(src, ax=ax)3 unmodified linesplt.savefig(overlay_path, dpi=220, bbox_inches="tight", pad_inches=0)plt.close(fig)stats = {"total_trees": int(len(cleaned))}return overlay_path, gpkg_path, stats, ""except Exception as e:return None, None, None, f"Error: {e}"finally:# Clean up temp files to avoid filling disktry:shutil.rmtree(workdir, ignore_errors=True)except Exception:passdef infer(file_path, environment, confidence):if file_path is None:return None, None, None, "Error: No file uploaded. Please upload a GeoTIFF (.tif) file first."126 unmodified lines1271281291301311321331341351361371381391401413 unmodified lines145146147148149150151152153154155156157158159160161126 unmodified linescleaned.simplify(0.4))# Save outputs to a Gradio-friendly temp dir (not cleaned up immediately)out_dir = tempfile.mkdtemp(prefix="dtr2_out_")# save vectorgpkg_path = os.path.join(out_dir, "crowns_out.gpkg")cleaned.to_file(gpkg_path, driver="GPKG")# save overlayoverlay_path = os.path.join(out_dir, "crowns_out.png")with rasterio.open(infile) as src:fig, ax = plt.subplots(figsize=(10, 10))show(src, ax=ax)3 unmodified linesplt.savefig(overlay_path, dpi=220, bbox_inches="tight", pad_inches=0)plt.close(fig)# Clean up the large working directory (tiles, predictions, etc.)shutil.rmtree(workdir, ignore_errors=True)stats = {"total_trees": int(len(cleaned))}return overlay_path, gpkg_path, stats, ""except Exception as e:# Clean up on error tooshutil.rmtree(workdir, ignore_errors=True)return None, None, None, f"Error: {e}"def infer(file_path, environment, confidence):if file_path is None:return None, None, None, "Error: No file uploaded. Please upload a GeoTIFF (.tif) file first."
app.py+10-9