Download dataset_code/spatialvid/add_config_step1.py from SuperCS/useful_code: direct link, hf CLI and curl.
- Browser
- Download file 4.98 kB
-
https://huggingface.co/datasets/SuperCS/useful_code/resolve/aaf853dfec71df594de1823e958ef7533fdc28fa/dataset_code/spatialvid/add_config_step1.py
- Command line
-
hf download hf://datasets/SuperCS/useful_code@aaf853dfec71df594de1823e958ef7533fdc28fa/dataset_code/spatialvid/add_config_step1.py
-
curl -L -o add_config_step1.py https://huggingface.co/datasets/SuperCS/useful_code/resolve/aaf853dfec71df594de1823e958ef7533fdc28fa/dataset_code/spatialvid/add_config_step1.py
4.98 kB
| 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)) | |