Spaces:
Runtime error
Runtime error
add padding
Browse files19 unmodified lines20212223242526272822 unmodified lines5152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312411 unmodified lines136137138686913914014114214314438 unmodified lines18318418518618718818919019119219 unmodified linesfrom rasterio.plot import showimport torchimport gradio as grimport mathimport numpy as npfrom shapely.geometry import box
from detectron2.engine import DefaultPredictorfrom detectree2.preprocessing.tiling import tile_data22 unmodified linesexcept Exception: pass
def pad_for_tiling(src_path, buffer_m, tile_w_m, tile_h_m): """ Create a padded copy of src_path so that: - We add 'buffer_m' on all sides (in pixels). - The final width/height are exact multiples of the tile size (in pixels). Returns (out_path, original_bounds) """ out_path = src_path.replace(".tif", "_padded.tif").replace(".tiff", "_padded.tiff") with rasterio.open(src_path) as src: if src.crs is None: raise ValueError("Input has no CRS; cannot compute meter-to-pixel sizes reliably.")
# Pixel sizes (assumes no rotation/skew) res_x = abs(src.transform.a) res_y = abs(src.transform.e)
# Convert meters to pixels buf_px_x = int(math.ceil(buffer_m / res_x)) buf_px_y = int(math.ceil(buffer_m / res_y)) tile_px_w = int(math.ceil(tile_w_m / res_x)) tile_px_h = int(math.ceil(tile_h_m / res_y))
w, h = src.width, src.height
# We will add buffer on all sides, then extend right/bottom to multiples of tile size base_w = w + 2 * buf_px_x base_h = h + 2 * buf_px_y
target_w = int(math.ceil(base_w / tile_px_w) * tile_px_w) target_h = int(math.ceil(base_h / tile_px_h) * tile_px_h)
pad_right = target_w - base_w pad_bottom = target_h - base_h
new_w = base_w + pad_right new_h = base_h + pad_bottom
profile = src.profile.copy() # Choose/ensure a nodata value nodata = profile.get("nodata", None) if nodata is None: nodata = 0 profile["nodata"] = nodata
profile.update( width=new_w, height=new_h, # shift origin up/left by the buffer so original data lands inset transform=Affine( src.transform.a, src.transform.b, src.transform.c - buf_px_x * res_x, src.transform.d, src.transform.e, src.transform.f - buf_px_y * res_y ) )
# Allocate padded raster and paste original into the center data = np.full((src.count, new_h, new_w), nodata, dtype=src.dtypes[0]) data[:, buf_px_y:buf_px_y + h, buf_px_x:buf_px_x + w] = src.read()
with rasterio.open(out_path, "w", **profile) as dst: dst.write(data)
# Save original bounds to clip later original_bounds = src.bounds
return out_path, original_bounds
# Tiling defaultsBUFFER, TILE_W, TILE_H = 30, 40, 40
11 unmodified lines with rasterio.open(infile) as src: if src.crs is None or src.transform == Affine.identity(): return None, None, None, "Error: input GeoTIFF has no CRS/transform."
tile_data(infile, tiles_dir, BUFFER, TILE_W, TILE_H) padded_file, orig_bounds = pad_for_tiling(infile, BUFFER, TILE_W, TILE_H) tile_data(padded_file, tiles_dir, BUFFER, TILE_W, TILE_H)
weights = WEIGHTS.get(environment) or WEIGHTS[DEFAULT_ENV] cfg = setup_cfg(update_model=weights)38 unmodified lines cleaned.simplify(0.4) )
clip_geom = box(*orig_bounds) if not cleaned.empty: cleaned = cleaned.clip(clip_geom)
# save vector gpkg_path = os.path.join(workdir, "crowns_out.gpkg") cleaned.to_file(gpkg_path, driver="GPKG")app.py+78-2