import os import json import argparse import base64 import cv2 import tempfile import subprocess import shutil import io import time import glob from openai import OpenAI from tqdm import tqdm from PIL import Image 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}") def encode_media_to_base64(media_path: str) -> str: try: with open(media_path, "rb") as media_file: return base64.b64encode(media_file.read()).decode('utf-8') except Exception as e: raise IOError(f"Could not read or encode file {media_path}: {e}") def extract_keyframes(video_path: str, max_frames: int = 20) -> list: cap = cv2.VideoCapture(video_path) if not cap.isOpened(): raise ValueError(f"Cannot open video file: {video_path}") total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) video_fps = cap.get(cv2.CAP_PROP_FPS) duration = total_frames / video_fps if video_fps > 0 else 0 if duration <= 20: target_frames = min(max_frames, int(duration)) frame_interval = max(1, int(video_fps)) else: target_frames = max_frames frame_interval = max(1, int(total_frames / max_frames)) keyframes = [] frame_count = 0 sampled_count = 0 while sampled_count < target_frames: ret, frame = cap.read() if not ret: break if frame_count % frame_interval == 0: frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) pil_image = Image.fromarray(frame_rgb) keyframes.append(pil_image) sampled_count += 1 frame_count += 1 cap.release() print(f" Extracted {len(keyframes)} keyframes.") return keyframes def extract_audio_to_text(video_path: str, client: OpenAI) -> str: temp_audio_path = "" try: with tempfile.NamedTemporaryFile(suffix=".mp3", delete=False) as temp_audio: temp_audio_path = temp_audio.name command = [ 'ffmpeg', '-i', video_path, '-vn', '-acodec', 'mp3', '-ar', '16000', '-ac', '1', '-y', temp_audio_path ] result = subprocess.run(command, capture_output=True, text=True, check=False) if result.returncode != 0: print(f" [WARN] Ffmpeg audio extraction failed: {result.stderr}") return "Audio extraction failed." with open(temp_audio_path, 'rb') as audio_file: transcription = client.audio.transcriptions.create( model="whisper-1", file=audio_file, response_format="text" ) return transcription except Exception as e: print(f" Audio processing failed: {e}") return "Audio processing failed." finally: if temp_audio_path and os.path.exists(temp_audio_path): os.unlink(temp_audio_path) def process_dataset(dataset_path: str, client: OpenAI, model_name: str, result_suffix: str): result_path = f"{os.path.splitext(dataset_path)[0]}{result_suffix}" if os.path.exists(result_path): print(f"Result file '{os.path.basename(result_path)}' already exists. Skipping.") return try: with open(dataset_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 {dataset_path}: {e}") return all_results = [] base_dir = os.path.dirname(dataset_path) for item in tqdm(data, desc=f"Processing items from {os.path.basename(dataset_path)}"): start_time = time.time() model_output = "" try: prompt = item['conversations'][0]['value'] ground_truth = item['conversations'][1]['value'] media_key = 'image' if 'image' in item else 'video' media_relative_path = item.get(media_key) if not media_relative_path: raise ValueError("JSON item is missing 'image' or 'video' key.") 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.replace("", "").replace("