File size: 1,999 Bytes
11aceb2
d93939e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from pathlib import Path
import argparse, hashlib, json
from datasets import load_dataset, Image as DatasetImage
from huggingface_hub import HfApi, hf_hub_download
REPO="fireviewer/flame_smoke_detection_box_not_precise"
def prepare(output, task="fire_smoke", revision=None):
    revision=revision or HfApi().dataset_info(REPO).sha
    out=Path(output); out.mkdir(parents=True,exist_ok=True)
    wanted={}; totals={}
    for split in ["train","validation","test"]:
        p=hf_hub_download(REPO,f"annotations/merged/{task}/{split}.json",repo_type="dataset",revision=revision)
        d=json.loads(Path(p).read_text())
        wanted[split]={im["sha256"]:im for im in d["images"]}
        folder=out/split; (folder/"images").mkdir(parents=True,exist_ok=True)
        for im in d["images"]: im["file_name"]="images/"+Path(im["file_name"]).name
        (folder/"_annotations.coco.json").write_text(json.dumps(d,ensure_ascii=False),encoding="utf-8")
        seen=set()
        for row in load_dataset(REPO,"corpus",split=split,revision=revision,streaming=True).cast_column("image",DatasetImage(decode=False)):
            h=row["sha256"]
            if h not in wanted[split]: continue
            image=row["image"]
            if not isinstance(image,dict): raise RuntimeError("Image decoding must be disabled; cast the image column to datasets.Image(decode=False).")
            blob=image["bytes"]; assert hashlib.sha256(blob).hexdigest()==h
            dest=folder/wanted[split][h]["file_name"]
            dest.write_bytes(blob); seen.add(h)
        assert seen==set(wanted[split]),(split,len(seen),len(wanted[split]))
        totals[split]=len(seen)
    return totals, {"revision":revision,"task":task}
if __name__=="__main__":
    p=argparse.ArgumentParser();p.add_argument("--output",required=True);p.add_argument("--task",choices=["fire_smoke","visible_flame","probable_fire"],default="fire_smoke");p.add_argument("--revision")
    a=p.parse_args(); print(prepare(a.output,a.task,a.revision))