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