File size: 4,977 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 118 119 120 121 122 123 124 125 126 127 128 |
import pandas as pd
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
from functools import partial
import numpy as np
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, skip_end_frames=0, overlap=0):
"""
将长视频分割成多个段,充分利用所有帧
Args:
df: 输入DataFrame
stride: bucket选择的stride参数
skip_frames: 跳过开头的帧数
skip_end_frames: 跳过结尾的帧数
overlap: 段之间的重叠帧数,默认为0
"""
result_rows = []
max_bucket = max(length_bucket_options[stride])
for idx, row in tqdm(df.iterrows(), total=len(df), desc="Processing videos"):
if pd.isna(row.get('new_num_frame')):
print(f"Skipping row {idx}: new_num_frame is NaN or missing")
continue
num_frames = row['new_num_frame']
# 计算可用帧数(去除开头和结尾)
available_frames = num_frames - skip_frames - skip_end_frames
# 如果可用帧数太少,跳过这个视频
if available_frames < min(length_bucket_options[stride]):
continue
if available_frames <= max_bucket:
# 短视频,直接处理
new_row = row.copy()
new_row['start_frame'] = skip_frames
bucket_length = find_nearest_length_bucket(available_frames, stride)
new_row['end_frame'] = skip_frames + bucket_length
new_row['segment_id'] = 0
result_rows.append(new_row)
else:
# 长视频,分割成多个段
step_size = max_bucket - overlap
segment_count = 0
start_pos = skip_frames
effective_end = num_frames - skip_end_frames # 有效结束位置
while start_pos < effective_end:
remaining_frames = effective_end - 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]) > effective_end:
break
return pd.DataFrame(result_rows)
def add_frame_range_with_segments(csv_path, output_path=None, stride=1, skip_frames=0, skip_end_frames=0, overlap=0):
"""
为CSV添加start_frame和end_frame列,并将长视频分割成多个段
Args:
csv_path: 输入CSV文件路径
output_path: 输出CSV文件路径,如果为None则覆盖原文件
stride: bucket选择的stride参数
skip_frames: 跳过开头的帧数,默认为0
skip_end_frames: 跳过结尾的帧数,默认为0
overlap: 段之间的重叠帧数,默认为0
"""
# 读取CSV
df = pd.read_csv(csv_path)
# 分割长视频并添加帧范围
result_df = split_long_videos(df, stride, skip_frames, skip_end_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__":
input_csv = '/mnt/bn/yufan-dev-my/ysh/Ckpts/SpatialVID/SpatialVID-HQ-Final/data/SpatialVID_HQ_step0.csv'
output_csv = '/mnt/bn/yufan-dev-my/ysh/Ckpts/SpatialVID/SpatialVID-HQ-Final/data/SpatialVID_HQ_step1.csv'
df = add_frame_range_with_segments(input_csv, output_csv, stride=1, skip_frames=11, skip_end_frames=19, overlap=0)
# 打印结果统计
print(f"处理后行数: {len(df)}")
print(f"分段统计:")
print(df['segment_id'].value_counts().sort_index())
print("\n前几行示例:")
print(df.head(10))
|