Free Motion Capture for Everyone πβ¨
TypeScript
multiprocessing working! :D
jon matthis4b8d4e7b92d0
diff --git a/freemocap/dev_scratchpad/dev_alpha/FMC_MultiCameraRecorder.py b/freemocap/dev_scratchpad/dev_alpha/FMC_MultiCameraRecorder.pyindex bbf5a68f..692d1766 100644--- a/freemocap/dev_scratchpad/dev_alpha/FMC_MultiCameraRecorder.py+++ b/freemocap/dev_scratchpad/dev_alpha/FMC_MultiCameraRecorder.py@@ -10,12 +10,14 @@from os import mkdir-from fmc_camera import FMC_Camera+from FMC_Camera import FMC_Cameraimport concurrent.futuresimport loggingimport queueimport threading+import pathos.multiprocessing as pathos_mp+from pathos.helpers import mp as pathos_mp_helperimport timeimport platformimport datetime@@ -32,6 +34,7 @@ from rich.progress import trackfrom rich.traceback import installinstall(show_locals=True)from rich.console import Console+rich_console = Console()from rich.progress import track@@ -80,24 +83,20 @@ class FMC_MultiCameraRecorder: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+')- # self.rich_console = Console(file=log_file_ob)- self.rich_console = Console()+ # rich_console = Console(file=log_file_obj)+ rich_console = Console()self._num_cams = num_camsself._cams_to_use_list = cams_to_use_list- self.exit_event = threading.Event()-- self.rich_console.rule('Starting FreeMoCap MultiCam Recorder!')++ rich_console.rule('Starting FreeMoCap MultiCam Recorder!')+- if self.cams_to_use_list is None:- self.find_available_cameras()- self.start_multi_cam_thread_pool()-######### ββββββ ββββββ ββββββ ββββββ βββββββ ββββββ ββββββββ ββ βββββββ βββββββ@@ -146,7 +145,18 @@ class FMC_MultiCameraRecorder:### ββ ββ βββββββ ββ ββ ββ ββββββ ββββββ βββββββ######+ 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."""@@ -167,7 +177,7 @@ class FMC_MultiCameraRecorder:if cap.isOpened():self._cams_to_use_list.append(camNum)cap.release()- console.print("Found cameras at ", self._cams_to_use_list)+ 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@@ -189,18 +199,65 @@ class FMC_MultiCameraRecorder: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 = Noneself._cam_thread = [None] * self.num_cams- self.rich_console.rule('starting multi_cam_thread_pool')+ 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.thread_barrier = threading.Barrier(self.num_cams, action=self.grab_incoming_cam_tuples)+ 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, thread_barrier = self.thread_barrier, exit_event = self.exit_event, console=self.rich_console)+ 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()++ # self._cam_thread = pp_pool.map(FMC_Camera, cam_input_dict)+ # pool.submit(self.show_multi_cam_cv2)+ # for this_cam_num in range(self.num_cams):+ # cam_input_dict['cam_num'] = this_cam_num+ # self._cam_process[this_cam_num]=mp.Process(target=FMC_Camera, args=(this_cam_num,), kwargs=cam_input_dict )+ # self._cam_process[this_cam_num].start()+ # self._cam_process[this_cam_num].join()++######### ββββββ ββββββ βββββ ββββββ ββ βββ ββ ββββββ ββββββ βββ βββ ββ βββ ββ ββββββ ββββββββ ββ ββ ββββββ ββ βββββββ βββββββ@@ -214,25 +271,30 @@ class FMC_MultiCameraRecorder: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"""-- 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+ # self.show_multi_cam_cv2()- 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)- # self.rich_console.log('Created a multi_cam_image')+ 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()))######@@ -247,7 +309,7 @@ class FMC_MultiCameraRecorder:def show_multi_cam_cv2(self):"""display multi_cam_image using `cv2.imshow` and maybe save it to mp4, who knows?"""- self.rich_console.log('Launching Multi Cam Viewer')+ 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()@@ -269,8 +331,8 @@ class FMC_MultiCameraRecorder:cv2.destroyAllWindows()if self._save_to_mp4:self._output_video_object.release()- self.rich_console.log('Shutting down MultiCamera Viewer')- selfmulti_cam.exit_event.set() #send the 'Exit' signal to everyone+ rich_console.rule('Shutting down MultiCamera Viewer')+ self.exit_event.set() #send the 'Exit' signal to everyone######@@ -308,38 +370,40 @@ class FMC_MultiCameraRecorder:def create_diagnostic_images(self):"""plot some diagnostics to assess quality of camera sync"""-- camTimestamps = (self.each_cam_timestamps_unix_ns-self.init_start_time)/1e9 #subtract start time and convert to sec- np.save(str(self._save_path /'multi_cam_timestamps_frameNum_camNum.npy'), camTimestamps)- 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()-- plt.savefig(str(self._save_path / 'recording_diagnostics.png'))- plt.show()+ try:+ camTimestamps = (self.each_cam_timestamps_unix_ns-self.init_start_time)/1e9 #subtract start time and convert to sec+ np.save(str(self._save_path /'multi_cam_timestamps_frameNum_camNum.npy'), camTimestamps)+ 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()++ plt.savefig(str(self._save_path / 'recording_diagnostics.png'))+ plt.show()+ except:+ rich_console.print_exception()f=9###@@ -350,13 +414,12 @@ class FMC_MultiCameraRecorder:### βββββββ ββ ββββββ ββ ββ ββ###- def __enter__(self):+ def __enter__(self):"""Context manager - No need to do anything special on start"""return selfdef __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######@@ -370,13 +433,11 @@ class FMC_MultiCameraRecorder:if __name__ == '__main__':+ pathos_mp_helper.freeze_support()console = Console() #create rich console to catch and print exceptionstry:- with FMC_MultiCameraRecorder() as multi_cam:- runtime = 10- logging.info("Main: Record for {} seconds".format(runtime))- time.sleep(runtime)- print('shutting down MultiCamRecorder')+ fmc_multi_cam_recorder = FMC_MultiCameraRecorder()+ fmc_multi_cam_recorder.start()except Exception:console.print_exception()diff --git a/freemocap/dev_scratchpad/dev_alpha/fmc_camera.py b/freemocap/dev_scratchpad/dev_alpha/fmc_camera.pyindex c86d4874..490388d1 100644--- a/freemocap/dev_scratchpad/dev_alpha/fmc_camera.py+++ b/freemocap/dev_scratchpad/dev_alpha/fmc_camera.py@@ -34,17 +34,28 @@ cap_default_parameters_dict = {class FMC_Camera:"""Simple class to open a cv2.VideoCapture object, with parts to connect to a `MutliCamRecorder` object"""+ ##+ ##+ ## ββ βββ ββ ββ ββββββββ+ ## ββ ββββ ββ ββ ββ+ ## ββ ββ ββ ββ ββ ββ+ ## ββ ββ ββ ββ ββ ββ+ ## βββββββ βββββββ ββ ββ ββββ ββ ββ βββββββ βββββββ+ ##+ ##+ ##+def __init__(self,cam_num=0,- cap_parameters_dict = cap_default_parameters_dict,- vid_save_path=None,frame_queue = None,- thread_barrier = None,+ barrier = None,exit_event = None,show_cam_stream = False,- console = None,+ 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)@@ -65,18 +76,18 @@ class FMC_Camera:self._cap_parameters_dict = cap_parameters_dictself._frame_queue = frame_queue- self._thread_barrier = thread_barrier+ self._barrier = barrierself._exit_event = exit_eventself._vid_save_path = vid_save_pathself._show_console = show_console- self.rich_console = console # a console object from the Rich python package+ self.rich_console = rich_console # a console object from the Rich python packageif vid_save_path:self._vid_save_path = Path(vid_save_path)- if not console:+ if not self.rich_console:self.rich_console = Console()self.open() #open VideoCapture Object@@ -187,13 +198,17 @@ class FMC_Camera: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['exposure'])+ self.cv2_cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self._cap_parameters_dict['cap_resolution_height'])++ 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)+" VideoCapture started successfully")+ 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, lolself._vid_cap_timestamps_unix_ns = np.empty(0)@@ -239,20 +254,23 @@ class FMC_Camera:"""run camera in a threadput 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._thread_barrier.wait()+ 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()+ def run_in_subprocess(self):+ self.run_in_thread()######