File size: 1,758 Bytes
ae419ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
from pathlib import Path

from common import ROOT, load_config
from dynafall.data import load_pickle, save_pickle, split_video_records
from dynafall.features import make_clips, normalize_pose


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("--dataset", required=True)
    ap.add_argument("--config", default="configs/default.yaml")
    ap.add_argument("--seed", type=int, default=None)
    ap.add_argument("--group-key", choices=["video", "scenario"], default="video")
    ap.add_argument("--output-name", default=None)
    args = ap.parse_args()
    cfg = load_config(args.config)
    seed = args.seed if args.seed is not None else cfg["seed"]
    records = load_pickle(ROOT / "data/poses" / f"{args.dataset}_keypoints.pkl")
    splits = split_video_records(records, cfg["splits"], seed, group_key=args.group_key)
    out_dir = ROOT / "data/processed" / (args.output_name or args.dataset)
    buckets = {k: [] for k in ["train", "val", "test"]}
    for rec in records:
        split = next(k for k, ids in splits.items() if rec["video_id"] in ids)
        norm = normalize_pose(rec["keypoints"])
        for i, clip in enumerate(make_clips(norm, cfg["clip_len"], cfg["stride"])):
            buckets[split].append({"video_id": rec["video_id"], "clip_id": i, "label": int(rec["label"]), "joint": clip})
    for split, rows in buckets.items():
        save_pickle(rows, out_dir / f"{split}.pkl")
        print(f"{split}: {len(rows)} clips")
    save_pickle(
        {"seed": seed, "group_key": args.group_key, "splits": {k: sorted(v) for k, v in splits.items()}},
        out_dir / "video_splits.pkl",
    )
    print(f"Wrote {out_dir}")


if __name__ == "__main__":
    main()