File size: 1,474 Bytes
a863f79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import argparse
import json
from collections import defaultdict
from pathlib import Path

FAMILIES = ("left_right", "above_below", "front_behind", "on_not_on")


def family_of(scene_id):
    for family in FAMILIES:
        if scene_id.startswith(family):
            return family
    raise ValueError(f"无法从 scene_id 推断族: {scene_id}")


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--prompts", required=True)
    parser.add_argument("--out", required=True)
    parser.add_argument("--per-family", type=int, default=120)
    args = parser.parse_args()

    by_family = defaultdict(dict)
    with open(args.prompts, encoding="utf-8") as handle:
        for line in handle:
            row = json.loads(line)
            by_family[family_of(row["scene_id"])].setdefault(row["scene_id"], []).append(row)

    kept = []
    for family in FAMILIES:
        scene_ids = sorted(by_family[family])[-args.per_family:]
        if len(scene_ids) < args.per_family:
            raise RuntimeError(f"{family} 只有 {len(scene_ids)} 个场景,少于 {args.per_family}")
        for scene_id in scene_ids:
            kept.extend(by_family[family][scene_id])

    with open(args.out, "w", encoding="utf-8") as handle:
        for row in kept:
            handle.write(json.dumps(row, ensure_ascii=False) + "\n")
    print(json.dumps({"prompts": len(kept), "scenes": len({r['scene_id'] for r in kept})}))


if __name__ == "__main__":
    main()