File size: 4,483 Bytes
e051419
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import pandas as pd

length_bucket_options = {
    1: [321, 301, 281, 261, 241, 221, 193, 181, 161, 141, 121, 101, 81, 61, 41, 21],
    2: [193, 177, 161, 156, 145, 133, 129, 121, 113, 109, 97, 85, 81, 73, 65, 61, 49, 37, 25],
}

def find_nearest_length_bucket(length, stride=1):
    buckets = length_bucket_options[stride]
    min_bucket = min(buckets)
    if length < min_bucket:
        return length
    valid_buckets = [bucket for bucket in buckets if bucket <= length]
    return max(valid_buckets)

def split_long_videos(df, stride=1, skip_frames=0, overlap=0):
    """
    将长视频分割成多个段,充分利用所有帧
    
    Args:
        df: 输入DataFrame
        stride: bucket选择的stride参数
        skip_frames: 跳过开头的帧数
        overlap: 段之间的重叠帧数,默认为0
    """
    result_rows = []
    max_bucket = max(length_bucket_options[stride])
    
    for idx, row in df.iterrows():
        num_frames = row['num frames']
        
        if num_frames <= max_bucket:
            # 短视频,直接处理
            new_row = row.copy()
            new_row['start_frame'] = skip_frames
            bucket_length = find_nearest_length_bucket(num_frames - skip_frames, stride)
            new_row['end_frame'] = skip_frames + bucket_length
            new_row['segment_id'] = 0
            result_rows.append(new_row)
        else:
            # 长视频,分割成多个段
            available_frames = num_frames - skip_frames
            step_size = max_bucket - overlap
            segment_count = 0
            
            start_pos = skip_frames
            while start_pos < num_frames:
                remaining_frames = num_frames - start_pos
                
                # 如果剩余帧数小于最小bucket,跳过
                if remaining_frames < min(length_bucket_options[stride]):
                    break
                
                new_row = row.copy()
                new_row['start_frame'] = start_pos
                
                # 计算这个段的长度
                segment_length = min(remaining_frames, max_bucket)
                bucket_length = find_nearest_length_bucket(segment_length, stride)
                new_row['end_frame'] = start_pos + bucket_length
                new_row['segment_id'] = segment_count
                
                result_rows.append(new_row)
                
                # 移动到下一个段的起始位置
                start_pos += step_size
                segment_count += 1
                
                # 如果剩余帧数不足以形成新段,退出
                if start_pos + min(length_bucket_options[stride]) > num_frames:
                    break
    
    return pd.DataFrame(result_rows)

def add_frame_range_with_segments(csv_path, output_path=None, stride=1, skip_frames=0, overlap=0):
    """
    为CSV添加start_frame和end_frame列,并将长视频分割成多个段
    
    Args:
        csv_path: 输入CSV文件路径
        output_path: 输出CSV文件路径,如果为None则覆盖原文件
        stride: bucket选择的stride参数
        skip_frames: 跳过开头的帧数,默认为0
        overlap: 段之间的重叠帧数,默认为0
    """
    # 读取CSV
    df = pd.read_csv(csv_path)
    
    # 分割长视频并添加帧范围
    result_df = split_long_videos(df, stride, skip_frames, overlap)
    
    # 保存结果
    if output_path is None:
        output_path = csv_path
    result_df.to_csv(output_path, index=False)
    
    return result_df

# 使用示例
if __name__ == "__main__":
    # 示例1: 基本用法,无重叠
    input_csv = '/mnt/bn/yufan-dev-my/ysh/Ckpts/SpatialVID/SpatialVID-HQ/data/train/SpatialVID_HQ_metadata.csv'  # 替换为你的输入文件名
    output_csv = 'test.csv'
    df = add_frame_range_with_segments(input_csv, output_csv, stride=1, skip_frames=11, overlap=0)
    
    # 示例2: 带重叠,确保段之间的连续性
    # df = add_frame_range_with_segments('your_file.csv', 'output.csv', stride=1, skip_frames=0, overlap=20)
    
    # 示例3: 使用stride=2的bucket
    # df = add_frame_range_with_segments('your_file.csv', 'output.csv', stride=2, skip_frames=5, overlap=10)
    
    # 打印结果统计
    print(f"原始行数可能更少,处理后行数: {len(df)}")
    print(f"分段统计:")
    print(df['segment_id'].value_counts().sort_index())
    print("\n前几行示例:")
    print(df.head(10))