import os import json import argparse import time import traceback from typing import Any, Dict, List, Optional, Tuple from tqdm import tqdm try: import torch from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor import av except ImportError as e: print(f"Original error: {e}") exit(1) # --- Configuration --- DEFAULT_MODEL_PATH = "example/model/Qwen2.5-VL-model" def get_media_type(file_path: str) -> str: ext = os.path.splitext(file_path)[1].lower() if ext in ['.mp4', '.avi', '.mov', '.mkv', '.webm']: return 'video' elif ext in ['.jpg', '.jpeg', '.png', '.bmp', '.gif', '.webp']: return 'image' else: raise ValueError(f"Unsupported file format: {ext} in file {file_path}") def run_inference_on_file( json_path: str, model: Qwen2_5_VLForConditionalGeneration, processor: AutoProcessor, result_suffix: str, fps: float, max_pixels: int, total_pixels: Optional[int], gen_tokens: int ): result_json_path = f"{os.path.splitext(json_path)[0]}{result_suffix}" if os.path.exists(result_json_path): print(f" [INFO] Result file '{os.path.basename(result_json_path)}' already exists. Skipping.") return try: with open(json_path, 'r', encoding='utf-8') as f: data = json.load(f) except (json.JSONDecodeError, FileNotFoundError) as e: print(f"Could not read or parse JSON file {json_path}: {e}") return from qwen_vl_utils import process_vision_info all_results = [] for item in tqdm(data, desc=f" Inferring on {os.path.basename(json_path)}"): start_time = time.time() model_output = "N/A" try: prompt_text = item['conversations'][0]['value'] ground_truth = item['conversations'][1]['value'] media_path_key = 'image' if 'image' in item else 'video' media_relative_path = item.get(media_path_key) if not media_relative_path: raise ValueError("JSON entry is missing 'image' or 'video' key.") base_dir = os.path.dirname(json_path) media_full_path = os.path.join(base_dir, media_relative_path) if not os.path.exists(media_full_path): raise FileNotFoundError(f"Media file not found: {media_full_path}") media_type = get_media_type(media_full_path) clean_prompt = prompt_text.replace("", "").replace("