YFanwang's picture
Upload VideoX-Fun batch 1/62 (5000 files)
a386871 verified
Raw
History Blame Contribute Delete
2.57 kB
import os
import json
import shutil
from pathlib import Path
def filter_negative_samples(source_folder):
# 1. 初始化路径
source_path = Path(f"{source_folder}/train_sample_full")
target_path = Path(f"{source_folder}/negative")
# 创建目标文件夹(如果不存在)
target_path.mkdir(exist_ok=True)
# 2. 遍历文件夹下所有的 .json 文件
json_files = list(source_path.glob("*.json"))
count = 0
for json_file in json_files:
# 排除名字中包含 'metadata' 的文件
if "metadata" in json_file.name:
continue
try:
with open(json_file, 'r', encoding='utf-8') as f:
data = json.load(f)
# 3. 读取 ref_answer (假设是一个列表或字符串)
# 根据你提供的 JSON,ref_answer 是一个包含字符串的列表
ref_answers = data.get("pred_answer", [])
is_negative = False
for ans_str in ref_answers:
# 检查字符串中是否包含指定的 "answer": "No"
# 使用转义字符或简单的子串匹配
if '"answer": "No"' in ans_str:
is_negative = True
break
# 4. 如果符合条件,执行复制
if is_negative:
# 获取文件名(不含后缀)
file_stem = json_file.stem
video_file = source_path / f"{file_stem}.mp4"
# 复制 JSON
shutil.copy2(json_file, target_path / json_file.name)
# 复制 MP4 (如果存在)
if video_file.exists():
shutil.copy2(video_file, target_path / video_file.name)
shutil.copy2(video_file, target_path / video_file.name)
print(f"已复制: {file_stem}")
else:
print(f"警告: 找到负例但未找到对应视频 {video_file.name}")
count += 1
except Exception as e:
print(f"处理文件 {json_file.name} 时出错: {e}")
print(f"\n处理完成!共提取 {count} 组负样本到 negative 文件夹。")
# ================= 配置 =================
# 填入你的 JSON 和 MP4 所在的目录路径
FOLDER_PATH = "/nfs/ywang29/Reward_finetuning/VideoX-Fun/output_sd_object2/"
if __name__ == "__main__":
filter_negative_samples(FOLDER_PATH)