Skip to main content

ml_dl.polygons_to_yolo_tiles

ml_dl.polygons_to_yolo_tiles(
    scene_path,
    aoi_bbox_ll,
    polygons_utm,
    tile_px,
    image_dir,
    label_dir,
    stride=None,
    pixel_size_m=1.0,
    tile_basename_prefix='tile',
    class_id=0,
    min_box_px=6,
    rgb_band_indices=(1, 2, 3),
)

Cut tile_px x tile_px tiles from the AOI window of scene_path, write each as a PNG into image_dir, and write the matching YOLO-format label TXT into label_dir based on intersections with polygons_utm (a GeoDataFrame in the raster’s CRS).

The output layout follows the Ultralytics convention: image_dir/<basename>.png and label_dir/<basename>.txt with matching basenames per tile. Empty label files are written for tiles that contain no polygons – they still belong in the dataset as “negative” examples.

Parameters

Name Type Description Default
scene_path path - like Multi-band georeferenced raster (NAIP, PlanetScope, drone ortho, …). The first three bands of rgb_band_indices are written as the PNG image; alpha channels and NIR are ignored. required
aoi_bbox_ll 4-tuple of float (lon_min, lat_min, lon_max, lat_max) in WGS84. The function crops the raster to this bbox before tiling. required
polygons_utm geopandas.GeoDataFrame Ground-truth polygons in the raster’s CRS (so the intersection test is exact). Caller is responsible for any prior projection. required
tile_px int Tile size in pixels. 512 is a common choice for YOLO. required
image_dir path - like Output directories. Created if missing. required
label_dir path - like Output directories. Created if missing. required
stride int Pixel stride between tile origins. Defaults to tile_px (no overlap). Pass a smaller value (e.g. tile_px // 2) for the training split to get sliding-window data augmentation. None
pixel_size_m float Raster’s ground sampling distance in metres per pixel. Used only to convert between metre-space polygon bounds and pixel-space bounding boxes. 1.0
tile_basename_prefix str Prefix for every tile’s filename. Useful when concatenating multiple scenes’ tiles into one split (e.g. multi-city training). 'tile'
class_id int YOLO class index written into every label row. The notebook uses a single class (0 = “building”). 0
min_box_px int Drop boxes whose width OR height is below this many pixels. YOLO’s small-object detection floor is ~16 px reliable / ~6 px hard, so min_box_px=6 is a permissive default. 6
rgb_band_indices 3-tuple of int Which bands of the raster to write as R, G, B. Defaults to (1, 2, 3) which is the convention used by both NAIP and the sentinel_pipeline Sentinel-2 RGB stacks. (1, 2, 3)

Returns

Name Type Description
list of str Basenames written (no extension). Useful for downstream tracking.