Skip to content

Commit

Permalink
Merge pull request #12 from mariobuikhuizen/you-gotta-movie-it-movie-it
Browse files Browse the repository at this point in the history
fix: bqplot stuck after the first frame of the movie
  • Loading branch information
pllim authored Jun 26, 2023
2 parents 6111845 + 73c99f1 commit 99b6839
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions jdaviz/configs/default/plugins/export_plot/export_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
except ImportError:
HAS_OPENCV = False
else:
import threading
import time
HAS_OPENCV = True

Expand Down Expand Up @@ -86,15 +87,7 @@ def vue_save_figure(self, filetype):
"""
self.save_figure(filetype=filetype)

def save_movie(self, i_start, i_end, filename=None, filetype=None, rm_temp_files=True):
"""``i`` start/end control the frames being written out.
``i_end`` is inclusive. This method creates a bunch of
PNG files (one per frame) and then deletes them after stitching
the video.
"""
if not HAS_OPENCV:
raise ImportError("Please install opencv-python to save cube as movie.")

def _save_movie(self, i_start, i_end, filename=None, filetype=None, rm_temp_files=True):
if self.config != "cubeviz":
raise NotImplementedError(f"save_movie is not available for config={self.config}")

Expand Down Expand Up @@ -143,14 +136,18 @@ def save_movie(self, i_start, i_end, filename=None, filetype=None, rm_temp_files
slice_plg._on_slider_updated({'new': i})
cur_pngfile = f"._cubeviz_movie_frame_{i}.png"

# FIXME: bqplot stuck after first frame!
# If we can fix this, maybe we can have callback to video.write and don't need PNG files.
self.save_figure(filename=cur_pngfile, filetype="png")

temp_png_files.append(cur_pngfile)
i += i_step
time.sleep(ts) # Avoid giving user epilepsy

# Wait for the roundtrip to the frontend to complete in case the epilepsy
# mitigating sleep wasn't long enough
while self.viewer.figure._upload_png_callback is not None:
time.sleep(0.1)

for cur_pngfile in temp_png_files:
video.write(cv2.imread(cur_pngfile))

Expand All @@ -164,6 +161,18 @@ def save_movie(self, i_start, i_end, filename=None, filetype=None, rm_temp_files
for cur_pngfile in temp_png_files:
os.remove(cur_pngfile)

def save_movie(self, i_start, i_end, filename=None, filetype=None, rm_temp_files=True):
"""``i`` start/end control the frames being written out.
``i_end`` is inclusive. This method creates a bunch of
PNG files (one per frame) and then deletes them after stitching
the video.
"""
if not HAS_OPENCV:
raise ImportError("Please install opencv-python to save cube as movie.")
threading.Thread(
target=lambda: self._save_movie(i_start, i_end, filename, filetype, rm_temp_files)
).start()

def vue_save_movie(self, filetype):
"""
Callback for save movie events in the front end viewer toolbars. Uses
Expand Down

0 comments on commit 99b6839

Please sign in to comment.