| |
| """ |
| Downloads and extracts additional data from the Cityscapes dataset, moving the |
| required files to (existing) data directory. |
| """ |
|
|
| import tempfile |
| from tqdm import tqdm |
| import os |
| import re |
| from pathlib import Path |
| import shutil |
| import zipfile |
| import argparse |
| import pandas as pd |
|
|
| from cityscapesscripts.download import downloader |
|
|
| PACKAGE_TO_VARIABLE = { |
| "vehicle_sequence": "vehicle", |
| "timestamp_sequence": "timestamp", |
| "leftImg8bit_sequence_trainvaltest": "image", |
| "camera_trainvaltest": "camera", |
| } |
|
|
|
|
| def parse_args() -> argparse.Namespace: |
| parser = argparse.ArgumentParser( |
| description="Download and extract the Cityscapes dataset." |
| ) |
|
|
| parser.add_argument( |
| "downloads_dir", |
| type=Path, |
| help="Path to the directory where ZIP files are/will be stored (e.g., 'downloads').", |
| ) |
| parser.add_argument( |
| "data_dir", |
| type=Path, |
| help="Path to the directory where extracted files should be moved (e.g., 'data').", |
| ) |
| parser.add_argument( |
| "manifest_file", |
| type=Path, |
| help="Path to the manifest file (e.g., 'manifest.csv').", |
| ) |
| return parser.parse_args() |
|
|
|
|
| def read_manifest(manifest_file: str) -> pd.DataFrame: |
| """ |
| Read the manifest file and return a DataFrame. |
| """ |
| df = pd.read_csv(manifest_file, index_col="primary_key") |
| assert "split" in df.columns, "Missing 'split' column in manifest." |
| assert "sequence" in df.columns, "Missing 'sequence' column in manifest." |
| assert "frame" in df.columns, "Missing 'frame' column in manifest." |
| return df |
|
|
|
|
| def download_files(downloads_dir: str, zip_files: list[str]): |
| if not zip_files: |
| print("No files to download.") |
| return |
|
|
| session = downloader.login() |
|
|
| downloader.download_packages( |
| session=session, |
| package_names=zip_files, |
| destination_path=downloads_dir, |
| resume=True, |
| ) |
|
|
|
|
| def unzip_and_move( |
| pkg: str, |
| downloads_dir: Path, |
| data_dir: Path, |
| manifest: pd.DataFrame, |
| source_split: str, |
| ): |
| """ |
| Unzip, rename, and move the requested split files for a single package. |
| """ |
| zip_path = downloads_dir / f"{pkg}.zip" |
| pkg_type = PACKAGE_TO_VARIABLE[pkg] |
|
|
| re_name = re.compile(r"([^_]*_[^_]*_[^_]*)_.*") |
|
|
| if not zip_path.is_file(): |
| print(f"Warning: ZIP file not found => {zip_path}. Skipping...") |
| return |
|
|
| print(f"Processing: {zip_path}") |
|
|
| with tempfile.TemporaryDirectory() as td, zipfile.ZipFile(zip_path, "r") as zf: |
| names = zf.namelist() |
| for member in tqdm(names): |
| if f"/{source_split}/" not in member: |
| continue |
|
|
| |
| res_path = Path(zf.extract(member, td)) |
|
|
| if res_path.is_dir(): |
| continue |
|
|
| |
| primary_key = re_name.sub(r"\1", res_path.stem) |
| sample = manifest.loc[primary_key] |
|
|
| |
| new_path = ( |
| data_dir |
| / sample["split"] |
| / "{:06d}".format(sample["sequence"]) |
| / "{:06d}.{:s}{:s}".format(sample["frame"], pkg_type, res_path.suffix) |
| ) |
| new_path.parent.mkdir(parents=True, exist_ok=True) |
|
|
| shutil.move(res_path, new_path) |
|
|
|
|
| def main(): |
| args = parse_args() |
|
|
| |
| for dir in (args.downloads_dir, args.data_dir): |
| os.makedirs(dir, exist_ok=True) |
|
|
| |
| manifest = read_manifest(args.manifest_file) |
|
|
| |
| packages = list(PACKAGE_TO_VARIABLE.keys()) |
| zip_files = [f"{pkg}.zip" for pkg in packages] |
|
|
| |
| download_list = [] |
| for zf in zip_files: |
| zf_path = os.path.join(args.downloads_dir, zf) |
| if os.path.isfile(zf_path): |
| print(f"Already downloaded: {zf_path}") |
| else: |
| download_list.append(zf) |
|
|
| |
| download_files(args.downloads_dir, download_list) |
|
|
| |
| for pkg in packages: |
| unzip_and_move(pkg, args.downloads_dir, args.data_dir, manifest, "val") |
|
|
| print(f"Successfully processed: {packages}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|