File size: 3,287 Bytes
804301e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/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.")