Spaces:
Runtime error
Runtime error
padding cutoff issue
Browse files20 unmodified lines21222324252627282930313233 unmodified lines6667686970717220 unmodified linesimport torchimport gradio as grfrom detectron2.engine import DefaultPredictorfrom detectree2.preprocessing.tiling import tile_datafrom detectree2.models.outputs import project_to_geojson, stitch_crowns, clean_crownsfrom detectree2.models.predict import predict_on_datafrom detectree2.models.train import setup_cfg# configWEIGHT_DIR = os.getenv("DTR2_WEIGHT_DIR", "/data/weights")os.makedirs(WEIGHT_DIR, exist_ok=True)33 unmodified linesif 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)20 unmodified lines2122232425262728293031323334353637383940414243444546474849505152535455565758596061626333 unmodified lines97989910010110210310410520 unmodified linesimport torchimport gradio as grimport mathimport numpy as npfrom detectron2.engine import DefaultPredictorfrom detectree2.preprocessing.tiling import tile_datafrom detectree2.models.outputs import project_to_geojson, stitch_crowns, clean_crownsfrom detectree2.models.predict import predict_on_datafrom detectree2.models.train import setup_cfgdef pad_for_tiling(src_path, buffer_m, tile_w_m, tile_h_m):# pads so its dimensions are multiples of tile sizewith rasterio.open(src_path) as src:res_x, res_y = src.res # meters/pixel (positive values)tw_px = max(1, int(round(tile_w_m / res_x)))th_px = max(1, int(round(tile_h_m / abs(res_y))))new_w = math.ceil(src.width / tw_px) * tw_pxnew_h = math.ceil(src.height / th_px) * th_pxif new_w == src.width and new_h == src.height:return src_path # already aligneddata = src.read() # (bands, H, W)pad_w = new_w - src.widthpad_h = new_h - src.heightdata_padded = np.pad(data, ((0,0), (0,pad_h), (0,pad_w)), mode="edge")prof = src.profileprof.update(width=new_w, height=new_h)padded_path = src_path + ".padded.tif"with rasterio.open(padded_path, "w", **prof) as dst:dst.write(data_padded)return padded_path# configWEIGHT_DIR = os.getenv("DTR2_WEIGHT_DIR", "/data/weights")os.makedirs(WEIGHT_DIR, exist_ok=True)33 unmodified linesif src.crs is None or src.transform == Affine.identity():return None, None, None, "Error: input GeoTIFF has no CRS/transform."padded = pad_for_tiling(infile, BUFFER, TILE_W, TILE_H)tile_data(padded, tiles_dir, BUFFER, TILE_W, TILE_H)weights = WEIGHTS.get(environment) or WEIGHTS[DEFAULT_ENV]cfg = setup_cfg(update_model=weights)
app.py+34-1