import os import json import shutil # 📂 **路径配置** ANNOTATION_DIR = r"C:\Users\yang2\Desktop\YVT\holisticOcrYvt" VIDEO_DIR = r"C:\Users\yang2\Desktop\YVT\YVT\videos\test" VIDEO_DST_DIR = r"C:\Users\yang2\Desktop\BenchMark\HolisticOCR\Videos1" ANNOTATION_OUTPUT_PATH = r"C:\Users\yang2\Desktop\BenchMark\HolisticOCR\holisticOCRTest1.json" # 确保目标路径存在 os.makedirs(VIDEO_DST_DIR, exist_ok=True) def process_files(annotation_dir, video_dir, video_dst_dir, output_json): """ 处理 YVT 数据集,将视频和标注文件统一重命名并合并 JSON 标注。 """ combined_annotations = [] question_id = 551 # 如果文件已存在,先读取已有标注,防止覆盖 if os.path.exists(output_json): with open(output_json, 'r', encoding='utf-8') as f: combined_annotations = json.load(f) existing_ids = {qa['question_id'] for qa in combined_annotations} question_id = max(existing_ids) + 1 if existing_ids else 551 for file in os.listdir(annotation_dir): if file.endswith(".json"): annotation_path = os.path.join(annotation_dir, file) # 去掉.mp4_holisticOcr后缀,匹配视频名 video_base_name = file.replace(".mp4_holisticOcr.json", "") video_src = os.path.join(video_dir, f"{video_base_name}.mp4") video_dst = os.path.join(video_dst_dir, f"video_{question_id}.mp4") if os.path.exists(video_src): # 1️⃣ **复制并重命名视频** shutil.copy(video_src, video_dst) print(f"✅ Video copied: {video_src} -> {video_dst}") # 2️⃣ **读取 JSON 标注** with open(annotation_path, 'r', encoding='utf-8') as f: annotation_data = json.load(f) text_content = annotation_data.get('answer', '') text_answer = " ".join(text_content.split()) # 确保文本格式统一 # 3️⃣ **生成新的 JSON 标注** annotation_entry = { "video_id": f"video_{question_id}", "question": "请给出视频中所有文本内容。", "answer": text_answer, "type": "holisticOCR", "source": video_base_name, "question_id": question_id } combined_annotations.append(annotation_entry) print(f"✅ Annotation added: video_{question_id}, source: {video_base_name}") question_id += 1 else: print(f"⚠️ Video not found: {video_src}") # 4️⃣ **保存合并后的 JSON 文件** with open(output_json, 'w', encoding='utf-8') as out_f: json.dump(combined_annotations, out_f, ensure_ascii=False, indent=4) print(f"🎯 Annotations updated and saved to {output_json}") if __name__ == "__main__": try: process_files(ANNOTATION_DIR, VIDEO_DIR, VIDEO_DST_DIR, ANNOTATION_OUTPUT_PATH) print("🚀 YVT Holistic OCR Benchmark 数据集重命名与整合完成!") except Exception as e: print(f"❌ 发生错误: {e}")