File size: 2,099 Bytes
314ecfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Validate/export reviewed PIM JSONL. Does not infer labels from object disappearance."""
import argparse
import json
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from dataloader.pim_annotations import parse_annotation


def main():
    p=argparse.ArgumentParser(description=__doc__)
    p.add_argument('jsonl',type=Path)
    p.add_argument('--output-root',type=Path)
    p.add_argument('--validate-only',action='store_true')
    args=p.parse_args()
    if not args.validate_only and args.output_root is None:
        p.error('--output-root is required when exporting')
    records=[]; seen=set()
    for line_no,line in enumerate(args.jsonl.read_text().splitlines(),1):
        if not line.strip(): continue
        row=json.loads(line); route=Path(row['route']); frame=row['frame']
        if route.is_absolute() or '..' in route.parts or not route.parts:
            raise ValueError(f'Line {line_no}: route must be relative to the data root')
        if type(frame) is not int or frame<0:
            raise ValueError(f'Line {line_no}: frame must be a nonnegative integer')
        key=(str(route),frame)
        if key in seen: raise ValueError(f'Line {line_no}: duplicate {key}')
        seen.add(key)
        payload={k:v for k,v in row.items() if k not in ('route','frame')}
        parse_annotation(payload)
        records.append((route,frame,payload))
    # Validate the whole input before writing any sidecars.
    if not args.validate_only:
        destinations=[args.output_root/route/'pim'/f'{frame:04d}.json'
                      for route,frame,_ in records]
        if any(path.exists() for path in destinations):
            raise FileExistsError('Export would replace existing labels; use a new output root')
        for path,(_,_,payload) in zip(destinations,records):
            path.parent.mkdir(parents=True,exist_ok=True)
            path.write_text(json.dumps(payload,ensure_ascii=False,indent=2)+'\n')
    print(f'Validated {len(records)} frames; exported={not args.validate_only}')


if __name__=='__main__': main()