Datasets:
Download code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_cut_clips.py from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 3.68 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_cut_clips.py
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_cut_clips.py
-
curl -L -o test_cut_clips.py https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/v0.2-0c2690ae/tools/evaluation/test_cut_clips.py
3.68 kB
| import json | |
| import shutil | |
| import subprocess | |
| import tempfile | |
| import unittest | |
| from pathlib import Path | |
| import cut_clips | |
| import np_export | |
| HAVE_FFMPEG = bool(shutil.which('ffmpeg') and shutil.which('ffprobe')) | |
| def row(cid, views): | |
| return {'candidate_id': cid, 'decision': {'result': 'PASS'}, 'public': {'version_id': cid.split('#')[0]}, | |
| 'presentation': {'videos': {k: v[0] for k, v in views.items()}}, | |
| 'recording_views': {k: {'observer': o, 'start_frame': a, 'end_frame_exclusive': b} | |
| for k, (o, a, b) in views.items()}} | |
| class CutClips(unittest.TestCase): | |
| def test_cuts_only_cropped_views_and_export_finds_them(self): | |
| # 跨路题的 recording_views:裁剪的那路切成逐帧剪辑并写索引;整条录像不切;重跑跳过已切的 | |
| with tempfile.TemporaryDirectory() as tmp: | |
| root = Path(tmp) | |
| videos = root / 'run' | |
| src = videos / 'r' / 'seg1' / 'r/observation/videos' | |
| src.mkdir(parents=True) | |
| for obs in ('observer-000', 'observer-001'): | |
| subprocess.run(['ffmpeg', '-nostdin', '-v', 'error', '-f', 'lavfi', '-i', 'testsrc=size=64x48:rate=20', | |
| '-frames:v', '60', '-pix_fmt', 'yuv420p', str(src / f'{obs}.mp4')], check=True) | |
| qdir = root / 'vout' / 'seg1' | |
| qdir.mkdir(parents=True) | |
| q = qdir / 'groups.jsonl' | |
| q.write_text('\n'.join(json.dumps(r) for r in [ | |
| row('v.a/multi_ego.x#g1-00', {'A': ('observer-000', 10, 40), 'B': ('observer-001', 0, 60)}), | |
| row('v.a/multi_ego.x#g1-01', {'A': ('observer-000', 10, 40)}), | |
| ]) + '\n') | |
| out = root / 'clips-src' | |
| self.assertEqual(cut_clips.main(['--questions', str(q), '--videos', str(videos), '--out', str(out), | |
| '--jobs', '2']), 0) | |
| index = json.loads((out / 'clips/index.json').read_text()) | |
| self.assertEqual(list(index['clips']), ['clips/seg1/observer-000__f10-40.mp4']) | |
| entry = index['clips']['clips/seg1/observer-000__f10-40.mp4'] | |
| self.assertEqual((entry['frames'], entry['script_id'], entry['observer']), (30, 'seg1', 'observer-000')) | |
| self.assertEqual(cut_clips.frames_of(out / 'clips/seg1/observer-000__f10-40.mp4'), 30) | |
| loaded = np_export.load_clips(out / 'clips/index.json') | |
| self.assertIn(('seg1', 'observer-000', 10, 40), loaded) | |
| mtime = (out / 'clips/seg1/observer-000__f10-40.mp4').stat().st_mtime_ns | |
| self.assertEqual(cut_clips.main(['--questions', str(q), '--videos', str(videos), '--out', str(out)]), 0) | |
| self.assertEqual((out / 'clips/seg1/observer-000__f10-40.mp4').stat().st_mtime_ns, mtime) | |
| # 来源录像重渲(内容变、路径不变):旧剪辑不复用,按新录像重切 | |
| subprocess.run(['ffmpeg', '-nostdin', '-v', 'error', '-y', '-f', 'lavfi', '-i', 'color=red:size=64x48:rate=20', | |
| '-frames:v', '60', '-pix_fmt', 'yuv420p', str(src / 'observer-000.mp4')], check=True) | |
| self.assertEqual(cut_clips.main(['--questions', str(q), '--videos', str(videos), '--out', str(out)]), 0) | |
| recut = json.loads((out / 'clips/index.json').read_text())['clips']['clips/seg1/observer-000__f10-40.mp4'] | |
| self.assertNotEqual(recut['sha256'], entry['sha256']) | |
| self.assertEqual(recut['source_stat'], cut_clips.source_stat(src / 'observer-000.mp4')) | |
| if __name__ == '__main__': | |
| unittest.main() | |