File size: 2,431 Bytes
80c695c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Draw the instance masks of one question's evidence objects on its photorealistic clip.

  python code/examples/show_masks.py --data /path/to/Clinical3D-Bench --id 3010d94944f9ca88 --frame 30 --out vis.png

masks/video/<clip>.npz holds object_id (60, 448, 768) on the videos/ raster, with source[i] in {src, warp, none}:
src = the frame is on the render's camera path and the render mask is used; warp = the render's instances
re-projected to the frame's estimated pose; none = no trustworthy mask (valid[i] False, all zeros).
masks/render/<clip>.npz holds object_id and part_id for videos_render/ (exact). objects/<clip>.json maps ids to
entities and categories; a question's evidence lists the entities (object or part keys) it depends on.
"""
import argparse
import json
import subprocess
from pathlib import Path

import numpy as np
from PIL import Image


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('--data', type=Path, required=True)
    ap.add_argument('--id', required=True)
    ap.add_argument('--frame', type=int, default=30)
    ap.add_argument('--out', type=Path, default=Path('vis.png'))
    a = ap.parse_args()
    q = next(r for r in map(json.loads, (a.data / 'questions.jsonl').read_text().splitlines()) if r['id'] == a.id)
    print(q['type'], '|', q['question'], '| answer', q['answer_letter'])
    objs = json.loads((a.data / 'objects' / f"{q['clip']}.json").read_text())
    ids = {o['entity']: o['object_id'] for o in objs['objects']}
    m = np.load(a.data / 'masks/video' / f"{q['clip']}.npz")
    i = a.frame
    print('mask source of frame', i, ':', m['source'][i])
    raw = subprocess.run(['ffmpeg', '-loglevel', 'error', '-i', str(a.data / q['video']), '-vf', f'select=eq(n\\,{i})',
                          '-vsync', '0', '-f', 'rawvideo', '-pix_fmt', 'rgb24', '-'], capture_output=True, check=True).stdout
    im = np.frombuffer(raw, np.uint8).reshape(448, 768, 3).copy()
    colors = [(255, 60, 60), (60, 200, 255), (255, 200, 0), (120, 255, 120)]
    for k, e in enumerate(q['evidence']):
        oid = ids.get(e['key'].split('/')[0])
        if oid is None:
            continue
        sel = m['object_id'][i] == oid
        im[sel] = (0.5 * im[sel] + 0.5 * np.array(colors[k % 4])).astype(np.uint8)
        print(e['key'], 'object_id', oid, 'pixels', int(sel.sum()))
    Image.fromarray(im).save(a.out)
    print('saved', a.out)


if __name__ == '__main__':
    main()