Spaces:
Runtime error
Runtime error
add padding
Browse files19 unmodified lines20212223242522 unmodified lines48495051525311 unmodified lines656667686970717238 unmodified lines11111211311411511619 unmodified linesfrom rasterio.plot import showimport torchimport gradio as grfrom detectron2.engine import DefaultPredictorfrom detectree2.preprocessing.tiling import tile_data22 unmodified linesexcept Exception:pass# Tiling defaultsBUFFER, TILE_W, TILE_H = 30, 40, 4011 unmodified lineswith 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)weights = WEIGHTS.get(environment) or WEIGHTS[DEFAULT_ENV]cfg = setup_cfg(update_model=weights)38 unmodified linescleaned.simplify(0.4))# save vectorgpkg_path = os.path.join(workdir, "crowns_out.gpkg")cleaned.to_file(gpkg_path, driver="GPKG")19 unmodified lines20212223242526272822 unmodified lines5152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312411 unmodified lines13613713813914014114214314438 unmodified lines18318418518618718818919019119219 unmodified linesfrom rasterio.plot import showimport torchimport gradio as grimport mathimport numpy as npfrom shapely.geometry import boxfrom detectron2.engine import DefaultPredictorfrom detectree2.preprocessing.tiling import tile_data22 unmodified linesexcept Exception:passdef 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 pixelsbuf_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 sizebase_w = w + 2 * buf_px_xbase_h = h + 2 * buf_px_ytarget_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_wpad_bottom = target_h - base_hnew_w = base_w + pad_rightnew_h = base_h + pad_bottomprofile = src.profile.copy()# Choose/ensure a nodata valuenodata = profile.get("nodata", None)if nodata is None:nodata = 0profile["nodata"] = nodataprofile.update(width=new_w,height=new_h,# shift origin up/left by the buffer so original data lands insettransform=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 centerdata = 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 lateroriginal_bounds = src.boundsreturn out_path, original_bounds# Tiling defaultsBUFFER, TILE_W, TILE_H = 30, 40, 4011 unmodified lineswith 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."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 linescleaned.simplify(0.4))clip_geom = box(*orig_bounds)if not cleaned.empty:cleaned = cleaned.clip(clip_geom)# save vectorgpkg_path = os.path.join(workdir, "crowns_out.gpkg")cleaned.to_file(gpkg_path, driver="GPKG")
app.py+78-2