Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/318- Adding Video recording functionality to vispy viewer #334

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion pyneuroml/plot/PlotMorphologyVispy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
import math
import random
import typing
import imageio
from vispy.gloo import util
import vispy.app
from vispy.scene import SceneCanvas
from vispy.visuals import MeshVisual

import numpy
import progressbar
Expand Down Expand Up @@ -120,6 +125,14 @@ def create_new_vispy_canvas(
"""
# vispy: full gl+ context is required for instanced rendering
use(gl="gl+")
canvas_name = "My 3D Visualization"
canvas, scene_canvas, view = create_new_vispy_canvas(canvas_name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this calling itself recursively: create_new_vispy_canvas is calling itself, and there's no break clause to break the recursion? Have you tested this code out to see that it works?

canvas = vispy.app.Canvas(keys="interactive", size=(800, 600), title=canvas_name)
scene_canvas = SceneCanvas(keys="interactive", show=True)
view = scene_canvas.central_widget.add_view()

camera = scene_canvas.central_widget.camera
view.camera = camera

canvas = scene.SceneCanvas(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're already creating a SceneCanvas, camera, view etc. here---do you need to create one too? Can the ones we already create not be used?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes sir you are correct I think I ignored this.

keys="interactive",
Expand Down Expand Up @@ -237,8 +250,36 @@ def vispy_on_key_press(event):
# quit
elif event.text == "9":
canvas.app.quit()
if event.key == "r":
# Start recording video
output_file = "output.mp4"
duration = 10 # Video duration in seconds
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to not limit the video duration? We can have an upper limit, but ideally what we want is:

  • press r to start recording
  • press r after some time to stop recording

That'll allow users to make multiple recordings, and the file name can increment: output-1.mp4, output-2.mp4 and so on?

fps = 24 # Frames per second
record_video(output_file, duration, fps)

def record_video(output_file, duration, fps):
"""
Record a video of the vispy canvas content for a specified duration.

Args:
output_file (str): The output file path for the video.
duration (int): The duration of the video in seconds (default: 5).
fps (int): The frames per second for the video (default: 30).
"""

frames = []
for _ in range(duration * fps):
canvas.app.flush_commands()
frame = util._screenshot((canvas.size[0], canvas.size[1]))
frames.append(frame)

return canvas, view
imageio.mimwrite(output_file, frames, fps=fps)
print(f"Video saved to {output_file}")

# Additional setup and event handling for the vispy canvas
canvas.events.key_press.connect(vispy_on_key_press)

return canvas, scene_canvas, view


def plot_interactive_3D(
Expand Down Expand Up @@ -735,6 +776,7 @@ def plot_3D_cell_morphology(
:raises: ValueError if `cell` is None

"""

if cell is None:
raise ValueError(
"No cell provided. If you would like to plot a network of point neurons, consider using `plot_2D_point_cells` instead"
Expand Down
Loading