Free Motion Capture for Everyone πβ¨
TypeScript
i don't know if this is the right move? Am I deleting a bunch of stuff?
Jon PC Win Hallway3e2e6f2f99f6
diff --git a/freemocap/dev_scratchpad/dev_alpha_old/FMC_Camera.py b/freemocap/dev_scratchpad/dev_alpha_old/FMC_Camera.pydeleted file mode 100644index 3c1131a7..00000000--- a/freemocap/dev_scratchpad/dev_alpha_old/FMC_Camera.py+++ /dev/null@@ -1,374 +0,0 @@-##-##-## βββββββ βββ βββ ββββββ ββββββ βββββ βββ βββ βββββββ ββββββ βββββ-## ββ ββββ ββββ ββ ββ ββ ββ ββββ ββββ ββ ββ ββ ββ ββ-## βββββ ββ ββββ ββ ββ ββ βββββββ ββ ββββ ββ βββββ ββββββ βββββββ-## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-## ββ ββ ββ ββββββ ββββββ ββ ββ ββ ββ βββββββ ββ ββ ββ ββ-##-##-#Font - ANSI Regular - https://patorjk.com/software/taag/#p=display&f=ANSI%20Regular&t=Play%20Skeleton%20Animation----import platform-from pathlib import Path-import time--import cv2-import numpy as np--#python rich stuff - https://rich.readthedocs.io/-from rich import print-from rich.console import Console----cap_default_parameters_dict = {- 'exposure': -5,- 'cap_resolution_width': 1280,- 'cap_resolution_height': 720,- }--class FMC_Camera:- """ Class to open a cv2.VideoCapture object, with parts to connect to a `FMC_MutliCam` object as part of a FMC_Session.- Note, this class was made in service of the needs of the FreeMoCap project, at some point I'd love to develop this into a fully generic and useful 'camera' object, right now it's designed with that application (and only that application) in mind- """- ##- ##- ## ββ βββ ββ ββ ββββββββ- ## ββ ββββ ββ ββ ββ- ## ββ ββ ββ ββ ββ ββ- ## ββ ββ ββ ββ ββ ββ- ## βββββββ βββββββ ββ ββ ββββ ββ ββ βββββββ βββββββ- ##- ##- ##-- def __init__(- self,- cam_num=0,- rotation_code=None,- frame_queue = None,- barrier = None,- exit_event = None,- show_cam_stream = False,- rich_console = None,- show_console = False,- cap_parameters_dict = cap_default_parameters_dict,- vid_save_path=None,- ):- """Open a camera stream using `cv2.VideoCapture()`.- If `Windows`, use `cv2.VideoCapture(camNum, cv2.CAP_DSHOW)` to make video initialize much faster. Otherwise, use `cv2.VideoCapture(camNum, cv2.CAP_ANY)-- Args:- camNum (int, optional): The ID of the camera to open. Defaults to 0 (i.e. the first camera it finds)- vid_save_path (pathlib Path, optional): Where to save the video. Defaults to None.- barrier (threading.Barrier, optional): Barrier for syncronized multicam recording. Defaults to None.- frame_queue (queue.Queue, optional): [description]. Queue to send out images when we're recording in a thread.- """-- self._cam_num = cam_num- self._cam_name = 'Camera_{}'.format(str(self._cam_num).zfill(2))- self._cam_short_name = 'cam{}'.format(self._cam_name)-- self._vid_cap_start_time_unix = None-- self._cap_parameters_dict = cap_parameters_dict-- self._rotation_code = rotation_code-- self._frame_queue = frame_queue- self._barrier = barrier- self._exit_event = exit_event- self._vid_save_path = vid_save_path- self._show_console = show_console- self.rich_console = rich_console # a console object from the Rich python package--- if vid_save_path:- self._vid_save_path = Path(vid_save_path)--- if not self.rich_console:- self.rich_console = Console()-- self.open() #open VideoCapture Object-- self._show_cam_stream = show_cam_stream- if self._show_cam_stream:- self.show()#display camera stream-- if self._frame_queue:- self.run_in_thread() #launch in thread (or process?) mode- ##- ## ββββββ ββββββ ββββββ ββββββ βββββββ ββββββ ββββββββ ββ βββββββ βββββββ- ## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ## ββββββ ββββββ ββ ββ ββββββ βββββ ββββββ ββ ββ βββββ βββββββ- ## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ## ββ ββ ββ ββββββ ββ βββββββ ββ ββ ββ ββ βββββββ βββββββ- ##- ##- @property- def cam_num(self):- return self._cam_num-- @property- def cam_name(self):- return self._cam_name-- @property- def cam_short_name(self):- return self._cam_short_name-- @property- def vid_save_path(self):- return self._vid_save_path-- @property- def show_cam_stream(self):- return self._show_cam_stream-- @show_cam_stream.setter- def show_cam_stream(self, input_bool):- self._show_cam_stream = input_bool-- @property- def vid_cap_start_time_unix(self):- return self._vid_cap_start_time_unix-- @property- def vid_cap_timestamps_unix_ns(self): #not really a property though?- return self._vid_cap_timestamps_unix_ns-- @property- def show_console(self):- return self._show_console-- @show_console.setter- def show_console(self, input_bool):- self._show_console = input_bool-- ###- ###- ### βββ βββ βββββββ ββββββββ ββ ββ ββββββ ββββββ βββββββ βββ βββ βββββ βββ ββ- ### ββββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββββ ββ ββ ββββ ββ- ### ββ ββββ ββ βββββ ββ βββββββ ββ ββ ββ ββ βββββββ ββ ββββ ββ βββββββ ββ ββ ββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ βββββββ ββ ββ ββ ββββββ ββββββ βββββββ ββ ββ ββ ββ ββ ββββ- ###- ###- ###-- def show_cam_stream(self, show_cam_stream_bool=True):- """whether to open a window showing camera stream-- calls `self.show()` when set to `True`- and calls `self.close()` when set to `False` (b/c I'm too lazy to figure out how to make ti close th the display without closing the capture right now, sorry lol)-- Args:- show_cam_stream_bool (Bool): whether to show stream (default)-- Returns:- self- """- assert type(show_cam_stream_bool) == type(True), 'Input to `show_cam_stream` must be a bool'- self._show_cam_stream = show_cam_stream_bool-- if self._show_cam_stream:- self.show()- else:- self.close()-- return self._show_cam_stream- ###- ### ββββββ ββββββ βββββββ βββ ββ ββ ββ ββ ββββββ ββββββ βββββ ββββββ- ### ββ ββ ββ ββ ββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ ββββββ βββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ βββββββ ββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββββββ ββ βββββββ ββ ββββ ββββ ββ ββββββ ββββββ ββ ββ ββ- ###- ###- def open(self):- """- Open Video Capture Object and dispaly in little video- """-- if platform.system() == 'Windows':- self.cv2_cap = cv2.VideoCapture(self._cam_num, cv2.CAP_DSHOW)- else:- self.cv2_cap = cv2.VideoCapture(self._cam_num, cv2.CAP_ANY)-- self.cv2_cap.set(cv2.CAP_PROP_EXPOSURE, self._cap_parameters_dict['exposure'])- self.cv2_cap.set(cv2.CAP_PROP_FRAME_WIDTH, self._cap_parameters_dict['cap_resolution_width'])- self.cv2_cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self._cap_parameters_dict['cap_resolution_height'])-- self.cv2_cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))--- self.video_exposure = self.cv2_cap.get(cv2.CAP_PROP_EXPOSURE)- self.video_resolution_width = self.cv2_cap.get(cv2.CAP_PROP_FRAME_WIDTH)- self.video_resolution_height = self.cv2_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)-- if not self.cv2_cap.isOpened():- self.rich_console.log("Camera# "+str(self._cam_num)+" failed to open :(")-- else:- self.rich_console.log("Camera# "+str(self._cam_num)+" started - Exposure:({}) - Resolution(width, height):({},{}) ".format(self.video_exposure, self.video_resolution_width, self.video_resolution_height))- self._vid_cap_start_time_unix = time.time_ns() #the precision is aspirational, lol- self._vid_cap_timestamps_unix_ns = np.empty(0)-- ###- ### βββββββ ββ ββ ββββββ ββ ββ ββ ββ ββ ββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### βββββββ βββββββ ββ ββ ββ β ββ ββ ββ ββ ββ ββ- ### ββ ββ ββ ββ ββ ββ βββ ββ ββ ββ ββ ββ ββ- ### βββββββ ββ ββ ββββββ βββ βββ ββββ ββ ββββββ- ###-- def show(self):- """- display the camera stream, press ESC to close (and release the `cv2_cap`)- """- success, image, timestamp = self.read_next_frame()-- while success:- cv2.imshow(self._cam_name, image)-- success, image, timestamp = self.read_next_frame()-- console_msg = self.cam_name + " read an image at {:.4f}".format(timestamp)- self.update_console(console_msg)- self._vid_cap_timestamps_unix_ns = np.append(self.vid_cap_timestamps_unix_ns, timestamp)-- if self.wait_key() == 27: # exit on ESC- self.close()-- if cv2.getWindowProperty(self._cam_name, cv2.WND_PROP_VISIBLE) < 1: #break loop if window closed- break- cv2.destroyAllWindows()-- ###- ### ββββββ ββ ββ βββ ββ ββ βββ ββ ββββββββ ββ ββ ββββββ βββββββ βββββ ββββββ- ### ββ ββ ββ ββ ββββ ββ ββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ βββββββ ββββββ βββββ βββββββ ββ ββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ ββββββ ββ ββββ ββ ββ ββββ ββ ββ ββ ββ ββ βββββββ ββ ββ ββββββ- ###- ###- def run_in_thread(self):- """run camera in a thread- put incoming images into an tuple containing camNum_image_timestamp_tuple and stuff it into `self._frame_queue`- """- # with self.rich_console.status('Camera# {} is running'.format(self.cam_num)):- while not self._exit_event.is_set():- try:- success, image, timestamp = self.read_next_frame()- cam_image_timestamp_tuple = (self.cam_num, image, timestamp)-- self._frame_queue.put(cam_image_timestamp_tuple) #stuff this frame tuple packet into this camera's Queueue object-- # log_msg = self.cam_name+" got a frame at timestamp:"+ str(timestamp) + ' queue size: ' + str(self._frame_queue.qsize())- # self.rich_console.log(log_msg)-- self._barrier.wait() #wait until other cams have grabbed a frame (and the frame_grabber has frame grabbed them)- except:- self.rich_console.print_exception()-- #shut down when 'exit_event' is tripped- self.close()--- def run_in_subprocess(self):- self.run_in_thread()-- ###- ###- ### βββββββ ββββββββ ββββββ- ### ββ ββ ββ- ### βββββ ββ ββ- ### ββ ββ ββ- ### βββββββ ββ ββββββ ββ ββ ββ- ###- ###---- def __enter__(self):- """Context manager - No need to do anything special on start"""- return self-- def __exit__(self, exc_type, exc_value, exc_traceback):- """Context manager - close `self.cv2_cap` on exit"""- self.close()- return self-- def update_console(self, console_msg):- if self.show_console:- self.rich_console.log(console_msg) #there should be a way to log this without printing it, but I don't know how....-- def read_next_frame(self):- """ Grab next frame, return `Tuple(success[bool, True], image, timestamp)` on success or `Tuple(success[bool, False], False, False)` on failure-- Returns:- Success[bool]: True if frame image captured successfully- image: the image that was recieved from the VideoCapture object- timestamp: unix timestamp from time.time_ns()- """- success, image = self.cv2_cap.read()- timestamp = time.time_ns() #the precision is aspirational, lol- if success:- self._vid_cap_timestamps_unix_ns = np.append(self._vid_cap_timestamps_unix_ns, time.time_ns()) #the precision is aspirational, lol-- if self._rotation_code is None:- pass- elif self._rotation_code == 'cv2.ROTATE_90_CLOCKWISE':- image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)- elif self._rotation_code == 'cv2.ROTATE_90_COUNTERCLOCKWISE':- image = cv2.rotate(image, cv2.ROTATE_90_COUNTERCLOCKWISE)- elif self._rotation_code == 'cv2.ROTATE_180':- image = cv2.rotate(image, cv2.ROTATE_180)- else:- Exception, 'Invalid rotation code entered for FMC_Camera: {}'.format(self.cam_name)-- return success, image, timestamp- else:- return success, success, success-- def close(self):- """- close the video- """- self.cv2_cap.release()- self.rich_console.log("Camera# "+str(self._cam_num)+" shutting down")--- def wait_key(self):-- if self.show_cam_stream:- self._wait_key = cv2.waitKey(1)- else:- self._wait_key = 27-- return self._wait_key---###-### ββ βββββββ βββ βββ βββββ ββ βββ ββ-### ββ ββ ββββ ββββ ββ ββ ββ ββββ ββ-### ββ βββββ ββ ββββ ββ βββββββ ββ ββ ββ ββ-### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-### ββ ββ βββββββ βββββββ ββ ββ ββ ββ ββ ββ ββββ βββββββ βββββββ-###-###-if __name__ == '__main__':-- with FMC_Camera() as cam:- console = Console() #create Rich console to catch and print exceptions- try:- while True:- cam.show()- break- except Exception:- console.print_exception()-diff --git a/freemocap/dev_scratchpad/dev_alpha_old/FMC_MultiCamera.py b/freemocap/dev_scratchpad/dev_alpha_old/FMC_MultiCamera.pydeleted file mode 100644index 50eb6637..00000000--- a/freemocap/dev_scratchpad/dev_alpha_old/FMC_MultiCamera.py+++ /dev/null@@ -1,571 +0,0 @@-##-##-## βββββββ βββ βββ ββββββ βββ βββ ββ ββ ββ ββββββββ ββ ββββββ βββββ βββ βββ βββββββ ββββββ βββββ-## ββ ββββ ββββ ββ ββββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββββ ββ ββ ββ ββ ββ-## βββββ ββ ββββ ββ ββ ββ ββββ ββ ββ ββ ββ ββ ββ βββββ ββ βββββββ ββ ββββ ββ βββββ ββββββ βββββββ-## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-## ββ ββ ββ ββββββ ββ ββ ββββββ βββββββ ββ ββ ββββββ ββ ββ ββ ββ βββββββ ββ ββ ββ ββ-##-##-#Font - ANSI Regular - https://patorjk.com/software/taag/#p=display&f=ANSI%20Regular&t=Play%20Skeleton%20Animation---from rich.repr import T-from FMC_Camera import FMC_Camera---import threading-import pathos.multiprocessing as pathos_mp-from pathos.helpers import mp as pathos_mp_helper-import time-import platform-import datetime--import cv2-import matplotlib.pyplot as plt-import numpy as np-from pathlib import Path--from rich import print-from rich import inspect-from rich.progress import track-from rich.traceback import install-install(show_locals=True)-from rich.console import Console-rich_console = Console()-from rich.progress import track---class FMC_MultiCamera:- ##- ##- ## ββ βββ ββ ββ ββββββββ- ## ββ ββββ ββ ββ ββ- ## ββ ββ ββ ββ ββ ββ- ## ββ ββ ββ ββ ββ ββ- ## βββββββ βββββββ ββ ββ ββββ ββ ββ βββββββ βββββββ- ##- ##- ##- def __init__(- self,- rec_name = None,- freemocap_data_folder = None,- num_cams = 0,- cams_to_use_list = None,- rotation_codes_list = None,- show_multi_cam_stream_bool = True,- save_multi_cam_to_mp4 = False,- save_each_cam_to_mp4 = True,- save_to_h5 = False,- save_log_file = False- ):-- self._init_start_time = time.time_ns()#the precision is aspirational, lol-- if rec_name is None:- self._rec_name = datetime.datetime.now().strftime("sesh_%Y-%m-%d_%H_%M_%S_mc")- else:- self._rec_name = rec_name--- if freemocap_data_folder is None:- self._save_path = Path('data/' + self._rec_name)- else:- self._save_path = Path(freemocap_data_folder) / self._rec_name-- if freemocap_data_folder or save_multi_cam_to_mp4 or save_to_h5 or save_log_file or save_each_cam_to_mp4:- self._save_path.mkdir(parents=True, exist_ok=True)- self._path_to_log_file = Path(str(self._save_path / self._rec_name) + '_log.txt')-- self._save_multi_cam_to_mp4 = save_multi_cam_to_mp4- if self._save_multi_cam_to_mp4:- self._output_multi_cam_video_object = None #build this after we get the first multi_frame_image-- self._save_each_cam_to_mp4 = save_each_cam_to_mp4- if self._save_each_cam_to_mp4:- self._each_cam_video_writer_object_list = None #build this after we get the first multi_frame_image--- # log_file_obj = open(self._path_to_log_file,'w+')- # rich_console = Console(file=log_file_obj)- rich_console = Console()-- if cams_to_use_list:- self._num_cams = len(cams_to_use_list)- else:- self._num_cams = num_cams- self._cams_to_use_list = cams_to_use_list--- self._rotation_codes_list = rotation_codes_list--- self._show_multi_cam_stream_bool = show_multi_cam_stream_bool-- self.num_frames = 0-- rich_console.rule('Starting FreeMoCap MultiCam!')------ ###- ###- ### ββββββ ββββββ ββββββ ββββββ βββββββ ββββββ ββββββββ ββ βββββββ βββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββββββ ββββββ ββ ββ ββββββ βββββ ββββββ ββ ββ βββββ βββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ ββ ββββββ ββ βββββββ ββ ββ ββ ββ βββββββ βββββββ- ###- ###-- @property- def num_cams(self):- """How many cameras to use"""- return self._num_cams-- @num_cams.setter- def num_cams(self, num_cams_input):- """Define how many cameras to use"""- self._num_cams = num_cams_input-- @property- def cams_to_use_list(self):- """ a list denoting which camera IDs to use.- If both this and `num_cams` are set to default value (`0` and `None`, respectively), use all available cameras).- """- return self._cams_to_use_list-- @property- def init_start_time(self):- """Time that this object was initialized (UNIX Epoch time from `time.time_ns())-- Returns:- float: time this object was created (UNIX Epoch time from `time.time_ns())- """- return self._init_start_time-- @property- def each_cam_timestamps_unix_ns(self): #not really a property though?- return self._each_cam_timestamps_unix_ns- ###- ###- ### βββ βββ βββββββ ββββββββ ββ ββ ββββββ ββββββ βββββββ- ### ββββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββββ ββ βββββ ββ βββββββ ββ ββ ββ ββ βββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ βββββββ ββ ββ ββ ββββββ ββββββ βββββββ-- #################################################################- ###- ###- ### βββββββ ββββββββ βββββ ββββββ ββββββββ- ### ββ ββ ββ ββ ββ ββ ββ- ### βββββββ ββ βββββββ ββββββ ββ- ### ββ ββ ββ ββ ββ ββ ββ- ### βββββββ ββ ββ ββ ββ ββ ββ- ###- ###- ###- ###- def start(self, standalone_mode=False):- self.standalone_mode = standalone_mode- if self.cams_to_use_list is None:- self.find_available_cameras()--- # pathos_pool = ProcessPool(nodes=1)- # pathos_pool.map(self.start_multi_cam_thread_pool)- # self.start_multi_cam_thread_pool() #threads are mostly fine, but I think processes will be better eventually?- self.start_multi_cam_process_pool()- if standalone_mode:- self.create_diagnostic_images()-- ##- ##- ## βββββββ ββ βββ ββ ββββββ ββββββ βββββ βββ βββ βββββββ- ## ββ ββ ββββ ββ ββ ββ ββ ββ ββ ββββ ββββ ββ- ## βββββ ββ ββ ββ ββ ββ ββ ββ βββββββ ββ ββββ ββ βββββββ- ## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ## ββ ββ ββ ββββ ββββββ ββββββ ββ ββ ββ ββ βββββββ- ##- ##- ##-- def find_available_cameras(self):- """find available webcams and return IDs in list."""-- if self.num_cams == 0:- num_cams_to_check = 20 #man, i HOPE you're out there trying to record from more than 20 cameras!!!- else:- num_cams_to_check = self.num_cams-- if platform.system() == 'Windows':- capBackend = cv2.CAP_DSHOW- else:- capBackend = cv2.CAP_ANY-- self._cams_to_use_list = []- for camNum in range(num_cams_to_check):#track(range(num_cams_to_check), description='[green]Finding available cameras...' ):- # print('ATTEMPTING TO OPEN CAMERA AT PORT# {}'.format(camNum))- cap = cv2.VideoCapture(camNum, capBackend)- success, image = cap.read()- if success: #this is apparently a more power check than `cap.isOpened()` which sometimes returns false positives for unknown reason. Will lose the first frame if this method is ever applied to reading in videos instead of streaming from cameras- # rich_console.print('SUCCESS - CAMERA FOUND at # {}'.format(camNum), style="bold cyan")- self._cams_to_use_list.append(camNum)- cap.release()- else:- pass- # rich_console.print('No CAMERA FOUND at # {}'.format(camNum), style="magenta")-- rich_console.print("Found cameras at ", self._cams_to_use_list)-- self.num_cams = len(self._cams_to_use_list)- return self.cams_to_use_list--###-###-### βββββββ ββββββββ βββββ ββββββ ββββββββ ββββββ ββββββ ββββββ ββββββ βββββββ βββββββ βββββββ ββββββ ββββββ ββββββ ββ-### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-### βββββββ ββ βββββββ ββββββ ββ ββββββ ββββββ ββ ββ ββ βββββ βββββββ βββββββ ββββββ ββ ββ ββ ββ ββ-### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-### βββββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββββ ββββββ βββββββ βββββββ βββββββ ββ ββββββ ββββββ βββββββ-###-###-###- def start_multi_cam_process_pool(self):- """- reates a ProcessPoolExecutor that creates an `FMC_Camera` object for each camera in `self.cams_to_use_list` and sets each to trigger `run_in_process` mode (which just passes to `run_in_thread` mode)-- """- mrManager = pathos_mp_helper.Manager()- self.cam_frame_queue = mrManager.Queue(maxsize=self.num_cams)- self.multi_cam_tuple_queue = mrManager.Queue()- self.barrier = mrManager.Barrier(self.num_cams+1) #each cam + one more for the frame grabber- self.exit_event = mrManager.Event()-- self._each_cam_timestamps_unix_ns = None- self._cam_process = [None] * self.num_cams-- rich_console.rule('starting multi_cam_process_pool')-- in_frame_q_list = [self.cam_frame_queue]*self.num_cams- in_barrier_list = [self.barrier]*self.num_cams- in_exit_event_list = [self.exit_event]*self.num_cams-- if self._rotation_codes_list is None:- self._rotation_codes_list = [None]*self.num_cams-- num_jobs = self.num_cams- with pathos_mp.ProcessingPool(num_jobs) as self.cam_process_pool:- self.cam_process_pool.amap(- FMC_Camera,- tuple(self.cams_to_use_list),- tuple(self._rotation_codes_list),- tuple(in_frame_q_list),- tuple(in_barrier_list),- tuple(in_exit_event_list),- )- self.incoming_tuple_grabber_thread = threading.Thread(target=self.grab_incoming_cam_tuples, name='Incoming Frame Tuple Grabber Thread')- self.incoming_tuple_grabber_thread.start()- if self._show_multi_cam_stream_bool or self.standalone_mode:- self.show_multi_cam_opencv()---- ###- ###- ### ββββββ ββββββ βββββ ββββββ ββ βββ ββ ββββββ ββββββ βββ βββ ββ βββ ββ ββββββ ββββββββ ββ ββ ββββββ ββ βββββββ βββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ ββ ββ ββ ββββ ββββ ββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ βββ ββββββ βββββββ ββββββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ ββ ββ ββ ββ ββ βββ ββ ββ ββ ββββββ ββ βββββ βββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββββββ ββ ββ ββ ββ ββββββ ββ ββ ββββ ββββββ ββββββ ββ ββ ββ ββ ββββ ββββββ ββ ββββββ ββ βββββββ βββββββ βββββββ- ###- ###--- def grab_incoming_cam_tuples(self):- """grab incoming cam_tuples, put them into a multi_cam_tuple and stuff that into the multi_cam_tuple_queue"""-- # self.show_multi_cam_cv2()-- while not self.exit_event.is_set():- self.barrier.wait() #wait until each camera has grabbed an image and tagged their `barrier.wait()` signals-- self.num_frames += 1-- these_images_list = [None]*self.num_cams #empty list of size (numCam)- this_multi_cam_tuple_as_a_list = [None]*self.num_cams- these_timestamps = np.ndarray(self.num_cams)-- for cam_num in range(self.num_cams):- this_cam_image_timestamp_tuple = self.cam_frame_queue.get()- this_cam_num = this_cam_image_timestamp_tuple[0]- this_cam_index = self.cams_to_use_list.index(this_cam_num)- these_images_list[this_cam_index] = this_cam_image_timestamp_tuple[1]- these_timestamps[this_cam_index] = this_cam_image_timestamp_tuple[2]- this_multi_cam_tuple_as_a_list[this_cam_index] = this_cam_image_timestamp_tuple-- this_multi_cam_tuple = tuple(this_multi_cam_tuple_as_a_list)-- if self._each_cam_timestamps_unix_ns is None:- self._each_cam_timestamps_unix_ns = these_timestamps- else:- self._each_cam_timestamps_unix_ns = np.vstack((self._each_cam_timestamps_unix_ns, these_timestamps)) #result will be numpy array with `num_frames` rows and `num_cams` columns-- self.multi_cam_tuple_queue.put(this_multi_cam_tuple, np.mean(these_timestamps))--- rich_console.log('Created a multi_cam_tuple - queue size: {}'.format(self.multi_cam_tuple_queue.qsize()))--- def stitch_multicam_image(self, multi_cam_tuple):- """Take in a multi-cam-tuple (containing images from each camera during the period of time defined by the attached timestamps)-- Args:- multi_cam_tuple (tuple of tuples): The multi_cam_tuple produced by `self.grab_incoming_cam_tuples()` comprising a timestamp and a tuple from each of the cameras. Each camera's tuple consists of the camera's ID/Number, the image frame, and the timestamp that frame was recorded. The multi-camtimestamp is the mean of each of the timestamps from the individual cameras.- Returns:- multi_cam_image: an image of size camera_res_width*num_cams x camera_res_height The images are synchronized within the precision defined by the timestamps included in the individual camera tuples.- """-- these_images_list = [None]*self.num_cams #empty list of size (numCam)---- for this_cam_num in range(self.num_cams):- these_images_list[this_cam_num] = multi_cam_tuple[this_cam_num][1] #and that's how you navigate nested tuples, lol-- multi_cam_image = np.hstack(these_images_list) #create multiFrame_image by stitching together (horizontally stacking) incoming camera images (matrices)- return multi_cam_image-- # rich_console.log('Created a multi_cam_image - queue size: {}'.format(self.multi_cam_tuple_queue.qsize()))-- ###- ###- ### βββββββ ββ ββ ββββββ ββ ββ ββ ββ ββ ββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### βββββββ βββββββ ββ ββ ββ β ββ ββ ββ ββ ββ ββ- ### ββ ββ ββ ββ ββ ββ βββ ββ ββ ββ ββ ββ ββ- ### βββββββ ββ ββ ββββββ βββ βββ ββββ ββ ββββββ- ###- ### 6-- def show_multi_cam_opencv(self):- """display multi_cam_image using `cv2.imshow` and maybe save it to mp4, who knows?- """- rich_console.rule('Launching Multi Cam Viewer')- while not self.exit_event.is_set():- if not self.multi_cam_tuple_queue.empty():-- this_multi_cam_tuple = self.multi_cam_tuple_queue.get()- self.save_synchronized_videos(this_multi_cam_tuple)- this_multi_cam_image = self.stitch_multicam_image(this_multi_cam_tuple)-- if self._save_multi_cam_to_mp4:- if self._output_multi_cam_video_object is None:- self.initialize_multi_cam_output_video(this_multi_cam_image)--- self._output_multi_cam_video_object.write(this_multi_cam_image)-- if self._save_each_cam_to_mp4:- if self._each_cam_video_writer_object_list is None:- self.initialize_each_cam_output_video(this_multi_cam_tuple)-- for this_cam_num in range(self.num_cams):- self._each_cam_video_writer_object_list[this_cam_num].write(this_multi_cam_tuple[this_cam_num][1])-- cv2.imshow(self._rec_name, this_multi_cam_image)- key = cv2.waitKey(1)-- if key == 27: # exit on ESC- break-- if cv2.getWindowProperty(self._rec_name, cv2.WND_PROP_VISIBLE) < 1: #break loop if window closed- break-- cv2.destroyAllWindows()-- if self._save_multi_cam_to_mp4:- self._output_multi_cam_video_object.release()-- if self._save_each_cam_to_mp4:- for this_cam_num in range(self.num_cams):- self._each_cam_video_writer_object_list[this_cam_num].release()-- rich_console.rule('Shutting down MultiCamera Viewer')- self.exit_event.set() #send the 'Exit' signal to everyone.--- def save_synchronized_videos(self, multi_cam_tuple):- """save camera streams into individual videos (that can be processed with pre-alpha freemocap"""- these_images_list = [None]*self.num_cams #empty list of size (numCam)-- for this_cam_num in range(self.num_cams):- this_cam_image = multi_cam_tuple[this_cam_num][1] #and that's how you navigate nested tuples, lol------ ###- ###- ### ββ βββ ββ ββ ββββββββ ββββββ ββ ββ ββββββββ ββ ββ ββ ββββββ- ### ββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ ββββ ββ ββ ββββββ ββββββ ββ ββββ ββ ββββββ- ###- ###-- def initialize_multi_cam_output_video(self, multi_cam_image):- """use the multi_cam_image to initialize the video- multicam image will be of size [cam_resolution_width*num_cams by cam_resolution_height]- Args:- multi_cam_image ([type]): [description]- """- #create video save object- self._multi_cam_image_height, self._multi_cam_image_width, channels = multi_cam_image.shape-- self.multi_cam_image_size = ( self._multi_cam_image_width, self._multi_cam_image_height)- fourcc = cv2.VideoWriter_fourcc(*'DIVX')- self._output_multi_cam_vid_fileName = str(self._save_path / self._rec_name) + '_outVid.mp4'- fps = 25 #this is a bad and stupid guess, but it's actually kinda tricky to guess what is the right thing to put here. I'll fix this later and expect videos to be a bit faster than usual :(- self._output_multi_cam_video_object = cv2.VideoWriter(self._output_multi_cam_vid_fileName, fourcc, fps, self.multi_cam_image_size)--- def initialize_each_cam_output_video(self, multi_cam_tuple):-- self._each_cam_video_writer_object_list=[None]*self.num_cams- self._each_cam_video_filename_list=[None]*self.num_cams-- self.syncedVidsPath = self._save_path / 'Synchronized_Videos'- self.syncedVidsPath.mkdir(parents=True)-- for this_cam_num in range(self.num_cams):- #create video save object- this_cam_image = multi_cam_tuple[this_cam_num][1]- this_cam_timestamp_unix_ns = multi_cam_tuple[this_cam_num][2]- self._cam_image_height, self._cam_image_width, channels = this_cam_image.shape-- self._cam_image_size = ( self._cam_image_width, self._cam_image_height)- fourcc = cv2.VideoWriter_fourcc(*'DIVX')-- this_cam_vid_filename = self._rec_name + '_Camera_'+str(this_cam_num)+'_synchronized.mp4'- self._each_cam_video_filename_list[this_cam_num] = self.syncedVidsPath / this_cam_vid_filename-- fps = 25 #this is a bad and stupid guess, but it's actually kinda tricky to guess what is the right thing to put here. I'll fix this later and expect videos to be a bit faster than usual :(- self._each_cam_video_writer_object_list[this_cam_num] = cv2.VideoWriter(str(self._each_cam_video_filename_list[this_cam_num]), fourcc, fps, self._cam_image_size)- ##- ##- ##- ## ββββββ ββ ββββββ ββββββββ ββββββββ ββ βββ βββ βββββββ βββββββ ββββββββ βββββ βββ βββ ββββββ βββββββ- ## ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββββ ββ ββ ββ ββ ββ ββββ ββββ ββ ββ ββ- ## ββββββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ βββββ βββββββ ββ βββββββ ββ ββββ ββ ββββββ βββββββ- ## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ## ββ βββββββ ββββββ ββ ββ ββ ββ ββ βββββββ βββββββ ββ ββ ββ ββ ββ ββ βββββββ- ##- ##-- def create_diagnostic_images(self):- """plot some diagnostics to assess quality of camera sync"""- try:- camTimestamps = (self.each_cam_timestamps_unix_ns-self.init_start_time)/1e9 #subtract start time and convert to sec-- npy_save_path = self._save_path /'multi_cam_timestamps_frameNum_camNum.npy'- np.save(str(npy_save_path), camTimestamps)- rich_console.log('saving timestamp data to - ' + str(npy_save_path) )-- meanMultiFrameTimestamp = np.mean(camTimestamps, axis=1)- meanMultiFrameTimespan = np.max(camTimestamps, axis=1) - np.min(camTimestamps, axis=1) #what was the timespan covered by each frame- plt.ion()- fig = plt.figure(figsize=(18,10))- max_frame_duration = .1- ax1 = plt.subplot(231, title='Camera Frame Timestamp vs Frame#', xlabel='Frame#', ylabel='Timestamp (sec)')- ax2 = plt.subplot(232, ylim=(0,max_frame_duration), title='Camera Frame Duration Trace', xlabel='Frame#', ylabel='Duration (sec)')- ax3 = plt.subplot(233, xlim=(0,max_frame_duration), title='Camera Frame Duration Histogram (count)', xlabel='Duration(s, 1ms bins)', ylabel='Probability')- ax4 = plt.subplot(234, title='MuliFrame Timestamp vs Frame#', xlabel='Frame#', ylabel='Timestamp (sec)')- ax5 = plt.subplot(235, ylim=(0,max_frame_duration), title='Multi Frame Duration/Span Trace', xlabel='Frame#', ylabel='Duration (sec)')- ax6 = plt.subplot(236, xlim=(0,max_frame_duration), title='MultiFrame Duration Histogram (count)', xlabel='Duration(s, 1ms bins)', ylabel='Probability')-- for camNum in range(self.num_cams):- thisCamTimestamps = camTimestamps[:,camNum]- ax1.plot(thisCamTimestamps, label='Camera#'+str(camNum))- ax1.legend()- ax2.plot(np.diff(thisCamTimestamps),'.')- ax3.hist(np.diff(thisCamTimestamps), bins=np.arange(0,max_frame_duration,.001), alpha=0.5)-- ax4.plot(meanMultiFrameTimestamp, color='darkslategrey', label='MultiFrame'+str(camNum))- ax5.plot(np.diff(meanMultiFrameTimestamp),'.',color='darkslategrey', label='Frame Duration')- ax5.plot(meanMultiFrameTimespan, '.', color='orangered', label='Frame TimeSpan')- ax5.legend()- ax6.hist(np.diff(meanMultiFrameTimestamp), bins=np.arange(0,max_frame_duration,.001), density=True, alpha=0.5, color='darkslategrey', label='Frame Duration')- ax6.hist(np.diff(meanMultiFrameTimespan), bins=np.arange(0,max_frame_duration,.001), density=True, alpha=0.5, color='orangered', label='Frame Timespan')- ax5.legend()-- fig_save_path = self._save_path / 'recording_diagnostics.png'- plt.savefig(str(fig_save_path))- rich_console.log('Saving diagnostic figure to - '+str(fig_save_path))- plt.show()- plt.waitforbuttonpress()- except:- rich_console.print_exception()- f=9-- ###- ### βββββββ ββββββββ ββββββ- ### ββ ββ ββ- ### βββββ ββ ββ- ### ββ ββ ββ- ### βββββββ ββ ββββββ ββ ββ ββ- ###-- def __enter__(self):- """Context manager - No need to do anything special on start"""- return self-- def __exit__(self, exc_type, exc_value, exc_traceback):- """Context manager - on exit, set `self.exit_event` to activate shutdown sequence """- self.create_diagnostic_images()- return self-###-###-### ββ βββββββ βββ βββ βββββ ββ βββ ββ-### ββ ββ ββββ ββββ ββ ββ ββ ββββ ββ-### ββ βββββ ββ ββββ ββ βββββββ ββ ββ ββ ββ-### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-### ββ ββ βββββββ βββββββ ββ ββ ββ ββ ββ ββ ββββ βββββββ βββββββ-###-###---if __name__ == '__main__':- pathos_mp_helper.freeze_support()- console = Console() #create rich console to catch and print exceptions-- import socket- this_computer_name = socket.gethostname()-- freemocap_data_path=None- in_rotation_codes_list=None-- if this_computer_name=='jon-hallway-XPS-8930':- freemocap_data_path = Path('/home/jon/Dropbox/FreeMoCapProject/FreeMocap_Data')- in_rotation_codes_list = ['cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', ]- elif this_computer_name == 'DESKTOP-DCG6K4F':- freemocap_data_path = Path(r'C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data')- elif this_computer_name == 'DESKTOP-V3D343U':- freemocap_data_path = Path(r'C:\Users\WindowsPC_Hallway\Dropbox\FreeMoCapProject\FreeMocap_Data')- elif this_computer_name == 'Jons-MacBook-Pro.local':- freemocap_data_path = Path('/Users/jon/Dropbox/FreeMoCapProject')-- try:- multi_cam = FMC_MultiCamera(freemocap_data_folder=str(freemocap_data_path), save_each_cam_to_mp4 = True, rotation_codes_list=in_rotation_codes_list)- multi_cam.start(standalone_mode=True)-- except Exception:- console.print_exception()--diff --git a/freemocap/dev_scratchpad/dev_alpha_old/FMC_Session.py b/freemocap/dev_scratchpad/dev_alpha_old/FMC_Session.pydeleted file mode 100644index 1298e23d..00000000--- a/freemocap/dev_scratchpad/dev_alpha_old/FMC_Session.py+++ /dev/null@@ -1,272 +0,0 @@-##-##-## βββββββ βββ βββ ββββββ βββββββ βββββββ βββββββ βββββββ ββ ββββββ βββ ββ-## ββ ββββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ-## βββββ ββ ββββ ββ ββ βββββββ βββββ βββββββ βββββββ ββ ββ ββ ββ ββ ββ-## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-## ββ ββ ββ ββββββ βββββββ βββββββ βββββββ βββββββ ββ ββββββ ββ ββββ-##-## https://patorjk.com/software/taag/#p=display&f=ANSI%20Regular&t=FMC%20Session--from FMC_MultiCamera import FMC_MultiCamera-from FMC_Visualizer import FMC_Visualizer--import datetime-from pathlib import Path-import pickle--from rich import print, inspect-from rich.console import Console-import numpy as np--from freemocap import fmc_anipose-from aniposelib.boards import CharucoBoard--rich_console = Console()---class FMC_Session:- ##- ##- ## ββ βββ ββ ββ ββββββββ- ## ββ ββββ ββ ββ ββ- ## ββ ββ ββ ββ ββ ββ- ## ββ ββ ββ ββ ββ ββ- ## βββββββ βββββββ ββ ββ ββββ ββ ββ βββββββ βββββββ- ##- ##-- def __init__(self, sessionID=None, freemocap_data_folder=None, rotation_codes_list=None):-- if freemocap_data_path is None:- user_home_directory = Path.home()- self.freemocap_data_path = user_home_directory /'FreeMoCap_Data'-- if self.freemocap_data_path.exists():- rich_console.log("FreeMoCap_Data folder found in User home directory - " + str(self.freemocap_data_path))- elif not self.freemocap_data_path.exists():- self.freemocap_data_path.mkdir(exist_ok=True)- rich_console.log("FreeMoCap_Data CREATED in User home directory - " + str(self.freemocap_data_path))- rich_console.rule("To create a FreeMoCap_Data folder in a different place, use `FMC_Session(freemocap_data_folder=\"path_to_folder\")`")- else:- self.freemocap_data_path = freemocap_data_path-- if sessionID is None:- self.sessionID = datetime.datetime.now().strftime("FreeMoCap_Session_%Y-%m-%d_%H_%M_%S")- else:- self.sessionID = sessionID-- ##- ## ββββββ βββββββ ββββββ βββ βββ ββ ββ ββ ββββββββ ββ ββββββ βββββ βββ βββ- ## ββ ββ ββ ββ ββββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββββ- ## ββββββ βββββ ββ ββ ββββ ββ ββ ββ ββ ββ ββ ββ βββββββ ββ ββββ ββ- ## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ## ββ ββ βββββββ ββββββ ββ ββ ββββββ βββββββ ββ ββ ββββββ ββ ββ ββ ββ- ##- ##-- def record_multi_cam(self,freemocap_data_folder=None, rotation_codes_list=None):- self.multi_cam = FMC_MultiCamera(freemocap_data_folder=str(freemocap_data_path), rotation_codes_list=in_rotation_codes_list, rec_name=self.sessionID )- self.multi_cam.start()-- ##- ##- ## ββββββ βββββ ββ ββ ββββββ ββββββ βββββ ββββββ ββ ββ ββββββ ββ- ## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ## ββ βββββββ ββ ββ ββββββ ββ βββββββ ββββββ ββ ββ ββ ββ ββ- ## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ## ββββββ ββ ββ βββββββ ββ ββββββ ββββββ ββ ββ ββ ββββ ββββββ βββββββ- ##- ##-- def camera_capture_volume_calibration(self, calibration_method="anipose_charuco", charucoSquareSize=36):-- num_charuco_rows = 7- num_charuco_cols = 5- num_charuco_tracked_points = (num_charuco_rows-1) * (num_charuco_cols-1)- board = CharucoBoard(num_charuco_rows, num_charuco_cols,- #square_length=1, # here, in mm but any unit works (JSM NOTE - just using '1' so resulting units will be in 'charuco squarelenghts`)- #marker_length=.8,- # square_length = 121, #big boi charuco- # marker_length = 98,- square_length = charucoSquareSize,#mm- marker_length = charucoSquareSize*.8,#mm- marker_bits=4, dict_size=250)---- self.synched_video_path_list = self.multi_cam._each_cam_video_filename_list-- video_paths_list_of_list_of_strings = []-- for this_path in self.synched_video_path_list:- video_paths_list_of_list_of_strings.append([str(this_path)])#anipose needs this to be a list of lists (which is annoying by whatevs)-- cam_names = ['cam_'+str(cam_num) for cam_num in range(len(self.synched_video_path_list))]-- self.anipose_camera_calibration = fmc_anipose.CameraGroup.from_names(cam_names, fisheye=True ) # Looking through their code... it looks lke the 'fisheye=True' doesn't do much (see 2020-03-29 obsidian note)- error,charuco_frame_data, charuco_frame_numbers = self.anipose_camera_calibration.calibrate_videos(video_paths_list_of_list_of_strings, board)-- print("Anipose Calibration Successful!")-- #save calibration info to files- calibration_toml_filename = "{}_camera_calibration.toml".format(self.multi_cam._rec_name)- self.camera_calibration_toml_path = self.multi_cam._save_path / calibration_toml_filename- self.anipose_camera_calibration.dump(self.camera_calibration_toml_path)-- camera_calibration_info_dict = self.anipose_camera_calibration.get_dicts()- camera_calibration_pickle_path = self.multi_cam._save_path / "{}_camera_calibration.pickle".format(self.multi_cam._rec_name)-- with open(str(camera_calibration_pickle_path), 'wb') as pickle_file:- pickle.dump(camera_calibration_info_dict, pickle_file)-- #convert charuco data into a format that can be 3d reconstructed (effectively providing dummy data for the rest of the 3d reconstruction pipeline)- self.charuco_nCams_nFrames_nImgPts_XY = np.empty([self.multi_cam.num_cams, self.multi_cam.num_frames, num_charuco_tracked_points, 2])- self.charuco_nCams_nFrames_nImgPts_XY[:] = np.nan--- for this_charuco_frame_data, this_charuco_frame_num in zip(charuco_frame_data,charuco_frame_numbers):- for this_cam_num in range(self.multi_cam.num_cams):- try:- self.charuco_nCams_nFrames_nImgPts_XY[this_cam_num, this_charuco_frame_num, :,:] = np.squeeze(this_charuco_frame_data[this_cam_num]["filled"])- except:- # print("failed frame:", frame)- continue- ####- ####- #### ββββββ βββββββ ββββββ ββββββ βββ ββ βββββββ ββββββββ ββββββ ββ ββ ββββββ ββββββββ ββββββ ββββββ- #### ββ ββ ββ ββ ββ ββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- #### ββββββ βββββ ββ ββ ββ ββ ββ ββ βββββββ ββ ββββββ ββ ββ ββ ββ βββββ ββ ββ- #### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- #### ββ ββ βββββββ ββββββ ββββββ ββ ββββ βββββββ ββ ββ ββ ββββββ ββββββ ββ ββββββ ββββββ- ####- ####-- def reconstruct3D(self, data_nCams_nFrames_nImgPts_XYC, calibration_toml_path = None, confidence_threshold=0.3):-- if calibration_toml_path: #if the user specified a calibration toml (from a previous Anipose-based calibration) - load it here. Otherwise, assume the calibration is included in `self`- self.anipose_camera_calibration = fmc_anipose.CameraGroup.load(str(calibration_toml_path))-- num_cams, num_frames, num_img_points, num_dims = data_nCams_nFrames_nImgPts_XYC.shape-- if num_dims == 3: #if there are 3 dimensions, the third one is 'confidence' and can be used to threshold out low-confidence tracks- dataOG = data_nCams_nFrames_nImgPts_XYC.copy()-- for camNum in range(num_cams):-- thisCamX = data_nCams_nFrames_nImgPts_XYC[camNum, :, :,0 ]- thisCamY = data_nCams_nFrames_nImgPts_XYC[camNum, :, :,1 ]- thisCamConf = data_nCams_nFrames_nImgPts_XYC[camNum, :, :, 2]-- thisCamX[thisCamConf < confidence_threshold] = np.nan- thisCamY[thisCamConf < confidence_threshold] = np.nan--- if num_dims == 2:- data_nCams_nFrames_nImgPts_XY = data_nCams_nFrames_nImgPts_XYC- elif num_dims == 3:- data_nCams_nFrames_nImgPts_XY = np.squeeze(data_nCams_nFrames_nImgPts_XYC[:, :, :, 0:2])-- dataFlat_nCams_nTotalPoints_XY = data_nCams_nFrames_nImgPts_XY.reshape(num_cams, -1, 2) # reshape data to collapse across 'frames' so it becomes [numCams, numFrames*numPoints, XY]-- console.print('Reconstructing 3d points...')- data3d_flat = self.anipose_camera_calibration.triangulate(dataFlat_nCams_nTotalPoints_XY, progress=True)-- dataReprojerr_flat = self.anipose_camera_calibration.reprojection_error( data3d_flat, dataFlat_nCams_nTotalPoints_XY, mean=True)-- ##return:- data_fr_mar_xyz = data3d_flat.reshape(num_frames, num_img_points, 3)- dataReprojErr = dataReprojerr_flat.reshape(num_frames, num_img_points)-- return data_fr_mar_xyz, dataReprojErr-- ###- ###- ### ββββββββ ββββββ βββββ ββββββ ββ ββ ββ βββ ββ ββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ ββ- ### ββ ββββββ βββββββ ββ βββββ ββ ββ ββ ββ ββ βββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ ββ ββ ββ ββββββ ββ ββ ββ ββ ββββ ββββββ- ###- ###- def run_tracking_methods(self, use_media_pipe_bool=True, reconstruct3D_bool = True, save_to_npy_bool=True):-- if use_media_pipe_bool:- from tracking_methods.fmc_mediapipe_alpha import run_mediapipe-- self.mediapipe_nCams_nFrames_nImgPts_XY = run_mediapipe(self.synched_video_path_list)--- if reconstruct3D_bool:- self.mediapipe_nFrames_nTrackedPoints_XYZ, self.mediapipe_reprojection_error = self.reconstruct3D(self.mediapipe_nCams_nFrames_nImgPts_XY)--- if save_to_npy_bool:- mediapipe3d_filename = self.multi_cam._save_path/'mediapipe_3d_points.npy'- np.save(mediapipe3d_filename,self.mediapipe_nFrames_nTrackedPoints_XYZ)-- f=9------ ###- ### βββββββ ββββββββ ββββββ- ### ββ ββ ββ- ### βββββ ββ ββ- ### ββ ββ ββ- ### βββββββ ββ ββββββ ββ ββ ββ- ###-- def __enter__(self):- """Context manager - No need to do anything special on start"""- return self-- def __exit__(self, exc_type, exc_value, exc_traceback):- """Context manager - no need to do anything special on stop (maybe set 'exit_event?" """- return self--###-###-### ββ βββββββ βββ βββ βββββ ββ βββ ββ-### ββ ββ ββββ ββββ ββ ββ ββ ββββ ββ-### ββ βββββ ββ ββββ ββ βββββββ ββ ββ ββ ββ-### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-### ββ ββ βββββββ βββββββ ββ ββ ββ ββ ββ ββ ββββ βββββββ βββββββ-###-###--if __name__ == "__main__":-- import socket- this_computer_name = socket.gethostname()-- console = Console()-- freemocap_data_path=None- in_rotation_codes_list=None- charucoSquareSize = 36-- if this_computer_name=='jon-hallway-XPS-8930':- freemocap_data_path = Path('/home/jon/Dropbox/FreeMoCapProject/FreeMocap_Data')- in_rotation_codes_list = ['cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', ]- elif this_computer_name == 'DESKTOP-DCG6K4F':- freemocap_data_path = Path(r'C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data')- this_charucoSquareSize = 123-- try:- sesh = FMC_Session(sessionID = None, freemocap_data_folder=str(freemocap_data_path), rotation_codes_list=in_rotation_codes_list)- sesh.record_multi_cam()- sesh.camera_capture_volume_calibration(charucoSquareSize = this_charucoSquareSize)- sesh.charuco_nFrames_nTrackedPoints_XYZ, sesh.charuco_reprojection_error = sesh.reconstruct3D(sesh.charuco_nCams_nFrames_nImgPts_XY)-- charuco3d_filename = sesh.multi_cam._save_path/'charuco_3d_points.npy'- np.save(charuco3d_filename,sesh.charuco_nFrames_nTrackedPoints_XYZ)-- sesh.run_tracking_methods()- visualizer = FMC_Visualizer(fmc_session_obj=sesh)- visualizer.start()-- except:- console.print_exception()\ No newline at end of filediff --git a/freemocap/dev_scratchpad/dev_alpha_old/FMC_Visualizer.py b/freemocap/dev_scratchpad/dev_alpha_old/FMC_Visualizer.pydeleted file mode 100644index 8e57aeef..00000000--- a/freemocap/dev_scratchpad/dev_alpha_old/FMC_Visualizer.py+++ /dev/null@@ -1,169 +0,0 @@-# -*- coding: utf-8 -*--"""-built off the '3D Graphics- Scatter Plot' example from `python -m pyqtgraph.examples`-"""-from pathlib import Path-import sys--import pyqtgraph as pg-from pyqtgraph import functions as fn-from pyqtgraph.Qt import QtCore, QtGui-import pyqtgraph.opengl as gl-import numpy as np---class FMC_Visualizer:- def __init__(self, fmc_session_obj = None, recording_session_path = None):-- #setup QT app window- self.app = pg.mkQApp("FreeMoCap Data Visualizer")- self.gl_view_widget = gl.GLViewWidget()- self.gl_view_widget.show()- self.gl_view_widget.setWindowTitle('FreeMoCap Data Visualizer')- self.gl_view_widget.setCameraPosition(distance=10, elevation=-90, azimuth=-90)-- grid_distance = 10- gl_x_plane_grid_item = gl.GLGridItem()- gl_x_plane_grid_item.rotate(90, 0, 1, 0)- gl_x_plane_grid_item.translate(grid_distance, 0, 0)- self.gl_view_widget.addItem(gl_x_plane_grid_item)-- gl_y_plane_grid_item = gl.GLGridItem()- gl_y_plane_grid_item.rotate(90, 1, 0, 0)- gl_y_plane_grid_item.translate(0, grid_distance, 0)- self.gl_view_widget.addItem(gl_y_plane_grid_item)-- gl_z_plane_grid_item = gl.GLGridItem()- gl_z_plane_grid_item.translate(0, 0, grid_distance)- self.gl_view_widget.addItem(gl_z_plane_grid_item)-- origin_location_array = np.array([0,0, 0])- x_axis_line_array = np.array([[0,0, 0], [1,0,0]])- y_axis_line_array = np.array([[0,0, 0], [0,1,0]])- z_axis_line_array = np.array([[0,0, 0], [0,0,1]])-- # self.origin_location_scatter_item = gl.GLScatterPlotItem(pos=origin_location_array, color=(1,1,1,1), size=.1, pxMode=False)- self.origin_x_axis_gl_lineplot_item = gl.GLLinePlotItem(pos=x_axis_line_array, color=(1,0,0,1), width=1., antialias=True)- self.origin_y_axis_gl_lineplot_item = gl.GLLinePlotItem(pos=y_axis_line_array, color=(0,1,0,1), width=1., antialias=True)- self.origin_z_axis_gl_lineplot_item = gl.GLLinePlotItem(pos=z_axis_line_array, color=(0,0,1,1), width=1., antialias=True)-- # self.gl_view_widget.addItem(self.origin_location_scatter_item)- self.gl_view_widget.addItem(self.origin_x_axis_gl_lineplot_item)- self.gl_view_widget.addItem(self.origin_y_axis_gl_lineplot_item)- self.gl_view_widget.addItem(self.origin_z_axis_gl_lineplot_item)-- if fmc_session_obj is None:- if recording_session_path:- self.recording_session_path = Path(recording_session_path)- else:- #JSM NOTE - add something to load the last recorded session if nothing is specified- pass-- # load charuco data- self.charuco_3d_path = self.recording_session_path / "charuco_3d_points.npy"- self.charuco_nFrames_nTrackedPoints_XYZ = np.load(str(self.charuco_3d_path))- self.charuco_nFrames_nTrackedPoints_XYZ = self.charuco_nFrames_nTrackedPoints_XYZ/1000 #convert to meters-- # load mediapipe data- self.mediapipe_3d_path = self.recording_session_path / "mediapipe_3d_points.npy"- self.mediapipe_nFrames_nTrackedPoints_XYZ = np.load(str(self.mediapipe_3d_path))- self.mediapipe_nFrames_nTrackedPoints_XYZ = self.mediapipe_nFrames_nTrackedPoints_XYZ/1000 #convert to meters- else:- # load charuco data- self.charuco_nFrames_nTrackedPoints_XYZ = fmc_session_obj.charuco_nFrames_nTrackedPoints_XYZ/1000 #convert to meters-- # load mediapipe data- self.mediapipe_nFrames_nTrackedPoints_XYZ = fmc_session_obj.mediapipe_nFrames_nTrackedPoints_XYZ/1000 #convert to meters-- self.frame_num = 0- self.num_frames = self.charuco_nFrames_nTrackedPoints_XYZ.shape[0]--- self.charuco_gl_scatter_item = gl.GLScatterPlotItem(pos=self.charuco_nFrames_nTrackedPoints_XYZ[self.frame_num,:,:], color=(1,1,1,.3), size=.1, pxMode=False)- self.gl_view_widget.addItem(self.charuco_gl_scatter_item)-- self.charuco_gl_lineplot_item = gl.GLLinePlotItem(pos=self.charuco_nFrames_nTrackedPoints_XYZ[self.frame_num,:,:], color=(0,1,1,1), width=1., antialias=True)- self.gl_view_widget.addItem(self.charuco_gl_lineplot_item)--- # Plot Media Pipe stuff- self.mediapipe_gl_scatter_item = gl.GLScatterPlotItem(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num,:,:], color=(1,0,1,.3), size=.025, pxMode=False)- self.gl_view_widget.addItem(self.mediapipe_gl_scatter_item)-- #mediapipe body connect-the-dots order- self.mediapipe_head_indexes = [8, 6, 5, 4, 0, 10, 9, 0, 1, 2, 3, 7 ]- self.mediapipe_tors_indexes = [12, 11, 24, 23, 12]- self.mediapipe_rArm_indexes = [12, 14, 16, 18, 20, 16, 22 ]- self.mediapipe_lArm_indexes = [11, 13, 15, 17, 19, 15, 21]- self.mediapipe_rLeg_indexes = [24, 26, 28, 30, 32, 28, ]- self.mediapipe_lLeg_indexes = [23, 25, 27, 29, 31, 27, ]-- rHandIDstart = 33- lHandIDstart = 54- mediapipe_hand_indexes = [0,1,2,3,4,3,2,1,0,5,6,7,8,7,6,5,9,10,11,12,11,10,9,13,14,15,16,15,14,13,17,18,19,20,19,18,17,0]--- self.mediapipe_rHand_indexes = [this_index + rHandIDstart for this_index in mediapipe_hand_indexes]- self.mediapipe_lHand_indexes = [this_index + lHandIDstart for this_index in mediapipe_hand_indexes]-- self.mediapipe_head_gl_lineplot_item = gl.GLLinePlotItem(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num,self.mediapipe_head_indexes,:], color=(1,0,1,1), width=1., antialias=True)- self.mediapipe_tors_gl_lineplot_item = gl.GLLinePlotItem(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num,self.mediapipe_tors_indexes,:], color=(1,0,1,1), width=1., antialias=True)- self.mediapipe_rArm_gl_lineplot_item = gl.GLLinePlotItem(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num,self.mediapipe_rArm_indexes,:], color=(1,0,1,1), width=1., antialias=True)- self.mediapipe_lArm_gl_lineplot_item = gl.GLLinePlotItem(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num,self.mediapipe_lArm_indexes,:], color=(1,0,1,1), width=1., antialias=True)- self.mediapipe_rLeg_gl_lineplot_item = gl.GLLinePlotItem(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num,self.mediapipe_rLeg_indexes,:], color=(1,0,1,1), width=1., antialias=True)- self.mediapipe_lLeg_gl_lineplot_item = gl.GLLinePlotItem(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num,self.mediapipe_lLeg_indexes,:], color=(1,0,1,1), width=1., antialias=True)-- self.mediapipe_rHand_gl_lineplot_item = gl.GLLinePlotItem(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num,self.mediapipe_rHand_indexes,:], color=(1,1,0,1), width=2., antialias=True)- self.mediapipe_lHand_gl_lineplot_item = gl.GLLinePlotItem(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num,self.mediapipe_lHand_indexes,:], color=(0,1,1,1), width=2., antialias=True)--- self.gl_view_widget.addItem(self.mediapipe_head_gl_lineplot_item)- self.gl_view_widget.addItem(self.mediapipe_tors_gl_lineplot_item)- self.gl_view_widget.addItem(self.mediapipe_rArm_gl_lineplot_item)- self.gl_view_widget.addItem(self.mediapipe_lArm_gl_lineplot_item)- self.gl_view_widget.addItem(self.mediapipe_rLeg_gl_lineplot_item)- self.gl_view_widget.addItem(self.mediapipe_lLeg_gl_lineplot_item)- self.gl_view_widget.addItem(self.mediapipe_rHand_gl_lineplot_item)- self.gl_view_widget.addItem(self.mediapipe_lHand_gl_lineplot_item)--- self.qt_timer = QtCore.QTimer()- self.qt_timer.timeout.connect(self.update)- self.qt_timer.start(50)---- def update(self):-- self.frame_num +=1- if self.frame_num > self.num_frames:- self.frame_num = 0--- self.charuco_gl_scatter_item.setData(pos=self.charuco_nFrames_nTrackedPoints_XYZ[self.frame_num,:,:])- self.charuco_gl_lineplot_item.setData(pos=self.charuco_nFrames_nTrackedPoints_XYZ[self.frame_num,:,:])-- self.mediapipe_gl_scatter_item.setData(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num,:,:])-- self.mediapipe_head_gl_lineplot_item.setData(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num, self.mediapipe_head_indexes,:])- self.mediapipe_tors_gl_lineplot_item.setData(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num, self.mediapipe_tors_indexes,:])- self.mediapipe_rArm_gl_lineplot_item.setData(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num, self.mediapipe_rArm_indexes,:])- self.mediapipe_lArm_gl_lineplot_item.setData(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num, self.mediapipe_lArm_indexes,:])- self.mediapipe_rLeg_gl_lineplot_item.setData(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num, self.mediapipe_rLeg_indexes,:])- self.mediapipe_lLeg_gl_lineplot_item.setData(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num, self.mediapipe_lLeg_indexes,:])-- self.mediapipe_rHand_gl_lineplot_item.setData(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num, self.mediapipe_rHand_indexes,:])- self.mediapipe_lHand_gl_lineplot_item.setData(pos=self.mediapipe_nFrames_nTrackedPoints_XYZ[self.frame_num, self.mediapipe_lHand_indexes,:])-- if self.frame_num % 10 == 0:- print("Frame Num : " + str(self.frame_num) + " - ViewPoint - Distance: " + str(self.gl_view_widget.opts['distance']) + " Elevation: " + str(self.gl_view_widget.opts['elevation']) + " Azimuth: " + str(self.gl_view_widget.opts['azimuth']) )-- def start(self):- if (sys.flags.interactive != 1) or not hasattr(QtCore, "PYQT_VERSION"):- QtGui.QApplication.instance().exec_()---if __name__ == '__main__':- visualizer = FMC_Visualizer(recording_session_path = "C:/Users/jonma/Dropbox/FreeMoCapProject/FreeMocap_Data/FreeMoCap_Session_2021-12-09_11_05_31")- visualizer.start()-diff --git a/freemocap/dev_scratchpad/dev_alpha_old/__init__.py b/freemocap/dev_scratchpad/dev_alpha_old/__init__.pydeleted file mode 100644index e69de29b..00000000diff --git a/freemocap/dev_scratchpad/dev_alpha_old/capture_voulme_calibration.py b/freemocap/dev_scratchpad/dev_alpha_old/capture_voulme_calibration.pydeleted file mode 100644index 34b4603f..00000000--- a/freemocap/dev_scratchpad/dev_alpha_old/capture_voulme_calibration.py+++ /dev/null@@ -1,274 +0,0 @@-from rich import print-from rich.console import Console-from FMC_MultiCamera import FMC_MultiCamera-import socket-from pathlib import Path-from pathos.helpers import mp as pathos_mp_helper-import cv2-from copy import deepcopy-import numpy as np--from itertools import combinations--import matplotlib.pyplot as plt--aruco_dict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_4X4_250)-charuco_length = 7-charuco_width = 5--board = cv2.aruco.CharucoBoard_create(charuco_length, charuco_width, 1, .8, aruco_dict)-num_charuco_corners = (charuco_length-1) * (charuco_width-1)--def detect_charuco_board(image):- """- Charuco base pose estimation.- more-or-less copied from - https://mecaruco2.readthedocs.io/en/latest/notebooks_rst/Aruco/sandbox/ludovic/aruco_calibration_rotation.html- """- charuco_corners = []- charuco_ids = []-- # SUB PIXEL CORNER DETECTION CRITERION- criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 0.00001)-- gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)- aruco_square_corners, aruco_square_ids, rejectedImgPoints = cv2.aruco.detectMarkers(gray, aruco_dict)--- if len(aruco_square_corners)>0:- # SUB PIXEL DETECTION- for this_corner in aruco_square_corners:- cv2.cornerSubPix(gray, this_corner,- winSize = (3,3),- zeroZone = (-1,-1),- criteria = criteria)- res2 = cv2.aruco.interpolateCornersCharuco(aruco_square_corners,aruco_square_ids,gray,board)-- if res2[1] is not None and res2[2] is not None and len(res2[1])>3:- charuco_corners = res2[1]- charuco_ids = res2[2]--- return charuco_corners, charuco_ids, aruco_square_corners, aruco_square_ids---if __name__ == "__main__":- pathos_mp_helper.freeze_support()-- console = Console() #create rich console to catch and print exceptions--- this_computer_name = socket.gethostname()-- freemocap_data_path=None- in_rotation_codes_list=None- in_cams_to_use_list = None-- if this_computer_name=='jon-hallway-XPS-8930':- freemocap_data_path = Path('/home/jon/Dropbox/FreeMoCapProject/FreeMocap_Data')- in_rotation_codes_list = ['cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', ]- elif this_computer_name == 'DESKTOP-DCG6K4F':- freemocap_data_path = Path(r'C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data')- elif this_computer_name == 'DESKTOP-V3D343U':- freemocap_data_path = Path(r'C:\Users\WindowsPC_Hallway\Dropbox\FreeMoCapProject\FreeMocap_Data')- in_cams_to_use_list = [0,1,2,3]- elif this_computer_name == 'Jons-MacBook-Pro.local':- freemocap_data_path = Path('/Users/jon/Dropbox/FreeMoCapProject')-- try:- multi_cam = FMC_MultiCamera(cams_to_use_list = in_cams_to_use_list, show_multi_cam_stream_bool = False, freemocap_data_folder=str(freemocap_data_path), rotation_codes_list=in_rotation_codes_list)- multi_cam.start()----- console.rule('Launching Multi Cam Viewer')- current_loop_count = 0- aruco_square_corners = [ [] for _ in range(multi_cam.num_cams) ]- aruco_square_ids = [ np.array([]) for _ in range(multi_cam.num_cams) ]- charuco_corners = [ np.array([]) for _ in range(multi_cam.num_cams) ]- charuco_ids = [ np.array([]) for _ in range(multi_cam.num_cams) ]--- camera_pair_list = list(combinations(np.arange(multi_cam.num_cams),2))- camera_pair_joint_view_count = [ 0 for _ in range(len(camera_pair_list)) ]- each_cameras_shared_board_view_count_array = np.zeros((multi_cam.num_cams, multi_cam.num_cams))- each_cameras_shared_board_view_count_total = np.zeros(multi_cam.num_cams)-- charuco_points_from_previous_frames = [ [] for _ in range(multi_cam.num_cams) ]-- multi_cam_queue_size_list = []-- # #set up plots- # plt.ion()- # fig = plt.figure()- # ax_queue_size = plt.subplot(121, title='Multi Camera - Queueue Size', xlabel='Frame# I guess/? Maybe Loop#/?', ylabel='Multi frames in queue')- # ax_shared_views = plt.subplot(122, title='Shared Charuco Board Views', xlabel='Frame#/?', ylabel='Number of shared views')-- # artist_for_queue_trace = ax_queue_size.plot(0)-- # artists_for_shared_view_traces_list = []- # for this_cam_num in range(multi_cam.num_cams):- # artists_for_shared_view_traces_list.append(ax_shared_views.plot(0, label=f'Cam {this_cam_num}'))- # ax_shared_views.legend()-- while not multi_cam.exit_event.is_set():- if not multi_cam.multi_cam_tuple_queue.empty():-- this_multi_cam_tuple = multi_cam.multi_cam_tuple_queue.get()- current_loop_count += 1--- # multi_cam.save_synchronized_videos(this_multi_cam_tuple)-- # this_multi_cam_image_raw = multi_cam.stitch_multicam_image(this_multi_cam_tuple)-- # if multi_cam._save_multi_cam_to_mp4:- # if multi_cam._output_multi_cam_video_object is None:- # multi_cam.initialize_multi_cam_output_video(this_multi_cam_image_raw)--- # multi_cam._output_multi_cam_video_object.write(this_multi_cam_image_raw)-- if multi_cam._save_each_cam_to_mp4:- if multi_cam._each_cam_video_writer_object_list is None:- multi_cam.initialize_each_cam_output_video(this_multi_cam_tuple)-- for this_cam_num in range(multi_cam.num_cams):- multi_cam._each_cam_video_writer_object_list[this_cam_num].write(this_multi_cam_tuple[this_cam_num][1])---- new_cam_image_list = []- full_charuco_detected_on_this_frame = [ False for _ in range(multi_cam.num_cams) ]-- for this_cam_num in range(multi_cam.num_cams):- this_cam_tuple = this_multi_cam_tuple[this_cam_num]- this_cam_image = this_cam_tuple[1]-- if current_loop_count % 10 == 0:- charuco_corners[this_cam_num], charuco_ids[this_cam_num], aruco_square_corners[this_cam_num], aruco_square_ids[this_cam_num] = detect_charuco_board(this_cam_image)-- if len(charuco_ids[this_cam_num]) == num_charuco_corners:- full_charuco_detected_on_this_frame[this_cam_num] = True--- # image_w_markers = cv2.aruco.drawDetectedMarkers(this_cam_image, aruco_square_corners[this_cam_num], aruco_square_ids[this_cam_num])-- image_w_markers = cv2.aruco.drawDetectedCornersCharuco(this_cam_image, np.array(charuco_corners[this_cam_num]), np.array(charuco_ids[this_cam_num]))-- this_cam_name = "Camera " + str(this_cam_num)-- text_to_write_on_this_camera =''- current_cam_corner_count_str = this_cam_name + ": " + str(len(charuco_ids[this_cam_num])) + " of " + str(num_charuco_corners) + " detected | Full Board: " + str(full_charuco_detected_on_this_frame[this_cam_num])- this_cam_shared_views_str = " | Shared Views: " + str(each_cameras_shared_board_view_count_total[this_cam_num])- text_to_write_on_this_camera = current_cam_corner_count_str + this_cam_shared_views_str-- if this_cam_num==0:- queue_size_str = "Multi-Camera Queue Size: " + str(multi_cam.multi_cam_tuple_queue.qsize())- queue_text_position = (10,20)- cv2.putText(- image_w_markers, #numpy array on which text is written- queue_size_str, #text- queue_text_position, #position at which writing has to start- cv2.FONT_HERSHEY_SIMPLEX, #font family- .5, #font size- (255, 10, 100, 255), #font color- 2) #font stroke- cv2.putText(- image_w_markers, #numpy array on which text is written- queue_size_str, #text- queue_text_position, #position at which writing has to start- cv2.FONT_HERSHEY_SIMPLEX, #font family- .5, #font size- (30, 10, 0, 255), #font color- 3) #font stroke--- position = (10,50)- cv2.putText(- image_w_markers, #numpy array on which text is written- text_to_write_on_this_camera, #text- position, #position at which writing has to start- cv2.FONT_HERSHEY_SIMPLEX, #font family- .5, #font size- (30, 10, 0, 255), #font color- 3) #font stroke- cv2.putText(- image_w_markers, #numpy array on which text is written- text_to_write_on_this_camera, #text- position, #position at which writing has to start- cv2.FONT_HERSHEY_SIMPLEX, #font family- .5, #font size- (209, 180, 0, 255), #font color- 1) #font stroke--- if full_charuco_detected_on_this_frame[this_cam_num]:- cv2.polylines(image_w_markers, np.int32([charuco_corners[this_cam_num]]), True, (0,100,255), 2)- # for these_corners in charuco_points_from_previous_frames[this_cam_num]:- # if len(these_corners)>0:- # cv2.polylines(image_w_markers, np.int32([these_corners[this_cam_num]]), True, (0,100,255,255/2), 2)-- charuco_points_from_previous_frames[this_cam_num].append(charuco_corners[this_cam_num])-- new_cam_image_list.append(this_cam_image)--- #determine paired board views- for this_cam_num in range(multi_cam.num_cams):- if full_charuco_detected_on_this_frame[this_cam_num]:- for this_other_camera_num in range(multi_cam.num_cams):- if this_other_camera_num != this_cam_num:- if full_charuco_detected_on_this_frame[this_other_camera_num]:- each_cameras_shared_board_view_count_array[this_cam_num, this_other_camera_num] += 1- each_cameras_shared_board_view_count_total = np.sum(each_cameras_shared_board_view_count_array, axis=1)- # print(each_cameras_shared_board_view_count)--- for this_pair_num, this_cam_pair in enumerate(camera_pair_list):- camera_one_id = this_cam_pair[0]- camera_two_id = this_cam_pair[1]-- if full_charuco_detected_on_this_frame[camera_one_id] and full_charuco_detected_on_this_frame[camera_two_id]:- camera_pair_joint_view_count[this_pair_num] += 1--- #update plots- multi_cam_queue_size_list.append(multi_cam.multi_cam_tuple_queue.qsize())--- # artist_for_queue_trace[0].set_data((np.arange(current_loop_count),multi_cam_queue_size_list))- # plt.pause(0.01)- # plt.draw()- # for camNum in range(multi_cam.num_cams):- # ax_shared_views.plot(0, label=f'Cam {camNum}')--- multi_cam_image = np.hstack(new_cam_image_list)-- cv2.imshow(multi_cam._rec_name, multi_cam_image)--- key = cv2.waitKey(1)-- if key == 27: # exit on ESC- break-- if cv2.getWindowProperty(multi_cam._rec_name, cv2.WND_PROP_VISIBLE) < 1: #break loop if window closed- break-- cv2.destroyAllWindows()-- if multi_cam._save_multi_cam_to_mp4:- multi_cam._output_multi_cam_video_object.release()-- if multi_cam._save_each_cam_to_mp4:- for this_cam_num in range(multi_cam.num_cams):- multi_cam._each_cam_video_writer_object_list[this_cam_num].release()-- console.rule('Shutting down MultiCamera Viewer')- multi_cam.exit_event.set() #send the 'Exit' signal to everyone.--- except Exception:- console.print_exception()diff --git a/freemocap/dev_scratchpad/dev_alpha_old/default_json_definitions/freemocap_mediapipe_json.json b/freemocap/dev_scratchpad/dev_alpha_old/default_json_definitions/freemocap_mediapipe_json.jsondeleted file mode 100644index 72880b7b..00000000--- a/freemocap/dev_scratchpad/dev_alpha_old/default_json_definitions/freemocap_mediapipe_json.json+++ /dev/null@@ -1,735 +0,0 @@-{- "body": {- "body_mass_segments": {- "head": {- "parent": "neck",- "child": "head_center",- "anthropometric_definition": {- "proportional_length": 0.064,- "proportional_mass": 0.081,- "center_of_mass_proximal": 0,- "center_of_mass_distal": 1- },- "spherical_joint_limits": {- "azimuth": [- 0,- 180- ],- "elevation": [- 0,- 180- ]- }- },- "torso": {- "parent": "neck",- "child": "sacrum",- "anthropometric_definition": {- "proportional_length": 0.288,- "proportional_mass": 0.497,- "center_of_mass_proximal": 50,- "center_of_mass_distal": 50- },- "spherical_joint_limits": {- "azimuth": [- 0,- 180- ],- "elevation": [- 0,- 180- ]- }- },- "right_clavicle": {- "parent": "neck",- "child": "right_shoulder",- "anthropometric_definition": {- "proportional_length": 0.129,- "proportional_mass": null,- "center_of_mass_proximal": 0.712,- "center_of_mass_distal": 0.288- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "right_upper_arm": {- "parent": "right_shoulder",- "child": "right_elbow",- "anthropometric_definition": {- "proportional_length": 0.186,- "proportional_mass": 0.028,- "center_of_mass_proximal": 0.436,- "center_of_mass_distal": 0.564- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "right_lower_arm": {- "parent": "right_elbow",- "child": "right_wrist",- "anthropometric_definition": {- "proportional_length": 0.146,- "proportional_mass": 0.016,- "center_of_mass_proximal": 0.43,- "center_of_mass_distal": 0.57- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "right_hand": {- "parent": "right_wrist",- "child": "right_hand_middle_knuckle",- "anthropometric_definition": {- "proportional_length": 0.108,- "proportional_mass": 0.006,- "center_of_mass_proximal": 0.506,- "center_of_mass_distal": 0.494- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "right_hip": {- "parent": "sacrum",- "child": "right_hip",- "anthropometric_definition": {- "proportional_length": 0.096,- "proportional_mass": 0.071,- "center_of_mass_proximal": 0.5,- "center_of_mass_distal": 0.5- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "right_upper_leg": {- "parent": "right_hip",- "child": "right_knee",- "anthropometric_definition": {- "proportional_length": 0.245,- "proportional_mass": 0.1,- "center_of_mass_proximal": 0.433,- "center_of_mass_distal": 0.567- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "right_lower_leg": {- "parent": "right_knee",- "child": "right_ankle",- "anthropometric_definition": {- "proportional_length": 0.246,- "proportional_mass": 0.0456,- "center_of_mass_proximal": 0.433,- "center_of_mass_distal": 0.567- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "right_thumb": {- "parent": "right_wrist",- "child": "right_thumb",- "anthropometric_definition": {- "proportional_length": 0.05,- "proportional_mass": null,- "center_of_mass_proximal": 0.5,- "center_of_mass_distal": 0.5- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "right_foot ": {- "parent": "right_ankle",- "child": "right_foot_index",- "anthropometric_definition": {- "proportional_length": 0.152,- "proportional_mass": 0.0145,- "center_of_mass_proximal": 0.5,- "center_of_mass_distal": 0.5- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "left_clavicle": {- "parent": "neck",- "child": "left_shoulder",- "anthropometric_definition": {- "proportional_length": 0.129,- "proportional_mass": null,- "center_of_mass_proximal": 0.712,- "center_of_mass_distal": 0.288- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "left_upper_arm": {- "parent": "left_shoulder",- "child": "left_elbow",- "anthropometric_definition": {- "proportional_length": 0.186,- "proportional_mass": 0.028,- "center_of_mass_proximal": 0.436,- "center_of_mass_distal": 0.564- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "left_lower_arm": {- "parent": "left_elbow",- "child": "left_wrist",- "anthropometric_definition": {- "proportional_length": 0.146,- "proportional_mass": 0.016,- "center_of_mass_proximal": 0.43,- "center_of_mass_distal": 0.57- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "left_hand": {- "parent": "left_wrist",- "child": "left_hand_middle_knuckle",- "anthropometric_definition": {- "proportional_length": 0.108,- "proportional_mass": 0.006,- "center_of_mass_proximal": 0.506,- "center_of_mass_distal": 0.494- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "left_thumb": {- "parent": "left_wrist",- "child": "left_thumb",- "anthropometric_definition": {- "proportional_length": 0.05,- "proportional_mass": null,- "center_of_mass_proximal": 0.5,- "center_of_mass_distal": 0.5- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "left_hip": {- "parent": "sacrum",- "child": "left_hip",- "anthropometric_definition": {- "proportional_length": 0.096,- "proportional_mass": 0.071,- "center_of_mass_proximal": 0.5,- "center_of_mass_distal": 0.5- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "left_upper_leg": {- "parent": "left_hip",- "child": "left_knee",- "anthropometric_definition": {- "proportional_length": 0.245,- "proportional_mass": 0.1,- "center_of_mass_proximal": 0.433,- "center_of_mass_distal": 0.567- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "left_lower_leg": {- "parent": "left_knee",- "child": "left_ankle",- "anthropometric_definition": {- "proportional_length": 0.246,- "proportional_mass": 0.0456,- "center_of_mass_proximal": 0.433,- "center_of_mass_distal": 0.567- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "left_foot ": {- "parent": "left_ankle",- "child": "left_foot_index",- "anthropometric_definition": {- "proportional_length": 0.152,- "proportional_mass": 0.0145,- "center_of_mass_proximal": 0.5,- "center_of_mass_distal": 0.5- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- }- },- "virtual_markers": {- "head_center": {- "left_ear": 0.333,- "right_ear": 0.333,- "nose": 0.334- },- "neck": {- "left_shoulder": 0.5,- "right_shoulder": 0.5- },- "sacrum": {- "left_hip": 0.5,- "right_hip": 0.5- },- "left_hand_middle_knuckle": {- "left_index": 0.5,- "left_pinky": 0.5- },- "right_hand_middle_knuckle": {- "right_index": 0.5,- "right_pinky": 0.5- },- "center_of_mass": {- "etc": 0.9- }- },- "tracked_point_names": [- "nose",- "left_eye_inner",- "left_eye",- "left_eye_outer",- "right_eye_inner",- "right_eye",- "right_eye_outer",- "left_ear",- "right_ear",- "mouth_left",- "mouth_right",- "left_shoulder",- "right_shoulder",- "left_elbow",- "right_elbow",- "left_wrist",- "right_wrist",- "left_pinky",- "right_pinky",- "left_index",- "right_index",- "left_thumb",- "right_thumb",- "left_hip",- "right_hip",- "left_knee",- "right_knee",- "right_ankle",- "left_ankle",- "left_heel",- "right_heel",- "left_foot_index",- "right_foot_index"- ],- "tracked_point_to_rigify_fk_rig_bone_connections":{- "nose": {- "head": {- "track_type":"bone_tail",- "weight":1.0- },- "left_eye_inner": {},- "left_eye": {},- "left_eye_outer": {},- "right_eye_inner": {},- "right_eye": {},- "right_eye_outer": {},- "left_ear": {},- "right_ear": {},- "mouth_left": {},- "mouth_right": {},- "left_shoulder": {- "shoulder.L": {- "track_type":"bone_tail",- "weight":1.0- },- "upper_arm.L": {- "track_type":"bone_head",- "weight":1.0- }- },- "right_shoulder": {- "shoulder.R": {- "track_type":"bone_tail",- "weight":1.0- },- "upper_arm.R": {- "track_type":"bone_head",- "weight":1.0- }- },- "left_elbow": {- "upper_arm.L": {- "track_type":"bone_tail",- "weight":1.0- },- "forearm.L": {- "track_type":"bone_head",- "weight":1.0- }- },- "right_elbow": {- "upper_arm.R": {- "track_type":"bone_tail",- "weight":1.0- },- "forearm.R": {- "track_type":"bone_head",- "weight":1.0- }- },- "left_wrist": {- "forearm.L": {- "track_type":"bone_tail",- "weight":1.0- },- "hand.L": {- "track_type":"bone_head",- "weight":1.0- }- },- "right_wrist": {- "forearm.R": {- "track_type":"bone_tail",- "weight":1.0- },- "hand.R": {- "track_type":"bone_head",- "weight":1.0- }- },- "left_pinky": {- "hand.L": {- "track_type":"bone_tail",- "weight":1.0- }- },- "right_pinky": {- "hand.R": {- "track_type":"bone_tail",- "weight":1.0- }- },- "left_index": {},- "right_index": {},- "left_thumb": {},- "right_thumb": {},- "left_hip": {- "pelvis.L": {- "track_type":"bone_tail",- "weight":1.0- },- "thigh.L": {- "track_type":"bone_head",- "weight":1.0- }- },- "right_hip": {- "pelvis.R": {- "track_type":"bone_tail",- "weight":1.0- },- "thigh.R": {- "track_type":"bone_head",- "weight":1.0- }- },- "left_knee": {- "thigh.L": {- "track_type":"bone_tail",- "weight":1.0- },- "shin.L": {- "track_type":"bone_head",- "weight":1.0- }- },- "right_knee": {- "shin.R": {- "track_type":"bone_tail",- "weight":1.0- },- "foot.R": {- "track_type":"bone_head",- "weight":1.0- }- },- "right_ankle": {- "shin.R": {- "track_type":"bone_tail",- "weight":1.0- },- "foot.R": {- "track_type":"bone_head",- "weight":1.0- }- },- "left_ankle": {- "shin.L": {- "track_type":"bone_tail",- "weight":1.0- },- "foot.L": {- "track_type":"bone_head",- "weight":1.0- }- },- "left_heel": {- "foot.L": {- "track_type":"bone_tail",- "weight":1.0- },- "toe.L": {- "track_type":"bone_head",- "weight":1.0- }- },- "right_heel": {- "foot.R": {- "track_type":"bone_tail",- "weight":1.0- },- "toe.R": {- "track_type":"bone_head",- "weight":1.0- }- },- "left_foot_index": {- "toe.L": {- "track_type":"bone_tail",- "weight":1.0- }- },- "right_foot_index":{- "toe.R": {- "track_type":"bone_tail",- "weight":1.0- }- }- }- }-},- "hands": {- "body_segments": {- "constrain_length_bool": false,- "left_upper_arm": {- "parent": "left_shoulder",- "child": "left_elbow",- "anthropometric_definition": {- "proportional_length": 0.186,- "proportional_mass": 0.028,- "center_of_mass_proximal": 0.436,- "center_of_mass_distal": 0.564- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "etc": {}- },- "virtual_markers": {- "neck": {- "left_shoulder": 0.5,- "right_shoudler": 0.5- },- "center_of_mass": {- "etc": 0.9- }- },- "dotto_names": [- "nose",- "left_eye_inner",- "butt",- "etc"- ]- },- "face": {- "body_segments": {- "constrain_length_bool": false,- "left_upper_arm": {- "parent": "left_shoulder",- "child": "left_elbow",- "anthropometric_definition": {- "proportional_length": 0.186,- "proportional_mass": 0.028,- "center_of_mass_proximal": 0.436,- "center_of_mass_distal": 0.564- },- "spherical_joint_limits": {- "azimuth": [- 0,- 360- ],- "elevation": [- 0,- 360- ]- }- },- "etc": {}- },- "virtual_markers": {- "neck": {- "left_shoulder": 0.5,- "right_shoudler": 0.5- },- "center_of_mass": {- "etc": 0.9- }- },- "dotto_names": [- "nose",- "left_eye_inner",- "butt",- "etc"- ]- }-}\ No newline at end of filediff --git a/freemocap/dev_scratchpad/dev_alpha_old/fmc_alpha_runme.py b/freemocap/dev_scratchpad/dev_alpha_old/fmc_alpha_runme.pydeleted file mode 100644index f1e016fd..00000000--- a/freemocap/dev_scratchpad/dev_alpha_old/fmc_alpha_runme.py+++ /dev/null@@ -1,13 +0,0 @@-import freemocap as fmc--from FMC_MultiCamera import FMC_MultiCamera--from pathos.helpers import mp as pathos_mp_helper--if __name__ =='__main__':- pathos_mp_helper.freeze_support()-- multi_cam = FMC_MultiCamera(freemocap_data_folder='C:/Users/WindowsPC_Hallway/Dropbox/FreeMoCapProject/FreeMocap_Data')- sessionID = multi_cam._rec_name- multi_cam.start(standalone_mode=True)- f=9diff --git a/freemocap/dev_scratchpad/dev_alpha_old/freemocap_alpha_runme_script.py b/freemocap/dev_scratchpad/dev_alpha_old/freemocap_alpha_runme_script.pydeleted file mode 100644index 806fdad3..00000000--- a/freemocap/dev_scratchpad/dev_alpha_old/freemocap_alpha_runme_script.py+++ /dev/null@@ -1,35 +0,0 @@-from pathlib import Path--from pathos.helpers import mp as pathos_mp_helper-from rich.console import Console--from FMC_MultiCamera import FMC_MultiCamera-import freemocap as fmc----if __name__ == '__main__':- pathos_mp_helper.freeze_support()- console = Console() #create rich console to catch and print exceptions-- import socket- this_computer_name = socket.gethostname()-- freemocap_data_path=None- in_rotation_codes_list=None-- if this_computer_name=='jon-hallway-XPS-8930':- freemocap_data_path = Path('/home/jon/Dropbox/FreeMoCapProject/FreeMocap_Data')- in_rotation_codes_list = ['cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_COUNTERCLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', 'cv2.ROTATE_90_CLOCKWISE', ]- elif this_computer_name == 'DESKTOP-DCG6K4F':- freemocap_data_path = Path(r'C:\Users\jonma\Dropbox\FreeMoCapProject\FreeMocap_Data')--- try:- multi_cam = FMC_MultiCamera(freemocap_data_folder=str(freemocap_data_path), rotation_codes_list=in_rotation_codes_list)- multi_cam.start(standalone_mode=True)- except Exception:- console.print_exception()-- fmc.RunMe(multi_cam._rec_name, stage=3)-\ No newline at end of filediff --git a/freemocap/dev_scratchpad/dev_alpha_old/old_FMC_MultiCameraRecorder.py b/freemocap/dev_scratchpad/dev_alpha_old/old_FMC_MultiCameraRecorder.pydeleted file mode 100644index 43f3b7ab..00000000--- a/freemocap/dev_scratchpad/dev_alpha_old/old_FMC_MultiCameraRecorder.py+++ /dev/null@@ -1,455 +0,0 @@-##-##-## βββββββ βββ βββ ββββββ βββ βββ ββ ββ ββ ββββββββ ββ ββββββ βββββ βββ βββ βββββββ ββββββ βββββ-## ββ ββββ ββββ ββ ββββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββββ ββ ββ ββ ββ ββ-## βββββ ββ ββββ ββ ββ ββ ββββ ββ ββ ββ ββ ββ ββ βββββ ββ βββββββ ββ ββββ ββ βββββ ββββββ βββββββ-## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-## ββ ββ ββ ββββββ ββ ββ ββββββ βββββββ ββ ββ ββββββ ββ ββ ββ ββ βββββββ ββ ββ ββ ββ-##-##-#Font - ANSI Regular - https://patorjk.com/software/taag/#p=display&f=ANSI%20Regular&t=Play%20Skeleton%20Animation---from os import mkdir-from FMC_Camera import FMC_Camera--import concurrent.futures-import logging-import queue-import threading-import pathos.multiprocessing as pathos_mp-from pathos.helpers import mp as pathos_mp_helper-import time-import platform-import datetime--import cv2-import matplotlib.pyplot as plt-import numpy as np-import h5py-from pathlib import Path--from rich import print-from rich import inspect-from rich.progress import track-from rich.traceback import install-install(show_locals=True)-from rich.console import Console-rich_console = Console()-from rich.progress import track---class FMC_MultiCameraRecorder:- ##- ##- ## ββ βββ ββ ββ ββββββββ- ## ββ ββββ ββ ββ ββ- ## ββ ββ ββ ββ ββ ββ- ## ββ ββ ββ ββ ββ ββ- ## βββββββ βββββββ ββ ββ ββββ ββ ββ βββββββ βββββββ- ##- ##- ##- def __init__(- self,- rec_name = None,- save_path = None,- num_cams = 0,- cams_to_use_list = None,- show_multi_cam_stream = True,- save_to_mp4 = True,- save_to_h5 = False,- save_log_file = False- ):-- self._init_start_time = time.time_ns()#the precision is aspirational, lol-- if rec_name is None:- self._rec_name = datetime.datetime.now().strftime("FMC_MultiCamRecording_%Y-%b-%d_%H_%M_%S")- else:- self._rec_name = rec_name--- if save_path is None:- self._save_path = Path('data/' + self._rec_name)- else:- self._save_path = Path(save_path)-- if save_path or save_to_mp4 or save_to_h5 or save_log_file:- self._save_path.mkdir(parents=True)- self._path_to_log_file = Path(str(self._save_path / self._rec_name) + '_log.txt')-- self._save_to_mp4 = save_to_mp4- if self._save_to_mp4:- self._output_video_object = None #build this after we get the first multi_frame_image-- # log_file_obj = open(self._path_to_log_file,'w+')- # rich_console = Console(file=log_file_obj)- rich_console = Console()-- self._num_cams = num_cams- self._cams_to_use_list = cams_to_use_list----- rich_console.rule('Starting FreeMoCap MultiCam Recorder!')----- ###- ###- ### ββββββ ββββββ ββββββ ββββββ βββββββ ββββββ ββββββββ ββ βββββββ βββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββββββ ββββββ ββ ββ ββββββ βββββ ββββββ ββ ββ βββββ βββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ ββ ββββββ ββ βββββββ ββ ββ ββ ββ βββββββ βββββββ- ###- ###-- @property- def num_cams(self):- """How many cameras to use"""- return self._num_cams-- @num_cams.setter- def num_cams(self, num_cams_input):- """Define how many cameras to use"""- self._num_cams = num_cams_input-- @property- def cams_to_use_list(self):- """ a list denoting which camera IDs to use.- If both this and `num_cams` are set to default value (`0` and `None`, respectively), use all available cameras).- """- return self._cams_to_use_list-- @property- def init_start_time(self):- """Time that this object was initialized (UNIX Epoch time from `time.time_ns())-- Returns:- float: time this object was created (UNIX Epoch time from `time.time_ns())- """- return self._init_start_time-- @property- def each_cam_timestamps_unix_ns(self): #not really a property though?- return self._each_cam_timestamps_unix_ns- ###- ###- ### βββ βββ βββββββ ββββββββ ββ ββ ββββββ ββββββ βββββββ- ### ββββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββββ ββ βββββ ββ βββββββ ββ ββ ββ ββ βββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ βββββββ ββ ββ ββ ββββββ ββββββ βββββββ- ###- ###- def start(self):-- if self.cams_to_use_list is None:- self.find_available_cameras()-- # pathos_pool = ProcessPool(nodes=1)- # pathos_pool.map(self.start_multi_cam_thread_pool)- # self.start_multi_cam_thread_pool() #threads are mostly fine, but I think processes will be better eventually?- self.start_multi_cam_process_pool() # as of 2021-10-26, this doesn't work :(- self.create_diagnostic_images()---- def find_available_cameras(self):- """find available webcams and return IDs in list."""-- if self.num_cams == 0:- num_cams_to_check = 20 #man, i HOPE you're out there trying to record from more than 20 cameras!!!- else:- num_cams_to_check = self.num_cams-- if platform.system() == 'Windows':- capBackend = cv2.CAP_DSHOW- else:- capBackend = cv2.CAP_ANY-- self._cams_to_use_list = []- for camNum in track(range(num_cams_to_check), description='[green]Finding available cameras...' ):- cap = cv2.VideoCapture(camNum, capBackend)- if cap.isOpened():- self._cams_to_use_list.append(camNum)- cap.release()- rich_console.print("Found cameras at ", self._cams_to_use_list)-- self.num_cams = len(self._cams_to_use_list)- return self.cams_to_use_list- ###- ###- ### βββββββ ββββββββ βββββ ββββββ ββββββββ ββββββββ ββ ββ ββββββ βββββββ βββββ ββββββ ββββββ ββββββ ββββββ ββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### βββββββ ββ βββββββ ββββββ ββ ββ βββββββ ββββββ βββββ βββββββ ββ ββ ββββββ ββ ββ ββ ββ ββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### βββββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ βββββββ ββ ββ ββββββ ββ ββββββ ββββββ βββββββ- ###- ###-- def start_multi_cam_thread_pool(self):- """creates a threading.ThreadPoolExecutor that creates an `FMC_Camera` object for each camera in `self.cams_to_use_list` and sets each to trigger `run_in_thread` mode-- NOTE - Eventually I want these to be in different processes, I think?- """- self.cam_frame_queue = queue.Queue(maxsize=self.num_cams)- self.multi_cam_image_queue = queue.Queue()-- self.exit_event = threading.Event()-- self._each_cam_timestamps_unix_ns = None- self._cam_thread = [None] * self.num_cams-- rich_console.rule('starting multi_cam_thread_pool')-- while not self.exit_event.is_set():- with concurrent.futures.ThreadPoolExecutor(max_workers=self.num_cams+1) as executor:- self.barrier = threading.Barrier(self.num_cams, action=self.grab_incoming_cam_tuples)- for this_cam_num in self.cams_to_use_list: #i feel like there is some cooler way to do this using 'map' and 'zip' or whatever, but this feels easier on the ol' brain noggin lol- self._cam_thread[this_cam_num] = executor.submit(FMC_Camera, cam_num=this_cam_num, frame_queue = self.cam_frame_queue, barrier = self.barrier, exit_event = self.exit_event)- executor.submit(self.show_multi_cam_cv2)--###-###-### βββββββ ββββββββ βββββ ββββββ ββββββββ ββββββ ββββββ ββββββ ββββββ βββββββ βββββββ βββββββ ββββββ ββββββ ββββββ ββ-### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-### βββββββ ββ βββββββ ββββββ ββ ββββββ ββββββ ββ ββ ββ βββββ βββββββ βββββββ ββββββ ββ ββ ββ ββ ββ-### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-### βββββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββββββ ββββββ βββββββ βββββββ βββββββ ββ ββββββ ββββββ βββββββ-###-###-###- def start_multi_cam_process_pool(self):- """- reates a ProcessPoolExecutor that creates an `FMC_Camera` object for each camera in `self.cams_to_use_list` and sets each to trigger `run_in_process` mode (which just passes to `run_in_thread` mode)-- """- mrManager = pathos_mp_helper.Manager()- self.cam_frame_queue = mrManager.Queue(maxsize=self.num_cams)- self.multi_cam_image_queue = mrManager.Queue()- self.barrier = mrManager.Barrier(self.num_cams+1) #each cam + one more for the frame grabber- self.exit_event = mrManager.Event()-- self._each_cam_timestamps_unix_ns = None- self._cam_process = [None] * self.num_cams-- rich_console.rule('starting multi_cam_process_pool')-- in_frame_q_list = [self.cam_frame_queue]*self.num_cams- in_barrier_list = [self.barrier]*self.num_cams- in_exit_event_list = [self.exit_event]*self.num_cams- # nsole_list = [self.rich_console]*self.num_cams--- num_jobs = self.num_cams- with pathos_mp.ProcessingPool(num_jobs) as pp_pool: #lol- pp_pool.amap(- FMC_Camera,- tuple(self.cams_to_use_list),- tuple(in_frame_q_list),- tuple(in_barrier_list),- tuple(in_exit_event_list),- )- incoming_frame_grabber_thread = threading.Thread(target=self.grab_incoming_cam_tuples, name='Incoming Frame Grabber')- incoming_frame_grabber_thread.start()- self.show_multi_cam_cv2()---- ###- ###- ### ββββββ ββββββ βββββ ββββββ ββ βββ ββ ββββββ ββββββ βββ βββ ββ βββ ββ ββββββ ββββββββ ββ ββ ββββββ ββ βββββββ βββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ ββ ββ ββ ββββ ββββ ββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ βββ ββββββ βββββββ ββββββ ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ ββ ββ ββ ββ ββ βββ ββ ββ ββ ββββββ ββ βββββ βββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββββββ ββ ββ ββ ββ ββββββ ββ ββ ββββ ββββββ ββββββ ββ ββ ββ ββ ββββ ββββββ ββ ββββββ ββ βββββββ βββββββ βββββββ- ###- ###--- def grab_incoming_cam_tuples(self):- """grab incoming cam_tuples, put them into a multi_cam_tuple and stuff that into the multi_cam_tuple_queue"""-- # self.show_multi_cam_cv2()-- while not self.exit_event.is_set():- self.barrier.wait() #wait until each camera has grabbed an image and tagged their `barrier.wait()` signals-- these_images_list = [None]*self.num_cams #empty list of size (numCam)- these_timestamps = np.ndarray(self.num_cams)-- for cam_num in range(self.num_cams):- this_cam_image_timestamp_tuple = self.cam_frame_queue.get()- this_cam_num = this_cam_image_timestamp_tuple[0]- these_images_list[this_cam_num] = this_cam_image_timestamp_tuple[1]- these_timestamps[this_cam_num] = this_cam_image_timestamp_tuple[2]-- if self._each_cam_timestamps_unix_ns is None:- self._each_cam_timestamps_unix_ns = these_timestamps- else:- self._each_cam_timestamps_unix_ns = np.vstack((self._each_cam_timestamps_unix_ns, these_timestamps)) #result will be numpy array with `num_frames` rows and `num_cams` columns--- multi_cam_image = np.hstack(these_images_list) #create multiFrame_image by stitching together incoming camera images- self.multi_cam_image_queue.put(multi_cam_image)- # rich_console.log('Created a multi_cam_image - queue size: {}'.format(self.multi_cam_image_queue.qsize()))-- ###- ###- ### βββββββ ββ ββ ββββββ ββ ββ ββ ββ ββ ββββββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### βββββββ βββββββ ββ ββ ββ β ββ ββ ββ ββ ββ ββ- ### ββ ββ ββ ββ ββ ββ βββ ββ ββ ββ ββ ββ ββ- ### βββββββ ββ ββ ββββββ βββ βββ ββββ ββ ββββββ- ###- ###-- def show_multi_cam_cv2(self):- """display multi_cam_image using `cv2.imshow` and maybe save it to mp4, who knows?- """- rich_console.rule('Launching Multi Cam Viewer')- while not self.exit_event.is_set():- if not self.multi_cam_image_queue.empty():- multi_cam_image = self.multi_cam_image_queue.get()-- if self._save_to_mp4:- if self._output_video_object is None:- self.initialize_video(multi_cam_image)- self._output_video_object.write(multi_cam_image)-- cv2.imshow(self._rec_name, multi_cam_image)- key = cv2.waitKey(1)-- if key == 27: # exit on ESC- self.exit_event.set()-- if cv2.getWindowProperty(self._rec_name, cv2.WND_PROP_VISIBLE) < 1: #break loop if window closed- self.exit_event.set()-- cv2.destroyAllWindows()- if self._save_to_mp4:- self._output_video_object.release()- rich_console.rule('Shutting down MultiCamera Viewer')- self.exit_event.set() #send the 'Exit' signal to everyone.-- ###- ###- ### ββ βββ ββ ββ ββββββββ ββββββ ββ ββ ββββββββ ββ ββ ββ ββββββ- ### ββ ββββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ### ββ ββ ββββ ββ ββ ββββββ ββββββ ββ ββββ ββ ββββββ- ###- ###-- def initialize_video(self, multi_cam_image):- """use the multi_cam_image to initialize the video- multicam image will be of size [cam_resolution_width*num_cams by cam_resolution_height]- Args:- multi_cam_image ([type]): [description]- """- #create video save object- self._multi_cam_image_height, self._multi_cam_image_width, channels = multi_cam_image.shape-- self.multi_cam_image_size = ( self._multi_cam_image_width, self._multi_cam_image_height)- fourcc = cv2.VideoWriter_fourcc(*'DIVX')- self._outputVid_fileName = str(self._save_path / self._rec_name) + '_outVid.mp4'- fps = 30 #this is a bad and stupid guess, but it's actually kinda tricky to guess what is the right thing to put here. I'll fix this later and expect videos to be a bit faster than usual :(- self._output_video_object = cv2.VideoWriter(self._outputVid_fileName, fourcc, fps, self.multi_cam_image_size)- ##- ##- ## ββββββ ββ ββββββ ββββββββ ββββββββ ββ βββ βββ βββββββ βββββββ ββββββββ βββββ βββ βββ ββββββ βββββββ- ## ββ ββ ββ ββ ββ ββ ββ ββ ββββ ββββ ββ ββ ββ ββ ββ ββββ ββββ ββ ββ ββ- ## ββββββ ββ ββ ββ ββ ββ ββ ββ ββββ ββ βββββ βββββββ ββ βββββββ ββ ββββ ββ ββββββ βββββββ- ## ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ- ## ββ βββββββ ββββββ ββ ββ ββ ββ ββ βββββββ βββββββ ββ ββ ββ ββ ββ ββ βββββββ- ##- ##-- def create_diagnostic_images(self):- """plot some diagnostics to assess quality of camera sync"""- try:- camTimestamps = (self.each_cam_timestamps_unix_ns-self.init_start_time)/1e9 #subtract start time and convert to sec-- npy_save_path = self._save_path /'multi_cam_timestamps_frameNum_camNum.npy'- np.save(str(npy_save_path), camTimestamps)- rich_console.log('saving timestamp data to - ' + str(npy_save_path) )-- meanMultiFrameTimestamp = np.mean(camTimestamps, axis=1)- meanMultiFrameTimespan = np.max(camTimestamps, axis=1) - np.min(camTimestamps, axis=1) #what was the timespan covered by each frame- plt.ion()- fig = plt.figure(figsize=(18,10))- max_frame_duration = .1- ax1 = plt.subplot(231, title='Camera Frame Timestamp vs Frame#', xlabel='Frame#', ylabel='Timestamp (sec)')- ax2 = plt.subplot(232, ylim=(0,max_frame_duration), title='Camera Frame Duration Trace', xlabel='Frame#', ylabel='Duration (sec)')- ax3 = plt.subplot(233, xlim=(0,max_frame_duration), title='Camera Frame Duration Histogram (count)', xlabel='Duration(s, 1ms bins)', ylabel='Probability')- ax4 = plt.subplot(234, title='MuliFrame Timestamp vs Frame#', xlabel='Frame#', ylabel='Timestamp (sec)')- ax5 = plt.subplot(235, ylim=(0,max_frame_duration), title='Multi Frame Duration/Span Trace', xlabel='Frame#', ylabel='Duration (sec)')- ax6 = plt.subplot(236, xlim=(0,max_frame_duration), title='MultiFrame Duration Histogram (count)', xlabel='Duration(s, 1ms bins)', ylabel='Probability')-- for camNum in range(self.num_cams):- thisCamTimestamps = camTimestamps[:,camNum]- ax1.plot(thisCamTimestamps, label='Camera#'+str(camNum))- ax1.legend()- ax2.plot(np.diff(thisCamTimestamps),'.')- ax3.hist(np.diff(thisCamTimestamps), bins=np.arange(0,max_frame_duration,.001), alpha=0.5)-- ax4.plot(meanMultiFrameTimestamp, color='darkslategrey', label='MultiFrame'+str(camNum))- ax5.plot(np.diff(meanMultiFrameTimestamp),'.',color='darkslategrey', label='Frame Duration')- ax5.plot(meanMultiFrameTimespan, '.', color='orangered', label='Frame TimeSpan')- ax5.legend()- ax6.hist(np.diff(meanMultiFrameTimestamp), bins=np.arange(0,max_frame_duration,.001), density=True, alpha=0.5, color='darkslategrey', label='Frame Duration')- ax6.hist(np.diff(meanMultiFrameTimespan), bins=np.arange(0,max_frame_duration,.001), density=True, alpha=0.5, color='orangered', label='Frame Timespan')- ax5.legend()-- fig_save_path = self._save_path / 'recording_diagnostics.png'- plt.savefig(str(fig_save_path))- rich_console.log('Saving diagnostic figure to - '+str(fig_save_path))- plt.show()- plt.waitforbuttonpress()- except:- rich_console.print_exception()- f=9-- ###- ### βββββββ ββββββββ ββββββ- ### ββ ββ ββ- ### βββββ ββ ββ- ### ββ ββ ββ- ### βββββββ ββ ββββββ ββ ββ ββ- ###-- def __enter__(self):- """Context manager - No need to do anything special on start"""- return self-- def __exit__(self, exc_type, exc_value, exc_traceback):- """Context manager - on exit, set `self.exit_event` to activate shutdown sequence """- return self-###-###-### ββ βββββββ βββ βββ βββββ ββ βββ ββ-### ββ ββ ββββ ββββ ββ ββ ββ ββββ ββ-### ββ βββββ ββ ββββ ββ βββββββ ββ ββ ββ ββ-### ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ ββ-### ββ ββ βββββββ βββββββ ββ ββ ββ ββ ββ ββ ββββ βββββββ βββββββ-###-###---if __name__ == '__main__':- pathos_mp_helper.freeze_support()- console = Console() #create rich console to catch and print exceptions- try:- fmc_multi_cam_recorder = FMC_MultiCameraRecorder()- fmc_multi_cam_recorder.start()-- except Exception:- console.print_exception()--