Skip to content

Free Motion Capture for Everyone πŸ’€βœ¨

TypeScript

hacky solution to mostly-sync videos (but ensure they have the same number of frames)

Jon5d40c272f960
diff --git a/src/cameras/save_synchronized_videos.py b/src/cameras/save_synchronized_videos.py
new file mode 100644
index 00000000..4803441e
--- /dev/null
+++ b/src/cameras/save_synchronized_videos.py
@@ -0,0 +1,65 @@
+from typing import List
+
+import numpy as np
+from matplotlib import pyplot as plt
+
+from src.cameras.persistence.video_writer.video_recorder import VideoRecorder
+
+
+def save_synchronized_videos(list_of_video_recorders:List[VideoRecorder], calibration_videos:bool=False):
+ each_cam_frame_list = []
+ first_frame_timestamps = []
+ final_frame_timestamps = []
+
+ for video_recoder in list_of_video_recorders:
+ cam_frame_list = video_recoder.frame_list
+ # first_frame_timestamps.append(cam_frame_list[0].timestamp_in_seconds_from_record_start)
+ # final_frame_timestamps.append(cam_frame_list[-1].timestamp_in_seconds_from_record_start)
+ first_frame_timestamps.append(cam_frame_list[0].timestamp_unix_time_seconds)
+ final_frame_timestamps.append(cam_frame_list[-1].timestamp_unix_time_seconds)
+ each_cam_frame_list.append(cam_frame_list)
+
+ latest_first_frame = np.max(first_frame_timestamps)
+ earliest_final_frame = np.min(final_frame_timestamps)
+
+ print(f"first_frame_timestamps: {first_frame_timestamps}")
+ print(f"np.diff(first_frame_timestamps): {np.diff(first_frame_timestamps)}")
+ print(f"latest_first_frame: {latest_first_frame}")
+
+ print(f"final_frame_timestamps: {final_frame_timestamps}")
+ print(f"np.diff(final_frame_timestamps): {np.diff(final_frame_timestamps)}")
+ print(f"earliest_final_frame: {earliest_final_frame}")
+
+ each_cam_clipped_frame_list = []
+ each_cam_clipped_timestamp_list = []
+ for og_frame_list in each_cam_frame_list:
+ each_cam_clipped_frame_list.append([])
+ each_cam_clipped_timestamp_list.append([])
+ for frame in og_frame_list:
+ if frame.timestamp_unix_time_seconds < latest_first_frame:
+ continue
+ if frame.timestamp_unix_time_seconds > earliest_final_frame:
+ continue
+
+ each_cam_clipped_frame_list[-1].append(frame)
+ each_cam_clipped_timestamp_list[-1].append(frame.timestamp_unix_time_seconds)
+
+ number_of_frames_per_camera = [len(f) for f in each_cam_clipped_frame_list]
+ min_number_of_frames = np.min(number_of_frames_per_camera)
+ print(f" (clipped) number_of_frames_per_camera: {number_of_frames_per_camera}, min:{min_number_of_frames}")
+
+ each_cam_not_really_synchronized_frame_list = [frame_list[:min_number_of_frames] for frame_list in each_cam_clipped_frame_list]
+
+ number_of_frames_per_camera = [len(f) for f in each_cam_not_really_synchronized_frame_list]
+ print(f" (not_really_synchronized) number_of_frames_per_camera: {number_of_frames_per_camera}")
+
+ final_frame_timestamps = [frame_list[-1].timestamp_unix_time_seconds for frame_list in each_cam_not_really_synchronized_frame_list]
+
+ print(f"np.diff(final_frame_timestamps): {np.diff(final_frame_timestamps)}")
+
+ for video_recoder, frame_list in zip(list_of_video_recorders, each_cam_not_really_synchronized_frame_list):
+ video_recoder.save_list_of_frames_to_video_file(list_of_frames=frame_list, calibration_videos=calibration_videos)
+
+
+
+
diff --git a/src/freemocap_qt_gui/conference/workers/cam_charuco_frame_worker.py b/src/freemocap_qt_gui/conference/workers/cam_charuco_frame_worker.py
index 63fbe9b9..4ffeb0c7 100644
--- a/src/freemocap_qt_gui/conference/workers/cam_charuco_frame_worker.py
+++ b/src/freemocap_qt_gui/conference/workers/cam_charuco_frame_worker.py
@@ -27,6 +27,9 @@ class CamCharucoFrameWorker(QThread):
def should_save_frames(self, value):
self._should_save_frames = value
+ @property
+ def video_recorder(self):
+ return self._cam.video_recorder
def run(self):
cam = OpenCVCamera(
@@ -66,10 +69,10 @@ class CamCharucoFrameWorker(QThread):
finally:
print(f"Closing the camera {self._cam.webcam_id_as_str}, and saving video to disk")
self._cam.close()
- if any_frames_recorded:
- print(f"saving video for camera {self._cam.webcam_id_as_str}")
- self._cam.video_recorder.save_list_of_frames_to_video_file(calibration_videos=True)
- print(f"Saved video to: {str(self._cam.video_recorder.path_to_save_video_file)}")
+ # if any_frames_recorded:
+ # print(f"saving video for camera {self._cam.webcam_id_as_str}")
+ # self._cam.video_recorder.save_list_of_frames_to_video_file(calibration_videos=True)
+ # print(f"Saved video to: {str(self._cam.video_recorder.path_to_save_video_file)}")
def quit(self):
self._cam.close()
diff --git a/src/freemocap_qt_gui/conference/workflows/show_cams_charuco.py b/src/freemocap_qt_gui/conference/workflows/show_cams_charuco.py
index 53014bf2..edf81761 100644
--- a/src/freemocap_qt_gui/conference/workflows/show_cams_charuco.py
+++ b/src/freemocap_qt_gui/conference/workflows/show_cams_charuco.py
@@ -1,5 +1,6 @@
from PyQt6.QtWidgets import QHBoxLayout, QLabel, QVBoxLayout, QWidget, QPushButton
+from src.cameras.save_synchronized_videos import save_synchronized_videos
from src.freemocap_qt_gui.conference.workflows.single_camera import SingleCamera
from src.freemocap_qt_gui.refactored_gui.state.app_state import APP_STATE
from src.pipelines.calibration_pipeline.calibration_pipeline_orchestrator import CalibrationPipelineOrchestrator
@@ -49,9 +50,12 @@ class ShowCamsCharuco(QWidget):
cam.should_record_frames = True
def _stop_recording_frames(self):
+ video_recorders = []
for cam in self._cam_widgets:
+ video_recorders.append(cam.video_recorder)
cam.quit()
+ save_synchronized_videos(video_recorders, calibration_videos=True)
self._run_anipose_calibration()
def _run_anipose_calibration(self):
@@ -63,4 +67,4 @@ class ShowCamsCharuco(QWidget):
pin_camera_0_to_origin=True)
except:
print('something failed in the anipose calibration')
- raise Exception
\ No newline at end of file
+ raise Exception
diff --git a/src/freemocap_qt_gui/conference/workflows/single_camera.py b/src/freemocap_qt_gui/conference/workflows/single_camera.py
index 8d4a3215..f91e72b7 100644
--- a/src/freemocap_qt_gui/conference/workflows/single_camera.py
+++ b/src/freemocap_qt_gui/conference/workflows/single_camera.py
@@ -45,6 +45,10 @@ class SingleCamera(QWidget):
def should_record_frames(self, value):
self._worker.should_save_frames = value
+ @property
+ def video_recorder(self):
+ return self._worker.video_recorder
+
def capture(self):
self._create_preview_worker()
self.started.emit()