Free Motion Capture for Everyone πβ¨
TypeScript
Add option to synchronize videos on import (#379)
philipqueencad2290e6044
diff --git a/freemocap/gui/qt/main_window/freemocap_main_window.py b/freemocap/gui/qt/main_window/freemocap_main_window.pyindex fb265213..b43fd5dd 100644--- a/freemocap/gui/qt/main_window/freemocap_main_window.py+++ b/freemocap/gui/qt/main_window/freemocap_main_window.py@@ -52,10 +52,13 @@ from freemocap.gui.qt.widgets.control_panel.process_mocap_data_panel.process_mot)from freemocap.gui.qt.widgets.control_panel.visualization_control_panel import VisualizationControlPanelfrom freemocap.gui.qt.widgets.directory_view_widget import DirectoryViewWidget+from freemocap.gui.qt.widgets.import_videos_wizard import ImportVideosWizard+from freemocap.gui.qt.widgets.log_view_widget import LogViewWidgetfrom freemocap.gui.qt.widgets.home_widget import (HomeWidget,)-from freemocap.gui.qt.widgets.import_videos_window import ImportVideosWizard++from freemocap.gui.qt.widgets.import_videos_wizard import ImportVideosWizardfrom freemocap.gui.qt.widgets.log_view_widget import LogViewWidgetfrom freemocap.gui.qt.utilities.save_and_load_gui_state import (GuiState,@@ -494,7 +497,7 @@ class MainWindow(QMainWindow):returnself._import_videos_window = ImportVideosWizard(parent=self,- import_videos_path=import_videos_path,+ import_videos_path=import_videos_path, kill_thread_event=self._kill_thread_event,)self._import_videos_window.folder_to_save_videos_to_selected.connect(self._handle_import_videos)text = self._import_videos_window.exec()@@ -511,8 +514,8 @@ class MainWindow(QMainWindow):recording_folder_path=self.download_sample_data_thread_worker.sample_data_path)- @pyqtSlot(list, str)- def _handle_import_videos(self, video_paths: List[str], folder_to_save_videos: str):+ @pyqtSlot(list, str, bool)+ def _handle_import_videos(self, video_paths: List[str], folder_to_save_videos: str, synchronization_bool: bool):folder_to_save_videos = Path(folder_to_save_videos)folder_to_save_videos.mkdir(parents=True, exist_ok=True)@@ -520,23 +523,24 @@ class MainWindow(QMainWindow):if len(video_paths) == 0:logger.error("No videos to import!")return-- for video_path in tqdm(video_paths,- desc="Importing videos...",- colour=[255, 128, 0],- unit="video",- unit_scale=True,- leave=False- ):-- if not Path(video_path).exists():- logger.error(f"{video_path} does not exist!")- return-- destination_path = folder_to_save_videos / Path(video_path).name- logger.info(f"Copying video from {video_path} to {destination_path}")-- shutil.copy(video_path, destination_path)++ if not synchronization_bool:+ for video_path in tqdm(video_paths,+ desc="Importing videos...",+ colour=[255, 128, 0],+ unit="video",+ unit_scale=True,+ leave=False+ ):++ if not Path(video_path).exists():+ logger.error(f"{video_path} does not exist!")+ return++ destination_path = folder_to_save_videos / Path(video_path).name+ logger.info(f"Copying video from {video_path} to {destination_path}")++ shutil.copy(video_path, destination_path)timestamps_copied = copy_directory_if_contains_timestamps(source_dir=Path(video_paths[0]).parent,destination_dir=folder_to_save_videos)diff --git a/freemocap/gui/qt/widgets/import_videos_window.py b/freemocap/gui/qt/widgets/import_videos_wizard.pysimilarity index 60%rename from freemocap/gui/qt/widgets/import_videos_window.pyrename to freemocap/gui/qt/widgets/import_videos_wizard.pyindex bee784b3..bb76c5cb 100644--- a/freemocap/gui/qt/widgets/import_videos_window.py+++ b/freemocap/gui/qt/widgets/import_videos_wizard.py@@ -1,12 +1,17 @@import loggingfrom pathlib import Path+import threadingfrom typing import Union+from skelly_synchronize import create_debug_plotsfrom PyQt6.QtCore import pyqtSignalfrom PyQt6.QtGui import QFileSystemModelfrom PyQt6.QtWidgets import QVBoxLayout, QListWidget, QLabel, QFormLayout, QLineEdit, \- QTreeView, QPushButton, QDialog+ QTreeView, QPushButton, QDialog, QCheckBox, QHBoxLayout+from freemocap.gui.qt.workers.synchronize_videos_thread_worker import SynchronizeVideosThreadWorker++from freemocap.utilities.get_video_paths import get_video_pathsfrom freemocap.system.open_file import open_filefrom freemocap.system.paths_and_filenames.file_and_folder_names import SYNCHRONIZED_VIDEOS_FOLDER_NAMEfrom freemocap.system.paths_and_filenames.path_getters import get_recording_session_folder_path@@ -17,15 +22,17 @@ no_files_found_string = "No '.mp4' video files found! \n \n Note - We only looklogger = logging.getLogger(__name__)class ImportVideosWizard(QDialog):- folder_to_save_videos_to_selected = pyqtSignal(list, str)+ folder_to_save_videos_to_selected = pyqtSignal(list, str, bool)def __init__(self,import_videos_path: Union[str, Path],+ kill_thread_event: threading.Event,parent=None):super().__init__(parent=parent)+ self.kill_thread_event = kill_thread_eventself.setWindowTitle("Import Videos")-+ self.import_videos_path = import_videos_pathself._layout = QVBoxLayout()self.setLayout(self._layout)@@ -33,7 +40,7 @@ class ImportVideosWizard(QDialog):self._import_directory_view = self._create_import_directory_view(import_videos_path)self._layout.addWidget(self._import_directory_view)- self._video_file_paths = [str(path) for path in Path(import_videos_path).glob('*.mp4')]+ self._video_file_paths = [str(path) for path in get_video_paths(path_to_video_folder=import_videos_path)]self._video_file_list_view = self._create_video_file_list_widget()self._layout.addWidget(self._video_file_list_view)@@ -41,6 +48,21 @@ class ImportVideosWizard(QDialog):self._form_layout = QFormLayout()self._layout.addLayout(self._form_layout)+ self._synchronize_videos_checkbox = QCheckBox("Synchronize videos by audio cross-correlation.")+ self._synchronize_videos_checkbox.toggled.connect(self._handle_synchronize_checkbox_toggled)++ synchronization_message = QLabel(+ "(Videos must have exactly the same video frame rates and audio sample rates to be synchronized.)"+ )+ synchronization_message.setWordWrap(True)++ synchronization_layout = QHBoxLayout()++ synchronization_layout.addWidget(self._synchronize_videos_checkbox)+ synchronization_layout.addWidget(synchronization_message)++ self._form_layout.addRow("Synchronize videos:", synchronization_layout)+self._folder_name = f"import_{Path(import_videos_path).name}"self._folder_name_line_edit = QLineEdit(parent=self)self._folder_name_line_edit.textChanged.connect(self._handle_folder_name_line_edit_changed)@@ -56,8 +78,6 @@ class ImportVideosWizard(QDialog):self._continue_button.clicked.connect(self._handle_continue_button_clicked)self._form_layout.addRow(self._continue_button)--def _get_folder_videos_will_be_saved_to(self):return str(Path(get_recording_session_folder_path()) / self._folder_name / SYNCHRONIZED_VIDEOS_FOLDER_NAME)@@ -77,15 +97,16 @@ class ImportVideosWizard(QDialog):self._tree_view_widget.doubleClicked.connect(self._open_file)+ return self._tree_view_widget+def _open_file(self):index = self._tree_view_widget.currentIndex()file_path = self._file_system_model.filePath(index)logger.info(f"Opening file from file_system_view_widget: {file_path}")open_file(file_path)- def _create_video_file_list_widget(self):-+ def _create_video_file_list_widget(self):list_view = QListWidget()list_view.setWordWrap(True)if len(self._video_file_paths) == 0:@@ -99,11 +120,40 @@ class ImportVideosWizard(QDialog):self._folder_where_videos_will_be_saved_to_label.setText(self._get_folder_videos_will_be_saved_to())def _handle_continue_button_clicked(self, event):- self.folder_to_save_videos_to_selected.emit(- self._video_file_paths,- self._get_folder_videos_will_be_saved_to())+ if self._synchronize_videos_checkbox.isChecked():+ self.synchronize_videos_thread_worker = SynchronizeVideosThreadWorker(+ raw_video_folder_path=Path(self.import_videos_path),+ synchronized_video_folder_path=Path(self._get_folder_videos_will_be_saved_to()),+ kill_thread_event=self.kill_thread_event+ )+ self.synchronize_videos_thread_worker.start()+ self.synchronize_videos_thread_worker.finished.connect(self._handle_video_synchronization_finished)+ else:+ self.folder_to_save_videos_to_selected.emit(+ self._video_file_paths,+ self._get_folder_videos_will_be_saved_to(),+ False)self.accept()+ def _handle_synchronize_checkbox_toggled(self, event):+ if self._synchronize_videos_checkbox.isChecked():+ logger.info("Synchronize videos by audio selected, videos will be synchronized before importing")+ else:+ logger.info("Synchronize videos by audio deselected, videos will not be synchronized")++ def _handle_video_synchronization_finished(self):+ self._video_file_paths = [+ str(path)+ for path in get_video_paths(path_to_video_folder=self.synchronize_videos_thread_worker.output_folder_path)+ ]++ create_debug_plots(synchronized_video_folder_path=self.synchronize_videos_thread_worker.output_folder_path)++ self.folder_to_save_videos_to_selected.emit(+ self._video_file_paths,+ self._get_folder_videos_will_be_saved_to(),+ True)+if __name__ == "__main__":# from PyQt6.QtWidgets import QApplication# import sysdiff --git a/freemocap/gui/qt/workers/synchronize_videos_thread_worker.py b/freemocap/gui/qt/workers/synchronize_videos_thread_worker.pynew file mode 100644index 00000000..1339f815--- /dev/null+++ b/freemocap/gui/qt/workers/synchronize_videos_thread_worker.py@@ -0,0 +1,60 @@+import logging+import threading+from pathlib import Path+from typing import Union++from PyQt6.QtCore import pyqtSignal, QThread++from skelly_synchronize.skelly_synchronize import synchronize_videos_from_audio++logger = logging.getLogger(__name__)+++class SynchronizeVideosThreadWorker(QThread):+ finished = pyqtSignal()+ in_progress = pyqtSignal(str)++ def __init__(+ self,+ raw_video_folder_path: Path,+ synchronized_video_folder_path: Path,+ kill_thread_event: threading.Event,+ ):+ super().__init__()+ logger.info(+ f"Initializing Synchronize Videos Thread Worker"+ )+ self._kill_thread_event = kill_thread_event+ self._raw_video_folder_path = raw_video_folder_path+ self._synchronized_video_folder_path = synchronized_video_folder_path++ self.output_folder_path = None++ self._work_done = False++ @property+ def work_done(self):+ return self._work_done++ def _emit_in_progress_data(self, message: str):+ self.in_progress.emit(message)++ def run(self):+ logger.info("Beginning to synchronize videos")++ try:+ self.output_folder_path = synchronize_videos_from_audio(+ raw_video_folder_path=self._raw_video_folder_path,+ synchronized_video_folder_path=self._synchronized_video_folder_path,+ create_debug_plots_bool=False+ )++ except Exception as e:+ logger.exception("Something went wrong while synchronizing videos")+ logger.exception(e)++ self.finished.emit()+ self._work_done = True++ logger.info("Synchronizing Videos Complete")+diff --git a/pyproject.toml b/pyproject.tomlindex 75aa1ae7..2904475f 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -71,6 +71,7 @@ dependencies = ["skellycam","skelly_viewer","skellyforge",+ "skelly_synchronize","mediapipe","opencv-contrib-python==4.6.0.66","toml==0.10.2",