Free Motion Capture for Everyone πβ¨
TypeScript
Jon/load external videos (#248)
jonmatthis02f45e54d641
diff --git a/src/core_processes/capture_volume_calibration/charuco_board_detection/default_charuco_square_size.py b/src/core_processes/capture_volume_calibration/charuco_board_detection/default_charuco_square_size.pyindex c94b5e30..021eb9d7 100644--- a/src/core_processes/capture_volume_calibration/charuco_board_detection/default_charuco_square_size.py+++ b/src/core_processes/capture_volume_calibration/charuco_board_detection/default_charuco_square_size.py@@ -1 +1 @@-default_charuco_square_size_mm = 54+default_charuco_square_size_mm = 39diff --git a/src/core_processes/capture_volume_calibration/get_anipose_calibration_object.py b/src/core_processes/capture_volume_calibration/get_anipose_calibration_object.pyindex c289965b..7e760eea 100644--- a/src/core_processes/capture_volume_calibration/get_anipose_calibration_object.py+++ b/src/core_processes/capture_volume_calibration/get_anipose_calibration_object.py@@ -3,7 +3,10 @@ import shutilfrom pathlib import Pathfrom typing import Union-from src.config.home_dir import get_freemocap_data_folder_path+from src.config.home_dir import (+ get_freemocap_data_folder_path,+ CAMERA_CALIBRATION_FILE_NAME,+)from src.core_processes.capture_volume_calibration.anipose_camera_calibration import (freemocap_anipose,)@@ -21,13 +24,51 @@ def load_most_recent_anipose_calibration_toml(f"loading `most recent calibration from:{str(session_calibration_file_path)}")if save_copy_of_calibration_to_this_path is not None:+ logger.info(+ f"Saving copy of `most_recent_calibration.toml` to {save_copy_of_calibration_to_this_path}"+ )+shutil.copy(- str(session_calibration_file_path), save_copy_of_calibration_to_this_path+ str(session_calibration_file_path),+ str(+ Path(save_copy_of_calibration_to_this_path)+ / CAMERA_CALIBRATION_FILE_NAME+ ),)return freemocap_anipose.CameraGroup.load(str(session_calibration_file_path))+def load_anipose_calibration_toml_from_user_selection(+ user_selected_toml_path: Union[str, Path],+ save_copy_of_calibration_to_this_path: Union[str, Path] = None,+):+ logger.info(f"loading camera calibration file from:{str(user_selected_toml_path)}")+ try:+ anipose_calibration_object = freemocap_anipose.CameraGroup.load(+ str(user_selected_toml_path)+ )++ if save_copy_of_calibration_to_this_path is not None:+ logger.info(+ f"Saving copy of {user_selected_toml_path} to {save_copy_of_calibration_to_this_path}"+ )+ shutil.copy(+ str(user_selected_toml_path),+ str(+ Path(save_copy_of_calibration_to_this_path)+ / CAMERA_CALIBRATION_FILE_NAME+ ),+ )+ return anipose_calibration_object+ except Exception as e:+ logger.error(+ f"Failed to load anipose calibration info from {str(user_selected_toml_path)}"+ )+ raise e+ return None++def load_calibration_from_session_id(session_calibration_file_path: Union[str, Path],):diff --git a/src/gui/main/main_window/left_panel_controls/toolbox_widgets/calibrate_capture_volume_panel.py b/src/gui/main/main_window/left_panel_controls/toolbox_widgets/calibrate_capture_volume_panel.pyindex 17fbf792..ba94c941 100644--- a/src/gui/main/main_window/left_panel_controls/toolbox_widgets/calibrate_capture_volume_panel.py+++ b/src/gui/main/main_window/left_panel_controls/toolbox_widgets/calibrate_capture_volume_panel.py@@ -1,4 +1,5 @@import logging+from pathlib import Pathfrom PyQt6.QtWidgets import (QWidget,@@ -9,6 +10,8 @@ from PyQt6.QtWidgets import (QLineEdit,QComboBox,QLabel,+ QFileDialog,+ QRadioButton,)from pyqtgraph.parametertree import Parameter, ParameterTree@@ -30,36 +33,104 @@ class CalibrateCaptureVolumePanel(QWidget):self.setLayout(self._layout)self._layout.addStretch()- self._use_previous_calibration_checkbox = (- self._create_use_previous_calibration_checkbox()+ self._use_most_recent_calibration_radio_button = QRadioButton(+ "Use Most Recent Calibration")- self._layout.addWidget(self._use_previous_calibration_checkbox)+ self._use_most_recent_calibration_radio_button.toggled.connect(+ self._handle_use_previous_calibration_radio_button_toggled+ )+ self._layout.addWidget(self._use_most_recent_calibration_radio_button)++ self._load_camera_calibration_radio_button = QRadioButton(+ "Load Camera Calibration..."+ )+ self._layout.addWidget(self._load_camera_calibration_radio_button)+ self._load_camera_calibration_radio_button.toggled.connect(+ self._handle_load_camera_calibration_radio_button_toggled+ )++ self._load_calibration_toml_dialog_button = QPushButton(+ "Load Camera Calibration TOML..."+ )+ self._layout.addWidget(self._load_calibration_toml_dialog_button)+ self._load_calibration_toml_dialog_button.clicked.connect(+ self._open_load_camera_calibration_toml_dialog+ )+ self._load_calibration_toml_dialog_button.setToolTip(+ "locate the camera_calibration.toml file from a previous calibration"+ )++ self._user_selected_camera_calibration_toml_path_label = QLabel(+ "No Camera Calibration TOML Loaded"+ )+ self._user_selected_camera_calibration_toml_path_label.setWordWrap(True)+ self._layout.addWidget(self._user_selected_camera_calibration_toml_path_label)++ self._load_calibration_toml_dialog_button.hide()+ self._user_selected_camera_calibration_toml_path_label.hide()++ self._calibrate_from_synchronized_videos_radio_button = QRadioButton(+ "Calibrate from Synchronized Videos"+ )+ self._layout.addWidget(self._calibrate_from_synchronized_videos_radio_button)+ self._calibrate_from_synchronized_videos_radio_button.toggled.connect(+ self._handle_calibrate_from_synchronized_videos_radio_button_toggled+ )+ self._calibrate_from_videos_TODO_label = QLabel("TODO - This, lol")+ self._layout.addWidget(self._calibrate_from_videos_TODO_label)+ self._calibrate_from_videos_TODO_label.hide()+ self._record_calibration_videos_radio_button = QRadioButton(+ "Record New Calibration Videos"+ )+ self._layout.addWidget(self._record_calibration_videos_radio_button)+ self._record_calibration_videos_radio_button.setChecked(True)+ self._record_calibration_videos_radio_button.toggled.connect(+ self._handle_record_calibration_videos_radio_button_toggled+ )# start/stop recording button layout- record_button_layout = QVBoxLayout()- self._layout.addLayout(record_button_layout)+ record_buttons_layout = QVBoxLayout()+ self._layout.addLayout(record_buttons_layout)self._start_recording_button = QPushButton("Begin Recording")- record_button_layout.addWidget(self._start_recording_button)+ record_buttons_layout.addWidget(self._start_recording_button)self._stop_recording_button = QPushButton("Stop Recording")self._stop_recording_button.setEnabled(False)- record_button_layout.addWidget(self._stop_recording_button)-+ record_buttons_layout.addWidget(self._stop_recording_button)self._process_automatically_checkbox = QCheckBox("Process Calibration Videos Automatically")+ self._process_automatically_checkbox.setChecked(True)+ self._process_automatically_checkbox.stateChanged.connect(+ self._enable_or_disable_calibrate_from_videos_button+ )+ self._layout.addWidget(self._process_automatically_checkbox)- self._layout.addWidget(QLabel("Charuco Checkerboard Type:"))- self._charuco_board_definition = CharucoBoardDefinition()- self._charuco_combo_box = QComboBox()- self._charuco_combo_box.addItem("Pre-Alpha (5x7 squares)")- self._charuco_combo_box.addItem("Bigger Squares (3x5 squares)")+ self._calibrate_capture_volume_from_videos_button = QPushButton(+ "Calibrate Capture Volume From Videos"+ )+ self._calibrate_capture_volume_from_videos_button.setEnabled(False)+ self._layout.addWidget(+ self._calibrate_capture_volume_from_videos_button,+ )- self._layout.addWidget(self._charuco_combo_box)+ self._layout.addWidget(QLabel("____________"))+ self._layout.addWidget(QLabel("Calibration Parameters:"))++ qlabel_link_to_charuco_board = QLabel()+ qlabel_link_to_charuco_board.setText(+ '<a href="https://raw.githubusercontent.com/freemocap/freemocap/main/assets/charuco/charuco_board_image_highRes.png">Link to Charuco Board Image</a>'+ )+ qlabel_link_to_charuco_board.setOpenExternalLinks(True)+ qlabel_link_to_charuco_board.setWordWrap(True)+ self._layout.addWidget(qlabel_link_to_charuco_board)self._charuco_square_size_form_layout = QFormLayout()self._charuco_square_size_line_edit_widget = QLineEdit()+ self._charuco_square_size_line_edit_widget.setToolTip(+ "The length of one of the edges of the black squares in the calibration board in mm"+ )self._charuco_square_size_line_edit_widget.setText(str(default_charuco_square_size_mm))@@ -69,19 +140,13 @@ class CalibrateCaptureVolumePanel(QWidget):self._layout.addLayout(self._charuco_square_size_form_layout)- self._process_automatically_checkbox.setChecked(True)- self._process_automatically_checkbox.stateChanged.connect(- self._enable_or_disable_calibrate_from_videos_button- )- self._layout.addWidget(self._process_automatically_checkbox)+ self._layout.addWidget(QLabel("Charuco Checkerboard Type:"))+ self._charuco_board_definition = CharucoBoardDefinition()+ self._charuco_combo_box = QComboBox()+ self._charuco_combo_box.addItem("Pre-Alpha (5x7 squares)")+ # self._charuco_combo_box.addItem("Bigger Squares (3x5 squares)")- self._calibrate_capture_volume_from_videos_button = QPushButton(- "Calibrate Capture Volume From Videos"- )- self._calibrate_capture_volume_from_videos_button.setEnabled(False)- self._layout.addWidget(- self._calibrate_capture_volume_from_videos_button,- )+ self._layout.addWidget(self._charuco_combo_box)self._layout.addStretch()@@ -103,7 +168,11 @@ class CalibrateCaptureVolumePanel(QWidget):@propertydef use_previous_calibration_box_is_checked(self):- return self._use_previous_calibration_checkbox.isChecked()+ return self._use_most_recent_calibration_radio_button.isChecked()++ @property+ def load_camera_calibration_checkbox_is_checked(self):+ return self._load_camera_calibration_radio_button.isChecked()@propertydef charuco_square_size(self) -> float:@@ -113,11 +182,9 @@ class CalibrateCaptureVolumePanel(QWidget):def charuco_combo_box_selection(self):return self._charuco_combo_box.currentText()- def _create_use_previous_calibration_checkbox(self):- previous_calibration_checkbox = QCheckBox("Use Previous Calibration")- previous_calibration_checkbox.setChecked(False)-- return previous_calibration_checkbox+ @property+ def user_selected_calibration_toml_path(self):+ return self._user_selected_camera_calibration_toml_path_label.text()def _enable_or_disable_calibrate_from_videos_button(self):logger.debug("Process calibration videos automatically checkbox state changed")@@ -136,3 +203,68 @@ class CalibrateCaptureVolumePanel(QWidget):self._start_recording_button.setEnabled(True)self._start_recording_button.setText("Begin Recording")self._calibrate_capture_volume_from_videos_button.setEnabled(True)++ def _handle_use_previous_calibration_radio_button_toggled(self):++ self._hide_load_camera_calibration_stuff()+ self._hide_calibrate_from_videos_stuff()+ self._hide_record_new_calibration_videos_stuff()+ self._enable_disable_calibration_parameters_stuff(False)++ def _handle_load_camera_calibration_radio_button_toggled(self):+ self._load_calibration_toml_dialog_button.show()+ self._user_selected_camera_calibration_toml_path_label.show()+ self._hide_calibrate_from_videos_stuff()+ self._hide_record_new_calibration_videos_stuff()+ self._enable_disable_calibration_parameters_stuff(False)++ def _handle_calibrate_from_synchronized_videos_radio_button_toggled(self):+ self._calibrate_from_videos_TODO_label.show()+ self._hide_load_camera_calibration_stuff()+ self._hide_record_new_calibration_videos_stuff()+ self._enable_disable_calibration_parameters_stuff(True)++ def _handle_record_calibration_videos_radio_button_toggled(self):+ self._show_record_new_calibration_videos_stuff()+ self._hide_load_camera_calibration_stuff()+ self._hide_calibrate_from_videos_stuff()+ self._enable_disable_calibration_parameters_stuff(True)++ def _open_load_camera_calibration_toml_dialog(self):+ # from this tutorial - https://www.youtube.com/watch?v=gg5TepTc2Jg&t=649s+ calibration_toml_path_selection = QFileDialog.getOpenFileName(+ self,+ "Select 'toml' containing camera calibration info (Note - this is the 'toml' file produced by the `anipose` calibration process)",+ str(Path.home()),+ "Camera Calibration TOML (*.toml)",+ )+ self.calibration_toml_path_str = calibration_toml_path_selection[0]+ logger.info(+ f"User selected camera calibration toml path:{self.calibration_toml_path_str}"+ )+ self._user_selected_camera_calibration_toml_path_label.setText(+ self.calibration_toml_path_str+ )++ def _hide_record_new_calibration_videos_stuff(self):+ self._start_recording_button.hide()+ self._stop_recording_button.hide()+ self._process_automatically_checkbox.hide()+ self._calibrate_capture_volume_from_videos_button.hide()++ def _show_record_new_calibration_videos_stuff(self):+ self._start_recording_button.show()+ self._stop_recording_button.show()+ self._process_automatically_checkbox.show()+ self._calibrate_capture_volume_from_videos_button.show()++ def _hide_load_camera_calibration_stuff(self):+ self._load_calibration_toml_dialog_button.hide()+ self._user_selected_camera_calibration_toml_path_label.hide()++ def _enable_disable_calibration_parameters_stuff(self, bool_in: bool):+ self._charuco_combo_box.setEnabled(bool_in)+ self._charuco_square_size_line_edit_widget.setEnabled(bool_in)++ def _hide_calibrate_from_videos_stuff(self):+ self._calibrate_from_videos_TODO_label.hide()diff --git a/src/gui/main/main_window/main_window.py b/src/gui/main/main_window/main_window.pyindex e72a0bc0..40eaa05f 100644--- a/src/gui/main/main_window/main_window.py+++ b/src/gui/main/main_window/main_window.py@@ -24,6 +24,7 @@ from src.core_processes.capture_volume_calibration.charuco_board_detection.datacfrom src.core_processes.capture_volume_calibration.get_anipose_calibration_object import (load_most_recent_anipose_calibration_toml,load_calibration_from_session_id,+ load_anipose_calibration_toml_from_user_selection,)from src.core_processes.mediapipe_stuff.load_mediapipe2d_data import (load_mediapipe2d_data,@@ -166,10 +167,10 @@ class MainWindow(QMainWindow):self._load_session_action = QAction("&Load Session...", parent=self)self._load_session_action.setShortcut("Ctrl+O")- self._import_external_videos_action = QAction(- "&Import External Videos...", parent=self+ self._import_videos_action = QAction(+ "Import Synchronized &Videos...", parent=self)- self._import_external_videos_action.setShortcut("Ctrl+I")+ self._import_videos_action.setShortcut("Ctrl+I")self._reboot_gui_action = QAction("&Reboot GUI", parent=self)self._reboot_gui_action.setShortcut("Ctrl+R")@@ -223,6 +224,7 @@ class MainWindow(QMainWindow):file_menu.addAction(self._new_session_action)file_menu.addAction(self._load_most_recent_session_action)file_menu.addAction(self._load_session_action)+ file_menu.addAction(self._import_videos_action)file_menu.addAction(self._reboot_gui_action)file_menu.addAction(self._exit_action)@@ -267,11 +269,8 @@ class MainWindow(QMainWindow):self._load_session_action.triggered.connect(self._load_session_dialog)- self._todo_import_videos_pop_up = QLabel(- "\nTODO - pop up 'session id' panel, then a 'select video folder' dialog and copy those videos into the session folder\n"- )- self._import_external_videos_action.triggered.connect(- self._todo_import_videos_pop_up.show+ self._import_videos_action.triggered.connect(+ self._middle_viewing_panel.welcome_create_or_load_session_panel.show_import_videos_view)self._reboot_gui_action.triggered.connect(self._reboot_gui)@@ -326,8 +325,8 @@ class MainWindow(QMainWindow):self._load_session_action.trigger)- self._middle_viewing_panel.welcome_create_or_load_session_panel.import_external_videos_button.clicked.connect(- self._import_external_videos_action.trigger+ self._middle_viewing_panel.welcome_create_or_load_session_panel.synchronized_videos_selection_dialog_button.clicked.connect(+ self._import_videos)# Camera Control Panel@@ -464,6 +463,30 @@ class MainWindow(QMainWindow):self._start_session(self._session_id)+ def _import_videos(self):+ # from this tutorial - https://www.youtube.com/watch?v=gg5TepTc2Jg&t=649s+ external_videos_path = QFileDialog.getExistingDirectory(+ self,+ "Select a folder containig synchronized videos (each video must have *exactly* the same number of frames)",+ str(Path.home()),+ )+ self._session_id = (+ self._middle_viewing_panel.welcome_create_or_load_session_panel.session_id_input_string+ )++ synchronized_videos_folder_path = get_synchronized_videos_folder_path(+ self._session_id, create_folder=True+ )++ logger.info(+ f"Copying videos from {external_videos_path} to {synchronized_videos_folder_path}"+ )++ for video_path in Path(external_videos_path).glob("*.mp4"):+ shutil.copy(video_path, synchronized_videos_folder_path)++ self._start_session(self._session_id)+def _start_session(self, session_id: str, new_session: bool = False):if (@@ -486,7 +509,8 @@ class MainWindow(QMainWindow):)if (- self._middle_viewing_panel.welcome_create_or_load_session_panel.auto_detect_cameras_checkbox.isChecked()+ new_session+ and self._middle_viewing_panel.welcome_create_or_load_session_panel.auto_detect_cameras_checkbox.isChecked()):self._thread_worker_manager.launch_detect_cameras_worker()@@ -494,11 +518,6 @@ class MainWindow(QMainWindow):self._middle_viewing_panel.show_camera_streams()- if new_session:- self._auto_launch_camera_streams = True-- self._middle_viewing_panel.show_camera_streams()-if (new_sessionand self._middle_viewing_panel.welcome_create_or_load_session_panel.auto_connect_to_cameras_checkbox.isChecked()@@ -663,6 +682,19 @@ class MainWindow(QMainWindow):anipose_calibration_object = load_most_recent_anipose_calibration_toml(get_session_folder_path(self._session_id))+ elif (+ self._control_panel.calibrate_capture_volume_panel.load_camera_calibration_checkbox_is_checked+ ):+ anipose_calibration_object = load_anipose_calibration_toml_from_user_selection(+ self._control_panel.calibrate_capture_volume_panel.user_selected_calibration_toml_path,+ get_session_folder_path(self._session_id),+ )+ if anipose_calibration_object is None:+ logger.error(+ "Could not load user selected calibration file! Aborting '_setup_and_launch_triangulate_3d_thread_worker'..."+ )+ return+else:anipose_calibration_object = load_calibration_from_session_id(get_session_calibration_toml_file_path(self._session_id)diff --git a/src/gui/main/main_window/middle_panel_viewers/welcome_view/welcome_create_or_load_new_session_panel.py b/src/gui/main/main_window/middle_panel_viewers/welcome_view/welcome_create_or_load_new_session_panel.pyindex 4a62443d..ed1ecbd0 100644--- a/src/gui/main/main_window/middle_panel_viewers/welcome_view/welcome_create_or_load_new_session_panel.py+++ b/src/gui/main/main_window/middle_panel_viewers/welcome_view/welcome_create_or_load_new_session_panel.py@@ -55,9 +55,17 @@ class WelcomeCreateOrLoadNewSessionPanel(QWidget):self._load_session_button.setEnabled(True)self._layout.addWidget(self._load_session_button)- self._import_external_videos_button = QPushButton("Import External &Videos")- self._import_external_videos_button.setEnabled(True)- self._layout.addWidget(self._import_external_videos_button)+ self._import_synchronized_videos_button = QPushButton(+ "&Import Synchronized Videos (Ctrl+I)"+ )+ self._import_synchronized_videos_button.setEnabled(True)+ self._import_synchronized_videos_button.setToolTip(+ "Import videos that were recorded and synchronized externally. Each video must have the *exact* same number of frames"+ )+ self._import_synchronized_videos_button.clicked.connect(+ self.show_import_videos_view+ )+ self._layout.addWidget(self._import_synchronized_videos_button)send_pings_label = QLabel("(being able to show that people are using this thing will help us get funding for this project :D )"@@ -71,6 +79,7 @@ class WelcomeCreateOrLoadNewSessionPanel(QWidget):self._layout.addWidget(send_pings_label)self._layout.addStretch()+# show this if user selects 'new session' buttonself._session_id_form_layout = self._create_get_session_id_form_layout()self._auto_detect_cameras_checkbox = QCheckBox("Automatically detect cameras")@@ -84,6 +93,20 @@ class WelcomeCreateOrLoadNewSessionPanel(QWidget):self._start_session_button = QPushButton("Start Session \U00002728")self._start_session_button.setStyleSheet(recommended_next_button_style_sheet)+ # show this if user selects 'import videos' button+ self._synchronize_videos_checkbox = QCheckBox(+ "TODO - Synchronized videos using audio channels"+ )+ self._synchronize_videos_checkbox.setChecked(False)+ self._synchronize_videos_checkbox.setEnabled(False)++ self._launch_synchronized_videos_selection_dialog_button = QPushButton(+ "Select set of synchronized videos..."+ )+ self._launch_synchronized_videos_selection_dialog_button.setStyleSheet(+ recommended_next_button_style_sheet+ )+@propertydef create_new_session_button(self):return self._create_new_session_button@@ -100,10 +123,6 @@ class WelcomeCreateOrLoadNewSessionPanel(QWidget):def load_session_button(self):return self._load_session_button- @property- def import_external_videos_button(self):- return self._import_external_videos_button-@propertydef send_pings_checkbox(self):return self._send_pings_checkbox@@ -120,6 +139,10 @@ class WelcomeCreateOrLoadNewSessionPanel(QWidget):def auto_connect_to_cameras_checkbox(self):return self._auto_connect_to_cameras_checkbox+ @property+ def synchronized_videos_selection_dialog_button(self):+ return self._launch_synchronized_videos_selection_dialog_button+def _welcome_to_freemocap_title(self):# TO DO - this shouldn't be part of the `camera_view_panel` - it should be its own thing that gets swapped out on session startlogger.info("Creating `welcome to freemocap` widget")@@ -159,3 +182,16 @@ class WelcomeCreateOrLoadNewSessionPanel(QWidget):self._layout.addWidget(self._start_session_button)self._start_session_button.setFocus()self._layout.addStretch()++ def show_import_videos_view(self):+ hide_all_in_layout(self._layout)+ self._layout.addStretch()+ self._layout.addWidget(+ QLabel("Create a session folder with external recorded/synchronized videos")+ )+ self._layout.addLayout(self._session_id_form_layout)++ self._layout.addWidget(self._synchronize_videos_checkbox)+ self._layout.addWidget(self._launch_synchronized_videos_selection_dialog_button)+ self._launch_synchronized_videos_selection_dialog_button.setFocus()+ self._layout.addStretch()