text
stringlengths
0
1.47k
tmp = _rebuild_dir(save_dir, object_type)
shutil.rmtree(tmp, ignore_errors=True)
try:
obj_cond, n_clips, n_frames, n_joints, filtered = process_object(
object_type, **dict(task, save_dir=tmp))
made = [(sub, name) for sub in ('motions', 'videos', 'tpose')
for name in (sorted(os.listdir(pjoin(tmp, sub)))
if os.path.isdir(pjoin(tmp, sub)) else [])]
videos = {name for sub, name in made if sub == 'videos'}
missing = [name[:-4] for sub, name in made
if sub == 'motions' and name[:-4] + '.mp4' not in videos]
if missing:
raise RuntimeError(f'{len(missing)} preview(s) not rendered, e.g. {missing[0]}')
except Exception as e: # noqa: BLE001 — nothing of the cached object was touched
shutil.rmtree(tmp, ignore_errors=True)
logger.warning(f'[{object_type}] previews could not be rebuilt '
f'({type(e).__name__}: {e}); kept the cached clips')
return 'kept', None
result = {'cond': obj_cond, 'n_clips': n_clips, 'n_frames': n_frames,
'n_joints': n_joints, 'filtered': filtered}
old_dir = pjoin(tmp, 'previous')
moved_out, moved_in = [], []
try:
if os.path.isfile(part_path):
os.remove(part_path)
for sub, name in _object_files(save_dir, object_type, clip_prefix):
os.makedirs(pjoin(old_dir, sub), exist_ok=True)
os.replace(pjoin(save_dir, sub, name), pjoin(old_dir, sub, name))
moved_out.append((sub, name))
for sub, name in made:
os.makedirs(pjoin(save_dir, sub), exist_ok=True)
os.replace(pjoin(tmp, sub, name), pjoin(save_dir, sub, name))
moved_in.append((sub, name))
atomic_np_save(part_path, {**payload, 'result': result, 'vis': vis})
except Exception as e: # noqa: BLE001 — undo the swap
try:
for sub, name in moved_in:
os.remove(pjoin(save_dir, sub, name))
for sub, name in moved_out:
os.replace(pjoin(old_dir, sub, name), pjoin(save_dir, sub, name))
atomic_np_save(part_path, payload)
except Exception as undo: # noqa: BLE001 — leave a miss behind
if os.path.isfile(part_path):
os.remove(part_path)
logger.error(f'[{object_type}] previews could not be swapped in '
f'({type(e).__name__}: {e}) nor the swap undone '
f'({type(undo).__name__}: {undo}); the object will be re-processed '
f'(its earlier files are in {old_dir} until then)')
return 'lost', f'swap failed ({type(e).__name__}: {e}), undo failed ({undo})'
shutil.rmtree(tmp, ignore_errors=True)
logger.warning(f'[{object_type}] previews could not be swapped in '
f'({type(e).__name__}: {e}); restored the cached clips')
return 'kept', None
shutil.rmtree(tmp, ignore_errors=True)
return 'rebuilt', result
def _failed(object_type, save_dir, error):
"""The result of an object that failed, its error appended to
``extract_errors.log``."""
try:
os.makedirs(save_dir, exist_ok=True)
with open(_error_log_path(save_dir), 'a') as log_file:
log_file.write(f'{object_type}\t{error}\n')
except OSError as log_err:
logger.warning(f'[{object_type}] could not write error log: {log_err}')
return {
'cond': None, 'n_clips': 0, 'n_frames': 0, 'n_joints': 0,
'filtered': [{'name': f'{object_type} (all clips)',
'reason': f'processing failed: {error}'}],
'error': error,
}
def process_object_task(task):
"""Process one object type, caching its result for resume.
``task`` bundles every :func:`process_object` argument plus the part
path. The cached result is reused only when it was produced with the same
clip / threshold / topology settings (see ``CACHE_PARAM_KEYS``).
With ``save_vis``, a cached object whose previews are not current (see
:func:`_previews_current`) is rebuilt with :func:`_rebuild_with_previews`;
if that fails, the cached result is used without them, unless its files
could not be restored, which fails the object.
Failures are contained here: the object is reported as failed (and its
error appended to ``extract_errors.log``) instead of propagating, which
with ``mp.Pool`` would kill the whole run. On a cache miss the old entry
is removed before the object's clips are pruned, and no entry is written
for a failed object, so it is retried on the next run whatever its
settings.
Returns ``(object_type, result_dict, from_cache)``.
"""
object_type = task.pop('object_type')
part_path = task.pop('part_path')
clip_prefix = task.pop('clip_prefix')
params = _cache_params(task)