| |
| |
|
|
| |
| |
| |
|
|
| import argparse |
| import os |
| import glob |
| import json |
| from multiprocessing import Pool, current_process |
| import torch |
|
|
| |
|
|
| parser = argparse.ArgumentParser(description="SyncNet Batch Processing with Multiprocessing") |
|
|
| 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)') |
| parser.add_argument('--num_workers', type=int, default=4, help='Number of parallel workers') |
|
|
| args = parser.parse_args() |
|
|
|
|
| def process_single_video(task): |
| """Process a single video file and return the result.""" |
| idx, total, videofile, model_path, batch_size, vshift, tmp_dir = task |
| video_name = os.path.basename(videofile) |
|
|
| |
| from SyncNetInstance import SyncNetInstance |
|
|
| |
| process_id = current_process().name |
| reference = f"{os.path.splitext(video_name)[0]}_{process_id}" |
|
|
| |
| class Opt: |
| pass |
| opt = Opt() |
| opt.batch_size = batch_size |
| opt.vshift = vshift |
| opt.tmp_dir = tmp_dir |
| opt.reference = reference |
|
|
| print(f"[{idx+1}/{total}] [{process_id}] Processing: {video_name}") |
|
|
| try: |
| |
| s = SyncNetInstance() |
| s.loadParameters(model_path) |
|
|
| offset, conf, dists = s.evaluate(opt, videofile=videofile) |
|
|
| result = { |
| 'offset': int(offset), |
| 'confidence': float(conf), |
| 'min_dist': float(dists.min()) if dists is not None else None, |
| 'status': 'success' |
| } |
| print(f" [{process_id}] {video_name} -> Offset: {offset}, Confidence: {conf:.3f}") |
|
|
| except Exception as e: |
| result = { |
| 'offset': None, |
| 'confidence': None, |
| 'min_dist': None, |
| 'status': 'error', |
| 'error_message': str(e) |
| } |
| print(f" [{process_id}] {video_name} -> Error: {str(e)}") |
|
|
| return video_name, result |
|
|
|
|
| def main(): |
| |
|
|
| video_extensions = args.video_ext.split(',') |
| video_files = [] |
| for ext in video_extensions: |
| video_files.extend(glob.glob(os.path.join(args.video_dir, f'*.{ext.strip()}'))) |
| video_files.extend(glob.glob(os.path.join(args.video_dir, f'*.{ext.strip().upper()}'))) |
|
|
| video_files = sorted(list(set(video_files))) |
| total = len(video_files) |
| print(f"Found {total} video files to process with {args.num_workers} workers.") |
|
|
| if total == 0: |
| print("No video files found. Exiting.") |
| return |
|
|
| |
|
|
| tasks = [ |
| (idx, total, vf, args.initial_model, args.batch_size, args.vshift, args.tmp_dir) |
| for idx, vf in enumerate(video_files) |
| ] |
|
|
| |
|
|
| |
| torch.multiprocessing.set_start_method('spawn', force=True) |
|
|
| with Pool(processes=args.num_workers) as pool: |
| results_list = pool.map(process_single_video, tasks) |
|
|
| |
|
|
| results = {video_name: result for video_name, result in results_list} |
|
|
| |
|
|
| with open(args.output_file, 'w') as f: |
| json.dump(results, f, indent=2) |
|
|
| print(f"\nResults saved to {args.output_file}") |
|
|
| |
| successful = sum(1 for r in results.values() if r['status'] == 'success') |
| print(f"Processed {successful}/{total} videos successfully.") |
|
|
|
|
| if __name__ == '__main__': |
| main() |
|
|