Repository: nerfstudio-project/nerfstudio

Issue:
Image dimensions do not match those of the initialized video
**Describe the bug**
 I trained a nerfacto on nerfstudio-posters_v3 dataset, when I tried to render the outputs into videos, the error happened.

**To Reproduce**
Steps to reproduce the behavior:
1. run training code `ns-train nerfacto --data data/nerfstudio/poster`
2. run render code `ns-render --load-config PATH_TO_CONFIG`

**Expected behavior**
I think there should be rendered videos.

**Screenshots**
If applicable, add screenshots to help explain your problem.
```
[14:54:27] Auto image downscale factor of 2                                                 nerfstudio_dataparser.py:314
           Skipping 0 files in dataset split train.                                         nerfstudio_dataparser.py:165
           Skipping 0 files in dataset split test.                                          nerfstudio_dataparser.py:165
Setting up training dataset...
Caching all 204 images.
Setting up evaluation dataset...
Caching all 22 images.
Loading latest checkpoint from load_dir
✅ Done loading checkpoint from
outputs/data-nerfstudio-posters_v3/nerfacto/2023-01-17_142500/nerfstudio_models/step-000029999.ckpt
Creating trajectory video
🎥 Rendering 🎥 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━   0% ? --:--
Traceback (most recent call last):
  File "/home/tiger/.local/bin/ns-render", line 8, in <module>
    sys.exit(entrypoint())
  File "/home/tiger/code/nerfstudio/scripts/render.py", line 283, in entrypoint
    tyro.cli(RenderTrajectory).main()
  File "/home/tiger/code/nerfstudio/scripts/render.py", line 276, in main
    render_height=render_height,
  File "/home/tiger/code/nerfstudio/scripts/render.py", line 128, in _render_trajectory_video
    writer.add_image(render_image)
  File "/home/tiger/.local/lib/python3.7/site-packages/mediapy/__init__.py", line 1396, in add_image
    raise ValueError(f'Image dimensions {image.shape[:2]} do not match'
ValueError: Image dimensions (960, 540) do not match those of the initialized video (736, 952).
```

**Additional context**
Add any other context about the problem here.



Discussion:
How did you create the camera path? Did you use the viewer? What version are you running?

Resolving patch:
diff --git a/scripts/render.py b/scripts/render.py
--- a/scripts/render.py
+++ b/scripts/render.py
@@ -42,8 +42,6 @@ def _render_trajectory_video(
     cameras: Cameras,
     output_filename: Path,
     rendered_output_names: List[str],
-    render_width: int,
-    render_height: int,
     rendered_resolution_scaling_factor: float = 1.0,
     seconds: float = 5.0,
     output_format: Literal["images", "video"] = "video",
@@ -56,8 +54,6 @@ def _render_trajectory_video(
         cameras: Cameras to render.
         output_filename: Name of the output file.
         rendered_output_names: List of outputs to visualise.
-        render_width: Video width to render.
-        render_height: Video height to render.
         rendered_resolution_scaling_factor: Scaling factor to apply to the camera image resolution.
         seconds: Length of output video.
         output_format: How to save output data.
@@ -89,20 +85,8 @@ def _render_trajectory_video(
         # but we don't know how big the video file will be, so it's not certain!)
 
     with ExitStack() as stack:
-        writer = (
-            stack.enter_context(
-                media.VideoWriter(
-                    path=output_filename,
-                    shape=(
-                        int(render_height * rendered_resolution_scaling_factor),
-                        int(render_width * rendered_resolution_scaling_factor) * len(rendered_output_names),
-                    ),
-                    fps=fps,
-                )
-            )
-            if output_format == "video"
-            else None
-        )
+        writer = None
+
         with progress:
             for camera_idx in progress.track(range(cameras.size), description=""):
                 camera_ray_bundle = cameras.generate_rays(camera_indices=camera_idx)
@@ -124,7 +108,17 @@ def _render_trajectory_video(
                 render_image = np.concatenate(render_image, axis=1)
                 if output_format == "images":
                     media.write_image(output_image_dir / f"{camera_idx:05d}.png", render_image)
-                if output_format == "video" and writer is not None:
+                if output_format == "video":
+                    if writer is None:
+                        render_width = int(render_image.shape[1] * rendered_resolution_scaling_factor)
+                        render_height = int(render_image.shape[0] * rendered_resolution_scaling_factor)
+                        writer = stack.enter_context(
+                            media.VideoWriter(
+                                path=output_filename,
+                                shape=(render_height, render_width),
+                                fps=fps,
+                            )
+                        )
                     writer.add_image(render_image)
 
     if output_format == "video":
@@ -242,8 +236,6 @@ def main(self) -> None:
             camera_start = pipeline.datamanager.eval_dataloader.get_camera(image_idx=0).flatten()
             # TODO(ethan): pass in the up direction of the camera
             camera_type = CameraType.PERSPECTIVE
-            render_width = 952
-            render_height = 736
             camera_path = get_spiral_path(camera_start, steps=30, radius=0.1)
         elif self.traj == "filename":
             with open(self.camera_path_filename, "r", encoding="utf-8") as f:
@@ -257,8 +249,6 @@ def main(self) -> None:
                 camera_type = CameraType.EQUIRECTANGULAR
             else:
                 camera_type = CameraType.PERSPECTIVE
-            render_width = camera_path["render_width"]
-            render_height = camera_path["render_height"]
             camera_path = get_path_from_json(camera_path)
         else:
             assert_never(self.traj)
@@ -272,8 +262,6 @@ def main(self) -> None:
             seconds=seconds,
             output_format=self.output_format,
             camera_type=camera_type,
-            render_width=render_width,
-            render_height=render_height,
         )
 
 


Test patch: