Skip to content

Commit

Permalink
NERF-0000: Adding Random Seed Frame Processing
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony-Tafoya committed Sep 6, 2024
1 parent f86dbe6 commit be44674
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 10 additions & 4 deletions nerfstudio/process_data/process_data_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def convert_video_to_images(
verbose: bool = False,
image_prefix: str = "frame_",
keep_image_dir: bool = False,
random_seed: Optional[int] = None
) -> Tuple[List[str], int]:
"""Converts a video into a sequence of images.
Expand Down Expand Up @@ -177,9 +178,7 @@ def convert_video_to_images(
start_x = crop_factor[2]
start_y = crop_factor[0]
crop_cmd = f"crop=w=iw*{width}:h=ih*{height}:x=iw*{start_x}:y=ih*{start_y},"

spacing = num_frames // num_frames_target


downscale_chains = [f"[t{i}]scale=iw/{2**i}:ih/{2**i}[out{i}]" for i in range(num_downscales + 1)]
downscale_dirs = [Path(str(image_dir) + (f"_{2**i}" if i > 0 else "")) for i in range(num_downscales + 1)]
downscale_paths = [downscale_dirs[i] / f"{image_prefix}%05d.png" for i in range(num_downscales + 1)]
Expand All @@ -196,7 +195,14 @@ def convert_video_to_images(

ffmpeg_cmd += " -vsync vfr"

if spacing > 1:
# Evenly distribute frame selection if random seed does not exist
spacing = num_frames // num_frames_target
if random_seed:
random.seed(random_seed)
frame_indices = sorted(random.sample(range(num_frames), num_frames_target))
select_cmd = f"select=\'" + "+".join([f"eq(n\,{idx})" for idx in frame_indices]) + "\',setpts=N/TB,"

Check failure on line 203 in nerfstudio/process_data/process_data_utils.py

View workflow job for this annotation

GitHub Actions / build

Ruff (F541)

nerfstudio/process_data/process_data_utils.py:203:26: F541 f-string without any placeholders
CONSOLE.print(f"Extracting {num_frames_target} frames using seed-based random selection.")
elif spacing > 1:
CONSOLE.print("Number of frames to extract:", math.ceil(num_frames / spacing))
select_cmd = f"thumbnail={spacing},setpts=N/TB,"
else:
Expand Down
7 changes: 7 additions & 0 deletions nerfstudio/process_data/video_to_nerfstudio_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class VideoToNerfstudioDataset(ColmapConverterToNerfstudioDataset):
"""Feature matching method to use. Vocab tree is recommended for a balance of speed
and accuracy. Exhaustive is slower but more accurate. Sequential is faster but
should only be used for videos."""
random_seed: int = None
"""Random seed to select video frames"""
eval_random_seed: int = None
"""Random seed to select video frames for eval set"""

def main(self) -> None:
"""Process video into a nerfstudio dataset."""
Expand All @@ -59,6 +63,7 @@ def main(self) -> None:
num_downscales=0,
crop_factor=(0.0, 0.0, 0.0, 0.0),
verbose=self.verbose,
random_seed = self.random_seed
)
else:
# If we're not dealing with equirects we can downscale in one step.
Expand All @@ -71,6 +76,7 @@ def main(self) -> None:
verbose=self.verbose,
image_prefix="frame_train_" if self.eval_data is not None else "frame_",
keep_image_dir=False,
random_seed = self.random_seed
)
if self.eval_data is not None:
summary_log_eval, num_extracted_frames_eval = process_data_utils.convert_video_to_images(
Expand All @@ -82,6 +88,7 @@ def main(self) -> None:
verbose=self.verbose,
image_prefix="frame_eval_",
keep_image_dir=True,
random_seed = self.eval_random_seed
)
summary_log += summary_log_eval
num_extracted_frames += num_extracted_frames_eval
Expand Down

0 comments on commit be44674

Please sign in to comment.