File size: 2,603 Bytes
1bb0b8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Unpack the frame archives of this dataset and (optionally) write a parquet with absolute frame paths.

    python extract_frames.py              # unzip frames_zip/*.zip -> ./frames/<frame_dir>/000.jpg ... 031.jpg
    python extract_frames.py --abs        # also write train.abs.parquet (frame paths made absolute; needs pyarrow)
    python extract_frames.py --out /data  # extract somewhere else (frames end up in /data/frames)

`train.parquet` stores frame paths relative to the extraction root (`frames/<frame_dir>/NNN.jpg`).
"""
import argparse
import glob
import hashlib
import json
import os
import zipfile

HERE = os.path.dirname(os.path.abspath(__file__))


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


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--out", default=HERE, help="extraction root (default: the dataset folder)")
    ap.add_argument("--abs", action="store_true", help="write train.abs.parquet with absolute frame paths")
    ap.add_argument("--no-verify", action="store_true", help="skip the sha256 check of the zip files")
    args = ap.parse_args()
    out = os.path.abspath(args.out)
    manifest = json.load(open(os.path.join(HERE, "manifest.json")))
    want = {z["file"]: z for z in manifest["zips"]}
    zips = sorted(glob.glob(os.path.join(HERE, "frames_zip", "frames-*.zip")))
    assert len(zips) == len(want), f"expected {len(want)} zip files, found {len(zips)}"
    for zp in zips:
        rel = "frames_zip/" + os.path.basename(zp)
        if not args.no_verify:
            assert sha256(zp) == want[rel]["sha256"], f"sha256 mismatch: {rel} (re-download it)"
        with zipfile.ZipFile(zp) as z:
            z.extractall(out)
        print("extracted", rel, flush=True)
    n = len(glob.glob(os.path.join(out, "frames", "*", "*.jpg")))
    assert n == manifest["frame_files"], f"expected {manifest['frame_files']} frames, found {n}"
    print(f"ok: {n} frames in {os.path.join(out, 'frames')}")
    if args.abs:
        import pyarrow as pa
        import pyarrow.parquet as pq
        t = pq.read_table(os.path.join(HERE, "train.parquet"))
        rows = t.to_pylist()
        for r in rows:
            r["videos"][0]["video"] = [os.path.join(out, f) for f in r["videos"][0]["video"]]
        dst = os.path.join(out, "train.abs.parquet")
        pq.write_table(pa.Table.from_pylist(rows, schema=t.schema), dst)
        print("wrote", dst)


if __name__ == "__main__":
    main()