VEHwang's picture
Upload CoCogni4D-Bench (questions, media, scripts, render records, code) (part 6)
5afd2f7 verified
Raw History Blame Contribute Delete
3.4 kB
"""Segment facts for review: what the prover judges from (gen/facts.py, prover-facts/1), one
recorded view at a time, as raw curves on a coarse clock -- projected height (full projected height / image height, 0 out of
view) and occlusion (0..1, out of frame counts as occluded) of every entity and doorway, room-boundary metres seen, and the
door and room crossings. No visibility verdict here: the thresholds are domain.visibility's (returned alongside for a
reference line), the console draws what it reads. Each coarse cell keeps the clearest moment in it (height max, occlusion
min), so a doorway seen for one frame is not averaged away."""
import json
from pathlib import Path
import numpy as np
from embodied_pipeline.domain import visibility
from .errors import require
from .segment_import import verify_import
def _cells(x, n, step, how):
"""[..., frames] -> [..., cells] lists, each cell `how` over `step` frames (the last cell may be short)."""
x = np.asarray(x[..., :n], dtype=float)
edges = range(0, n, step)
return np.stack([how(x[..., s:s+step], axis=-1) for s in edges], axis=-1).round(3).tolist()
def facts_view(directory, view=None, hz=2.):
manifest = verify_import(directory)
seg = Path(manifest['inputs']['case.json']['path']).parent
require((seg/'facts.json').is_file() and (seg/'facts.npz').is_file(), 'FACTS_MISSING', '本段没有 facts.json/facts.npz(生成器没写出)')
f = json.loads((seg/'facts.json').read_text())
videos = [v['video'] for v in f['views']]
require(view is None or view in videos, 'FACTS_VIEW', '本段没有这一路录像:%s' % view)
i = videos.index(view or videos[0]); meta = f['views'][i]
require(hz > 0, 'FACTS_HZ', 'hz 须为正数')
n, step = int(meta['frame_count']), max(1, int(round(meta['fps']/hz)))
with np.load(seg/'facts.npz') as z:
a = {k: z[k] for k in z.files}
curves = lambda h, o, ids: {k: dict(height_frac=hh, occlusion=oo) for k, hh, oo in
zip(ids, _cells(h[i], n, step, np.max), _cells(o[i], n, step, np.min))}
actors, rooms, doors = f['actors'], f['rooms'], f['doorways']
events = dict(
door=[dict(t_s=round(float(t), 3), actor=actors[int(w)], door=doors[int(d)], side=int(s))
for t, w, d, s in zip(a['door_event_t'], a['door_event_actor'], a['door_event_door'], a['door_event_side'])],
room=[dict(t_s=round(float(t), 3), actor=actors[int(w)], room=rooms[int(r)], kind=f['conventions']['room_event_kind'].get(str(int(k)), int(k)))
for t, w, r, k in zip(a['room_event_t'], a['room_event_actor'], a['room_event_room'], a['room_event_kind'])])
return dict(schema='segment-facts/1', script_id=f['script_id'], facts_schema=f['schema'], videos=videos, view=meta,
hz=meta['fps']/step, t_s=_cells(a['frame_time_s'][i], n, step, np.min),
entities=curves(a['obs_projected_height'], a['obs_occlusion'], [e['id'] for e in f['entities']]),
doorways=curves(a['obs_door_projected_height'], a['obs_door_occlusion'], doors),
room_boundary_visible_m={r: c for r, c in zip(rooms, _cells(a['obs_room_boundary_visible_m'][i], n, step, np.max))},
events=events, conventions=f['conventions'],
thresholds=dict(zip(('min_projected_height', 'max_occlusion_fraction'), visibility.thresholds())))