|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
""" |
|
|
|
|
|
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)) |
|
|
|