Skip to content

Commit

Permalink
Merge pull request #59 from ami-iit/fix/video_slider
Browse files Browse the repository at this point in the history
Fix how the video and the slider work togheter
  • Loading branch information
GiulioRomualdi authored Jul 31, 2023
2 parents dc5e8fd + bbfbbc7 commit 52c6668
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 36 deletions.
18 changes: 13 additions & 5 deletions robot_log_visualizer/file_reader/signal_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,27 @@ def current_time(self):
return value

def run(self):

while True:
start = time.time()
if self.state == PeriodicThreadState.running:
self.index_lock.lock()
tmp_index = self._index
# check if index must be increased
if self._current_time > self.timestamps[tmp_index] - self.initial_time:
self._current_time += self.period
self._current_time = min(
self._current_time, self.timestamps[-1] - self.initial_time
)

# find the index associated to the current time in self.timestamps
# this is valid since self.timestamps is sorted and self._current_time is increasing
while (
self._current_time > self.timestamps[tmp_index] - self.initial_time
):
tmp_index += 1
tmp_index = min(tmp_index, self.timestamps.shape[0] - 1)
if tmp_index > len(self.timestamps):
break

self._index = tmp_index
self._current_time += self.period

self.index_lock.unlock()

self.update_index_signal.emit()
Expand Down
4 changes: 0 additions & 4 deletions robot_log_visualizer/plotter/matplotlib_viewer_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class MatplotlibViewerCanvas(FigureCanvas):
"""

def __init__(self, parent, signal_provider, period):

# create a new figure
self.fig = Figure(dpi=100)

Expand Down Expand Up @@ -66,13 +65,11 @@ def quit_animation(self):
self.vertical_line_anim._stop()

def update_plots(self, paths, legends):

for path, legend in zip(paths, legends):
path_string = "/".join(path)
legend_string = "/".join(legend[1:])

if path_string not in self.active_paths.keys():

data = self.signal_provider.data
for key in path[:-1]:
data = data[key]
Expand All @@ -90,7 +87,6 @@ def update_plots(self, paths, legends):

paths_to_be_canceled = []
for active_path in self.active_paths.keys():

path = active_path.split("/")

if path not in paths:
Expand Down
3 changes: 1 addition & 2 deletions robot_log_visualizer/robot_visualizer/meshcat_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def load_model(self, considered_joints, model_name):
def get_model_path_from_envs(env_list):
return [
Path(f) if (env != "AMENT_PREFIX_PATH") else Path(f) / "share"
for env in env_list
for env in env_list if os.getenv(env) is not None
for f in os.getenv(env).split(os.pathsep)
]

Expand All @@ -67,7 +67,6 @@ def check_if_model_exist(folder_path, model):
[self.custom_package_dir],
)
else:

model_found_in_env_folders = False
for folder in get_model_path_from_envs(self.env_list):
if check_if_model_exist(folder, model_name):
Expand Down
53 changes: 29 additions & 24 deletions robot_log_visualizer/ui/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def build_plot_title_box_dialog():


def get_icon(icon_name):

icon = QtGui.QIcon()
icon.addPixmap(
QtGui.QPixmap(str(pathlib.Path(__file__).parent / "misc" / icon_name)),
Expand Down Expand Up @@ -149,7 +148,7 @@ def __init__(self, signal_provider, meshcat_provider, animation_period):

self.signal_provider = signal_provider
self.signal_size = len(self.signal_provider)
self.signal_provider.register_update_index(self.update_slider)
self.signal_provider.register_update_index(self.update_index)

self.meshcat_provider = meshcat_provider

Expand Down Expand Up @@ -256,10 +255,11 @@ def keyPressEvent(self, event):
# for every video item we set the instant
for video_item in self.video_items:
if video_item.media_loaded:
video_percentage = float(new_index) / float(
self.ui.timeSlider.maximum()
)
video_item.media_player.setPosition(
new_index
/ self.ui.timeSlider.maximum()
* video_item.media_player.duration()
int(video_percentage * video_item.media_player.duration())
)

# update the time slider
Expand All @@ -277,10 +277,11 @@ def keyPressEvent(self, event):
# for every video item we set the instant
for video_item in self.video_items:
if video_item.media_loaded:
video_percentage = float(new_index) / float(
self.ui.timeSlider.maximum()
)
video_item.media_player.setPosition(
new_index
/ self.ui.timeSlider.maximum()
* video_item.media_player.duration()
int(video_percentage * video_item.media_player.duration())
)

self.ui.timeSlider.setValue(new_index)
Expand All @@ -305,10 +306,9 @@ def timeSlider_on_sliderMoved(self):

for video_item in self.video_items:
if video_item.media_loaded:
video_percentage = float(index) / float(self.ui.timeSlider.maximum())
video_item.media_player.setPosition(
index
/ self.ui.timeSlider.maximum()
* video_item.media_player.duration()
int(video_percentage * video_item.media_player.duration())
)

self.signal_provider.update_index(index)
Expand All @@ -322,10 +322,9 @@ def timeSlider_on_release(self):

for video_item in self.video_items:
if video_item.media_loaded:
video_percentage = float(index) / float(self.ui.timeSlider.maximum())
video_item.media_player.setPosition(
index
/ self.ui.timeSlider.maximum()
* video_item.media_player.duration()
int(video_percentage * video_item.media_player.duration())
)

self.signal_provider.update_index(index)
Expand All @@ -338,12 +337,6 @@ def timeSlider_on_release(self):
def startButton_on_click(self):
self.ui.startButton.setEnabled(False)
self.ui.pauseButton.setEnabled(True)

## TODO its better to start the video only if the tab is active. This will improve the peroformances
for video_item in self.video_items:
if video_item.media_loaded:
video_item.media_player.play()

self.signal_provider.state = PeriodicThreadState.running
# self.meshcat_provider.state = PeriodicThreadState.running

Expand All @@ -363,7 +356,6 @@ def pauseButton_on_click(self):
self.logger.write_to_log("Dataset paused.")

def plotTabCloseButton_on_click(self, index):

self.ui.tabPlotWidget.removeTab(index)
self.plot_items[index].canvas.quit_animation()
del self.plot_items[index]
Expand All @@ -372,7 +364,6 @@ def plotTabCloseButton_on_click(self, index):
self.ui.tabPlotWidget.setTabsClosable(False)

def plotTabBar_on_doubleClick(self, index):

dlg, plot_title = build_plot_title_box_dialog()
if dlg.exec() == QDialog.Accepted:
self.ui.tabPlotWidget.setTabText(index, plot_title.text())
Expand Down Expand Up @@ -464,7 +455,6 @@ def textLogTreeWidget_on_click(self):
self.text_logger.highlight_cell(self.find_text_log_index(path))

def plotTabBar_currentChanged(self, index):

# clear the selection to prepare a new one
self.ui.variableTreeWidget.clearSelection()
for active_path_str in self.plot_items[index].canvas.active_paths.keys():
Expand All @@ -478,14 +468,24 @@ def plotTabBar_currentChanged(self, index):
item.setSelected(True)

@pyqtSlot()
def update_slider(self):
def update_index(self):
if not self.slider_pressed:
self.ui.timeSlider.setValue(self.signal_provider.index)
self.ui.timeLabel.setText(f"{self.signal_provider.current_time:.2f}")
self.text_logger.highlight_cell(
self.find_text_log_index(self.get_text_log_item_path())
)

# TODO: this is a hack to update the video player and it should be done only for the activated videos
for video_item in self.video_items:
if video_item.media_loaded:
video_percentage = float(self.ui.timeSlider.value()) / float(
self.ui.timeSlider.maximum()
)
video_item.media_player.setPosition(
int(video_percentage * video_item.media_player.duration())
)

def closeEvent(self, event):
# close the window
self.pyconsole.close()
Expand Down Expand Up @@ -609,6 +609,11 @@ def __load_mat_file(self, file_name):
)
self.logger.write_to_log("Video '" + video_filename + "' opened.")

# pause all the videos
for video_item in self.video_items:
if video_item.media_loaded:
video_item.media_player.pause()

self.meshcat_provider.state = PeriodicThreadState.running

self.dataset_loaded = True
Expand Down
1 change: 0 additions & 1 deletion robot_log_visualizer/ui/text_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def __init__(self, table_widget, background_color="#ffeba8"):
self.table_widget.clear()

def add_entry(self, text, timestamp, font_color=None):

item = QTableWidgetItem(text)
if font_color is not None:
# text = '<font color="' + str(font_color) + '">' + text + "</font>"
Expand Down

0 comments on commit 52c6668

Please sign in to comment.