syncnet_compute / syncnet_claude /batch_syncnet.py
xingzhaohu's picture
Add files using upload-large-folder tool
804301e verified
#!/usr/bin/python
#-*- coding: utf-8 -*-
# Example usage:
# python batch_syncnet.py --video_dir /share/zhaohu_workspace/video_gen-hunyuanvideo1.5_tai2v_training/outputs/test_results_step_010400 --output_file results_10400.json
# python batch_syncnet.py --video_dir /share/zhaohu_workspace/benchmarks/outputs_benchmark_4/latent_sync --output_file results_latentsync.json
import time, pdb, argparse, subprocess, os, glob, json
from SyncNetInstance import *
# ==================== LOAD PARAMS ====================
parser = argparse.ArgumentParser(description = "SyncNet Batch Processing");
parser.add_argument('--initial_model', type=str, default="data/syncnet_v2.model", help='Path to SyncNet model');
parser.add_argument('--batch_size', type=int, default=20, help='Batch size for processing');
parser.add_argument('--vshift', type=int, default=15, help='Video shift for evaluation');
parser.add_argument('--video_dir', type=str, required=True, help='Directory containing video files');
parser.add_argument('--tmp_dir', type=str, default="data/work/pytmp", help='Temporary directory');
parser.add_argument('--output_file', type=str, default="syncnet_scores.json", help='Output JSON file for results');
parser.add_argument('--video_ext', type=str, default="mp4,avi,mov,mkv", help='Video extensions to process (comma-separated)');
opt = parser.parse_args();
# ==================== LOAD MODEL ====================
s = SyncNetInstance();
s.loadParameters(opt.initial_model);
print("Model %s loaded." % opt.initial_model);
# ==================== FIND VIDEO FILES ====================
video_extensions = opt.video_ext.split(',')
video_files = []
for ext in video_extensions:
video_files.extend(glob.glob(os.path.join(opt.video_dir, f'*.{ext.strip()}')))
video_files.extend(glob.glob(os.path.join(opt.video_dir, f'*.{ext.strip().upper()}')))
video_files = sorted(list(set(video_files)))
print(f"Found {len(video_files)} video files to process.")
# ==================== PROCESS VIDEOS ====================
results = {}
for idx, videofile in enumerate(video_files):
video_name = os.path.basename(videofile)
print(f"\n[{idx+1}/{len(video_files)}] Processing: {video_name}")
# Set unique reference for each video to avoid conflicts
opt.reference = os.path.splitext(video_name)[0]
try:
offset, conf, dists = s.evaluate(opt, videofile=videofile)
results[video_name] = {
'offset': int(offset),
'confidence': float(conf),
'min_dist': float(dists.min()) if dists is not None else None,
'status': 'success'
}
print(f" -> Offset: {offset}, Confidence: {conf:.3f}")
except Exception as e:
results[video_name] = {
'offset': None,
'confidence': None,
'min_dist': None,
'status': 'error',
'error_message': str(e)
}
print(f" -> Error: {str(e)}")
# ==================== SAVE RESULTS ====================
with open(opt.output_file, 'w') as f:
json.dump(results, f, indent=2)
print(f"\nResults saved to {opt.output_file}")
# Print summary
successful = sum(1 for r in results.values() if r['status'] == 'success')
print(f"Processed {successful}/{len(video_files)} videos successfully.")