File size: 2,783 Bytes
65fd232
7229aa2
 
65fd232
7229aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
65fd232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7229aa2
 
 
 
 
65fd232
7229aa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65fd232
7229aa2
 
 
65fd232
 
7229aa2
 
65fd232
7229aa2
65fd232
 
 
7229aa2
 
 
 
 
 
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""Download tracks from the shared dataset repo, verify manifests and extract images."""
import os
import json
import tarfile
import hashlib
import argparse

from huggingface_hub import snapshot_download


def sha256(path):
    h = hashlib.sha256()
    with open(path, "rb") as f:
        for chunk in iter(lambda: f.read(1 << 20), b""):
            h.update(chunk)
    return h.hexdigest()


def extract_if_needed(track_dir, manifest):
    layout = manifest.get("image_layout", "images/<image_filename>")
    if layout == "images/<image_filename>":
        return
    archive = os.path.join(track_dir, layout)
    if not os.path.exists(archive):
        return
    images_dir = os.path.join(track_dir, "images")
    os.makedirs(images_dir, exist_ok=True)
    with tarfile.open(archive) as tar:
        for member in tar.getmembers():
            if member.isfile():
                member.name = os.path.basename(member.name)
                tar.extract(member, images_dir)


def main():
    ap = argparse.ArgumentParser(description="Pull all tracks from the HF dataset repo.")
    ap.add_argument("--repo", default="comradev7/Scenic-Font-Analysis")
    ap.add_argument("--out", default="data")
    ap.add_argument("--tracks", default="", help="Comma list (a,b,c). Empty = all.")
    ap.add_argument("--no-extract", action="store_true", help="Skip archive extraction.")
    args = ap.parse_args()

    patterns = None
    if args.tracks.strip():
        patterns = ["README.md", "schema.json", ".gitattributes"]
        for t in args.tracks.split(","):
            patterns.append(f"track_{t.strip().lower()}/**")

    root = snapshot_download(
        repo_id=args.repo, repo_type="dataset",
        local_dir=args.out, allow_patterns=patterns,
    )
    print(f"Downloaded to {root}")

    for entry in sorted(os.listdir(root)):
        track_dir = os.path.join(root, entry)
        manifest_path = os.path.join(track_dir, "manifest.json")
        if not (entry.startswith("track_") and os.path.isdir(track_dir)):
            continue
        gt = os.path.join(track_dir, "ground_truth.csv")
        if os.path.exists(manifest_path):
            meta = json.load(open(manifest_path, encoding="utf-8"))
            ok = os.path.exists(gt) and sha256(gt) == meta.get("ground_truth_sha256")
            print(f"{entry}: images={meta.get('image_count')} "
                  f"layout={meta.get('image_layout')} "
                  f"styles={meta.get('style_unified_counts')} csv_ok={ok}")
            if not args.no_extract:
                extract_if_needed(track_dir, meta)
                print(f"  extracted -> {os.path.join(track_dir, 'images')}")
        else:
            print(f"{entry}: (no manifest) csv_present={os.path.exists(gt)}")


if __name__ == "__main__":
    main()