|
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)
|
|
|
|
|
|
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):
|
|
|
|
shutil.copy(video_src, video_dst)
|
|
print(f"✅ Video copied: {video_src} -> {video_dst}")
|
|
|
|
|
|
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())
|
|
|
|
|
|
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}")
|
|
|
|
|
|
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}")
|
|
|