import os import datasets import rasterio import tqdm import datetime class SSL4EOEUForest(datasets.GeneratorBasedBuilder): def _info(self): return datasets.DatasetInfo( description="SSL4EO-EU Forest dataset with four-season Sentinel-2 timestamps and bounding box metadata extracted from image folders.", features=datasets.Features({ "image_paths": datasets.Sequence(datasets.Value("string")), "mask_path": datasets.Value("string"), "start_timestamps": datasets.Sequence(datasets.Value("timestamp[ms]")), "end_timestamps": datasets.Sequence(datasets.Value("timestamp[ms]")), "sentinel_tile_ids": datasets.Sequence(datasets.Value("string")), "bboxes": datasets.Sequence({ "minx": datasets.Value("float32"), "maxx": datasets.Value("float32"), "miny": datasets.Value("float32"), "maxy": datasets.Value("float32"), }) }), citation="""@misc{ssl4eo_eu_forest, author = {Nassim Ait Ali Braham and Conrad M Albrecht}, title = {SSL4EO-EU Forest Dataset}, year = {2025}, howpublished = {https://huggingface.co/datasets/dm4eo/ssl4eo-eu-forest}, note = {This work was carried under the EvoLand project, cf. https://www.evo-land.eu . This project has received funding from the European Union's Horizon Europe research and innovation programme under grant agreement No. 101082130.} }""", homepage="https://www.evo-land.eu", license="CC-BY-4.0", ) def _split_generators(self, dl_manager): use_local = os.environ.get("HF_DATASET_LOCAL", "false").lower() == "true" if use_local: root = os.path.abspath(".") else: root = dl_manager.download_and_extract(".") images_dir = os.path.join(root, "images") masks_dir = os.path.join(root, "masks") return [ datasets.SplitGenerator(name=datasets.Split("all"), gen_kwargs={ "images_dir": images_dir, "masks_dir": masks_dir }) ] def _generate_examples(self, images_dir, masks_dir): sample_id_list = sorted(os.listdir(images_dir)) key = 0 for sample_id in sample_id_list: sample_path = os.path.join(images_dir, sample_id) if not os.path.isdir(sample_path): continue timestamp_dirs = sorted(os.listdir(sample_path)) mask_path = os.path.join(masks_dir, sample_id, "mask.tif") if not os.path.exists(mask_path): continue image_paths = [] start_timestamps = [] end_timestamps = [] tile_ids = [] bboxes = [] for ts in timestamp_dirs: parts = ts.split("_") if len(parts) != 3: continue try: start_ts = datetime.datetime.strptime(parts[0], "%Y%m%dT%H%M%S") end_ts = datetime.datetime.strptime(parts[1], "%Y%m%dT%H%M%S") except ValueError: continue tile_id = parts[2] image_path = os.path.join(sample_path, ts, "all_bands.tif") if not os.path.exists(image_path): continue try: with rasterio.open(image_path) as src: bounds = src.bounds except Exception: continue image_paths.append(image_path) start_timestamps.append(start_ts) end_timestamps.append(end_ts) tile_ids.append(tile_id) bboxes.append({ "minx": bounds.left, "maxx": bounds.right, "miny": bounds.bottom, "maxy": bounds.top }) if image_paths: yield key, { "image_paths": image_paths, "mask_path": mask_path, "start_timestamps": start_timestamps, "end_timestamps": end_timestamps, "sentinel_tile_ids": tile_ids, "bboxes": bboxes } key += 1