-
Notifications
You must be signed in to change notification settings - Fork 30
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
Changes from all commits
ee8c1e2
80e739c
03a74d6
7a65a5f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes sir you are correct I think I ignored this. |
||
keys="interactive", | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
That'll allow users to make multiple recordings, and the file name can increment: |
||
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( | ||
|
@@ -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" | ||
|
There was a problem hiding this comment.
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?