Datasets:

Modalities:
Image
ArXiv:
Libraries:
Datasets
License:
File size: 2,950 Bytes
37a488d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import os
import cv2
import glob
from concurrent.futures import ThreadPoolExecutor
from tqdm import tqdm

# ================= 配置路径 =================
SRC_BASE = "/mnt/petrelfs/fengyuan/workspace_zpy/data/RISEBench/RISEBench_outputs/wan2/wan2_2_result"
DST_BASE = "/mnt/petrelfs/fengyuan/workspace_zpy/data/RISEBench/RISEBench_outputs/wan2"

SUB_FOLDERS = [
    "causal_reasoning",
    "logical_reasoning",
    "spatial_reasoning",
    "temporal_reasoning"
]

# 设置并行线程数 (根据CPU核心数调整,通常 8-16 比较合适)
MAX_WORKERS = 16 
# ===========================================

def extract_last_frame(args):
    """
    单个视频处理函数
    """
    src_path, dst_path = args
    
    try:
        # 打开视频
        cap = cv2.VideoCapture(src_path)
        if not cap.isOpened():
            return f"Error: Cannot open {src_path}"

        # 获取总帧数
        total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        
        if total_frames > 0:
            # 定位到最后一帧
            cap.set(cv2.CAP_PROP_POS_FRAMES, total_frames - 1)
            ret, frame = cap.read()
            
            if ret:
                # 保存图片
                cv2.imwrite(dst_path, frame)
            else:
                return f"Error: Could not read last frame of {src_path}"
        else:
            return f"Error: Video has 0 frames {src_path}"
            
        cap.release()
        return None # Success
        
    except Exception as e:
        return f"Exception processing {src_path}: {str(e)}"

def main():
    tasks = []

    print("正在扫描视频文件...")
    
    # 遍历四个子文件夹
    for sub in SUB_FOLDERS:
        src_dir = os.path.join(SRC_BASE, sub)
        dst_dir = os.path.join(DST_BASE, sub)
        
        # 如果输出目录不存在则创建
        os.makedirs(dst_dir, exist_ok=True)
        
        # 获取所有mp4文件
        mp4_files = glob.glob(os.path.join(src_dir, "*.mp4"))
        
        for mp4_path in mp4_files:
            file_name = os.path.basename(mp4_path)
            # 替换扩展名 .mp4 -> .png
            image_name = os.path.splitext(file_name)[0] + ".png"
            dst_path = os.path.join(dst_dir, image_name)
            
            tasks.append((mp4_path, dst_path))

    print(f"共发现 {len(tasks)} 个视频,开始处理...")

    # 使用多线程并行处理
    with ThreadPoolExecutor(max_workers=MAX_WORKERS) as executor:
        # 使用 tqdm 显示进度条
        results = list(tqdm(executor.map(extract_last_frame, tasks), total=len(tasks), unit="vid"))

    # 打印错误日志(如果有)
    errors = [res for res in results if res is not None]
    if errors:
        print(f"\n处理完成,但有 {len(errors)} 个错误:")
        for err in errors:
            print(err)
    else:
        print("\n所有视频处理成功!")

if __name__ == "__main__":
    main()