File size: 1,537 Bytes
bc971c7 | 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 | import pandas as pd
import subprocess
import os
from tqdm import tqdm
def slice_sign_dataset(csv_path, video_dir="fsl-data/raw_videos", output_dir="fsl-data/sliced_dataset"):
df = pd.read_csv(csv_path)
print(f"[*] Beginning slicing of {len(df)} samples...")
for idx, row in tqdm(df.iterrows(), total=len(df)):
filename = row['filename']
label = row['label']
start_f = int(row['start_frame'])
end_f = int(row['end_frame'])
video_stem = os.path.splitext(filename)[0]
target_folder = os.path.join(output_dir, video_stem, label)
os.makedirs(target_folder, exist_ok=True)
output_filename = f"sample_{idx:04d}.mp4"
output_path = os.path.join(target_folder, output_filename)
if os.path.exists(output_path):
continue
start_sec = start_f / 30.0
duration_sec = (end_f - start_f) / 30.0
input_path = os.path.join(video_dir, filename)
cmd = [
'ffmpeg',
'-y',
'-ss', str(start_sec),
'-i', input_path,
'-t', str(duration_sec),
'-c', 'copy',
'-loglevel', 'error',
output_path
]
try:
subprocess.run(cmd, check=True)
except subprocess.CalledProcessError as e:
print(f"[!] Error slicing {filename} at index {idx}: {e}")
if __name__ == "__main__":
slice_sign_dataset("master_annotations.csv")
|