Free Motion Capture for Everyone πβ¨
TypeScript
small refactor
Endurance Idehen984010a54887
diff --git a/freemocap/dev_scratchpad/dev_alpha_old/FMC_MultiCamera.py b/freemocap/dev_scratchpad/dev_alpha_old/FMC_MultiCamera.pyindex a5bddc89..f01a37bd 100644--- a/freemocap/dev_scratchpad/dev_alpha_old/FMC_MultiCamera.py+++ b/freemocap/dev_scratchpad/dev_alpha_old/FMC_MultiCamera.py@@ -7,7 +7,7 @@## ββ ββ ββ ββββββ ββ ββ ββββββ βββββββ ββ ββ ββββββ ββ ββ ββ ββ βββββββ ββ ββ ββ ββ####-#Font - ANSI Regular - https://patorjk.com/software/taag/#p=display&f=ANSI%20Regular&t=Play%20Skeleton%20Animation+# Font - ANSI Regular - https://patorjk.com/software/taag/#p=display&f=ANSI%20Regular&t=Play%20Skeleton%20Animationfrom rich.repr import T@@ -30,8 +30,10 @@ from rich import printfrom rich import inspectfrom rich.progress import trackfrom rich.traceback import install+install(show_locals=True)from rich.console import Console+rich_console = Console()from rich.progress import track@@ -48,68 +50,74 @@ 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+ 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, lolif rec_name is None:- self._rec_name = datetime.datetime.now().strftime("sesh_%Y-%m-%d_%H_%M_%S_mc")+ 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)+ 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:+ 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._path_to_log_file = Path(+ str(self._save_path / self._rec_name) + "_log.txt"+ )self._save_multi_cam_to_mp4 = save_multi_cam_to_mp4if self._save_multi_cam_to_mp4:- self._output_multi_cam_video_object = None #build this after we get the first multi_frame_image+ 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_mp4if self._save_each_cam_to_mp4:- self._each_cam_video_writer_object_list = None #build this after we get the first multi_frame_image-+ 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_camsself._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_boolself.num_frames = 0- rich_console.rule('Starting FreeMoCap MultiCam!')----+ rich_console.rule("Starting FreeMoCap MultiCam!")######@@ -133,9 +141,9 @@ class FMC_MultiCamera:@propertydef 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).- """+ """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@@ -148,8 +156,9 @@ class FMC_MultiCamera:return self._init_start_time@property- def each_cam_timestamps_unix_ns(self): #not really a property though?+ def each_cam_timestamps_unix_ns(self): # not really a property though?return self._each_cam_timestamps_unix_ns+######### βββ βββ βββββββ ββββββββ ββ ββ ββββββ ββββββ βββββββ@@ -175,7 +184,6 @@ class FMC_MultiCamera: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?@@ -198,21 +206,25 @@ class FMC_MultiCamera:"""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!!!+ 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':+ if platform.system() == "Windows":capBackend = cv2.CAP_DSHOWelse:capBackend = cv2.CAP_ANYself._cams_to_use_list = []- for camNum in range(num_cams_to_check):#track(range(num_cams_to_check), description='[green]Finding available cameras...' ):+ 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)+ 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+ 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()@@ -225,16 +237,16 @@ class FMC_MultiCamera: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)@@ -243,38 +255,41 @@ class FMC_MultiCamera: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.barrier = mrManager.Barrier(+ self.num_cams + 1+ ) # each cam + one more for the frame grabberself.exit_event = mrManager.Event()self._each_cam_timestamps_unix_ns = Noneself._cam_process = [None] * self.num_cams- rich_console.rule('starting multi_cam_process_pool')+ 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+ 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_camsif self._rotation_codes_list is None:- self._rotation_codes_list = [None]*self.num_cams+ self._rotation_codes_list = [None] * self.num_camsnum_jobs = self.num_camswith 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')+ 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()--######### ββββββ ββββββ βββββ ββββββ ββ βββ ββ ββββββ ββββββ βββ βββ ββ βββ ββ ββββββ ββββββββ ββ ββ ββββββ ββ βββββββ βββββββ@@ -285,41 +300,48 @@ class FMC_MultiCamera:######-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.barrier.wait() # wait until each camera has grabbed an image and tagged their `barrier.wait()` signalsself.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_images_list = [None] * self.num_cams # empty list of size (numCam)+ this_multi_cam_tuple_as_a_list = [None] * self.num_camsthese_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.port_number(this_cam_num)+ this_cam_index = self.cams_to_use_list.webcam_id(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_as_a_list[+ this_cam_index+ ] = this_cam_image_timestamp_tuplethis_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+ self._each_cam_timestamps_unix_ns = these_timestampselse:- 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))-+ 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- rich_console.log('Created a multi_cam_tuple - queue size: {}'.format(self.multi_cam_tuple_queue.qsize()))+ 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)@@ -330,14 +352,16 @@ class FMC_MultiCamera: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)--+ 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+ 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)+ 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()))@@ -353,9 +377,8 @@ class FMC_MultiCamera:### 6def 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')+ """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():@@ -367,7 +390,6 @@ class FMC_MultiCamera: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:@@ -375,7 +397,9 @@ class FMC_MultiCamera: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])+ 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)@@ -383,7 +407,9 @@ class FMC_MultiCamera:if key == 27: # exit on ESCbreak- if cv2.getWindowProperty(self._rec_name, cv2.WND_PROP_VISIBLE) < 1: #break loop if window closed+ if (+ cv2.getWindowProperty(self._rec_name, cv2.WND_PROP_VISIBLE) < 1+ ): # break loop if window closedbreakcv2.destroyAllWindows()@@ -395,20 +421,17 @@ class FMC_MultiCamera: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.-+ 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)+ 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----+ this_cam_image = multi_cam_tuple[this_cam_num][+ 1+ ] # and that's how you navigate nested tuples, lol######@@ -426,38 +449,62 @@ class FMC_MultiCamera: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)-+ # 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._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 = self._save_path / "Synchronized_Videos"self.syncedVidsPath.mkdir(parents=True)for this_cam_num in range(self.num_cams):- #create video save object+ # create video save objectthis_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_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,+ )- 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)######@@ -472,47 +519,114 @@ class FMC_MultiCamera: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+ 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'+ 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) )+ 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+ meanMultiFrameTimespan = np.max(camTimestamps, axis=1) - np.min(+ camTimestamps, axis=1+ ) # what was the timespan covered by each frameplt.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')+ fig = plt.figure(figsize=(18, 10))+ max_frame_duration = 0.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))+ 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')+ ax2.plot(np.diff(thisCamTimestamps), ".")+ ax3.hist(+ np.diff(thisCamTimestamps),+ bins=np.arange(0, max_frame_duration, 0.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')+ ax6.hist(+ np.diff(meanMultiFrameTimestamp),+ bins=np.arange(0, max_frame_duration, 0.001),+ density=True,+ alpha=0.5,+ color="darkslategrey",+ label="Frame Duration",+ )+ ax6.hist(+ np.diff(meanMultiFrameTimespan),+ bins=np.arange(0, max_frame_duration, 0.001),+ density=True,+ alpha=0.5,+ color="orangered",+ label="Frame Timespan",+ )ax5.legend()- fig_save_path = self._save_path / 'recording_diagnostics.png'+ 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))+ rich_console.log("Saving diagnostic figure to - " + str(fig_save_path))plt.show()plt.waitforbuttonpress()except:rich_console.print_exception()- f=9+ f = 9###### βββββββ ββββββββ ββββββ@@ -527,9 +641,11 @@ class FMC_MultiCamera:return selfdef __exit__(self, exc_type, exc_value, exc_traceback):- """Context manager - on exit, set `self.exit_event` to activate shutdown sequence """+ """Context manager - on exit, set `self.exit_event` to activate shutdown sequence"""self.create_diagnostic_images()return self++######### ββ βββββββ βββ βββ βββββ ββ βββ ββ@@ -541,31 +657,44 @@ class FMC_MultiCamera:###-if __name__ == '__main__':+if __name__ == "__main__":pathos_mp_helper.freeze_support()- console = Console() #create rich console to catch and print exceptions+ console = Console() # create rich console to catch and print exceptionsimport socket- this_computer_name = socket.gethostname()- freemocap_data_path=None- in_rotation_codes_list=None+ this_computer_name = socket.gethostname()- 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')+ 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 = 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/prod/cam/opencv_camera.py b/freemocap/prod/cam/opencv_camera.pydeleted file mode 100644index 44f3d1c9..00000000--- a/freemocap/prod/cam/opencv_camera.py+++ /dev/null@@ -1,41 +0,0 @@-import logging--import cv2-from pydantic import BaseModel--logger = logging.getLogger(__name__)---class OpenCVCameraOptions(BaseModel):- port_number: int- exposure: int = -5- resolution_width: int = 1280- resolution_height: int = 720- cv2_backend: int---# # OpenCV Implementation of interacting with a camera-class OpenCVCamera:-- def __init__(self, options: OpenCVCameraOptions):- self._cam_object = None- self._options = options-- def connect(self):- self._cam_object = self._create_cv_cam_object()-- @property- def cv2_object(self):- return self._cam_object-- # other methods in this class for manipulating the camera- def _create_cv_cam_object(self):- cam_object = cv2.VideoCapture(self._options.port_number, self._options.cv2_backend)- success, image = cam_object.read()- if success:- logger.info(f'Camera found at port number {self._options.port_number}')- return cam_object- else:- error_text = f'Could not connect to a camera at port# {self._options.port_number}'- logger.error(error_text)- raise SystemError(error_text)diff --git a/freemocap_blender_addon/load_freemocap_data_into_blender.py b/freemocap_blender_addon/load_freemocap_data_into_blender.pyindex ab8f0af5..7b11dfa1 100644--- a/freemocap_blender_addon/load_freemocap_data_into_blender.py+++ b/freemocap_blender_addon/load_freemocap_data_into_blender.py@@ -1,21 +1,22 @@import numpy as np-import bpy+import bpyfrom pathlib import Path-import json+import jsonfrom mathutils import Vectorfrom rich import print, inspectfrom rich.progress import trackfrom rich.console import Console-console = Console()++console = Console()console.rule()console.rule()-console.rule('Hello! getting started with FreeMoCap Loading Stuff :D')+console.rule("Hello! getting started with FreeMoCap Loading Stuff :D")console.rule()-#load mediapipe 3d skeleton data+# load mediapipe 3d skeleton datasessionID = "sesh_2021-12-30_13_57_13_cleanish"@@ -24,71 +25,90 @@ session_path = freemocap_data_path / sessionIDdata_path = session_path / "DataArrays"mediapipe_skel_data_path = data_path / "mediaPipeSkel_3d_smoothed.npy"-console.rule(style = "color({})".format("blue"))+console.rule(style="color({})".format("blue"))console.print("Loading in data from FreeMoCap SessionID: " + sessionID)-console.rule(style = "color({})".format("blue"))+console.rule(style="color({})".format("blue"))mediapipe_skel_fr_mar_dim = np.load(str(mediapipe_skel_data_path))-mediapipe_skel_fr_mar_dim = mediapipe_skel_fr_mar_dim/1000 #convert from mm to meters--number_of_frames, number_of_tracked_points, number_of_dimensions = mediapipe_skel_fr_mar_dim.shape-#load default mediapipe layout definitions--mediapipe_definitions_json_path = Path(r"C:/Users/jonma/Dropbox/GitKrakenRepos/freemocap/freemocap/dev_scratchpad/dev_alpha/default_json_definitions/freemocap_mediapipe_json.json")--with open(mediapipe_definitions_json_path ) as json_file:+mediapipe_skel_fr_mar_dim = (+ mediapipe_skel_fr_mar_dim / 1000+) # convert from mm to meters++(+ number_of_frames,+ number_of_tracked_points,+ number_of_dimensions,+) = mediapipe_skel_fr_mar_dim.shape+# load default mediapipe layout definitions++mediapipe_definitions_json_path = Path(+ r"C:/Users/jonma/Dropbox/GitKrakenRepos/freemocap/freemocap/dev_scratchpad/dev_alpha/default_json_definitions/freemocap_mediapipe_json.json"+)++with open(mediapipe_definitions_json_path) as json_file:mediapipe_definitions_dict = json.load(json_file)tracked_point_names = mediapipe_definitions_dict["body"]["tracked_point_names"]-bpy.ops.object.empty_add(type='ARROWS',location=(0,0,0))+bpy.ops.object.empty_add(type="ARROWS", location=(0, 0, 0))-#plop empties at origin with names from mediapipe-empty_scale_factor = .05+# plop empties at origin with names from mediapipe+empty_scale_factor = 0.05for this_tracked_point_number in range(len(tracked_point_names)):- bpy.ops.object.add(type='EMPTY')+ bpy.ops.object.add(type="EMPTY")this_empty = bpy.context.active_objectthis_empty = bpy.context.active_object- this_empty.name = tracked_point_names[this_tracked_point_number]- this_empty.scale = (empty_scale_factor,empty_scale_factor,empty_scale_factor)+ this_empty.name = tracked_point_names[this_tracked_point_number]+ this_empty.scale = (empty_scale_factor, empty_scale_factor, empty_scale_factor)++bpy.context.scene.unit_settings.length_unit = "METERS"+bpy.ops.object.mode_set(mode="OBJECT")-bpy.context.scene.unit_settings.length_unit = 'METERS'-bpy.ops.object.mode_set(mode='OBJECT')--for frame_number in track(range(number_of_frames), description="[cyan]Loading tracked point trajectories as keyframed empties...", total=mediapipe_skel_fr_mar_dim.shape[0] ):- bpy.context.scene.frame_set(frame_number)+for frame_number in track(+ range(number_of_frames),+ description="[cyan]Loading tracked point trajectories as keyframed empties...",+ total=mediapipe_skel_fr_mar_dim.shape[0],+):+ bpy.context.scene.frame_set(frame_number)for this_empty_name in tracked_point_names:this_empty = bpy.data.objects[this_empty_name]- this_empty.location = (mediapipe_skel_fr_mar_dim[frame_number,tracked_point_names.port_number(this_empty_name),0],- mediapipe_skel_fr_mar_dim[frame_number,tracked_point_names.port_number(this_empty_name),1],- mediapipe_skel_fr_mar_dim[frame_number,tracked_point_names.port_number(this_empty_name),2])- this_empty.keyframe_insert(data_path='location', frame=frame_number)+ this_empty.location = (+ mediapipe_skel_fr_mar_dim[+ frame_number, tracked_point_names.webcam_id(this_empty_name), 0+ ],+ mediapipe_skel_fr_mar_dim[+ frame_number, tracked_point_names.webcam_id(this_empty_name), 1+ ],+ mediapipe_skel_fr_mar_dim[+ frame_number, tracked_point_names.webcam_id(this_empty_name), 2+ ],+ )+ this_empty.keyframe_insert(data_path="location", frame=frame_number)-####################### There are now EMPTIES in the scene!####################-console.rule(style = "color({})".format("magenta"))+console.rule(style="color({})".format("magenta"))console.print("[magenta] Generating Meta Rig \o/")-console.rule(style = "color({})".format("magenta"))+console.rule(style="color({})".format("magenta"))bpy.ops.object.armature_human_metarig_add()metarig = bpy.context.active_object-metarig_scale = .85-metarig.scale = (metarig_scale,metarig_scale,metarig_scale)+metarig_scale = 0.85+metarig.scale = (metarig_scale, metarig_scale, metarig_scale)bpy.ops.pose.rigify_generate()-console.rule(style = "color({})".format("magenta"))+console.rule(style="color({})".format("magenta"))-console.rule(style = "color({})".format("yellow"))+console.rule(style="color({})".format("yellow"))console.print("[yellow] Driving limb FK whosits with empty location data")-console.rule(style = "color({})".format("yellow"))+console.rule(style="color({})".format("yellow"))## if not nan@@ -99,25 +119,25 @@ console.rule(style = "color({})".format("yellow"))## 1) i think just skip? later we can interpolate IK/FK and whatnot## on first frame## set all IK stretch to 0-#-#for frame_number in track(range(number_of_frames), description="[yellow]Driving rig FK controllers with empty locations...", total=mediapipe_skel_fr_mar_dim.shape[0] ):-# bpy.context.scene.frame_set(frame_number)+#+# for frame_number in track(range(number_of_frames), description="[yellow]Driving rig FK controllers with empty locations...", total=mediapipe_skel_fr_mar_dim.shape[0] ):+# bpy.context.scene.frame_set(frame_number)# for this_empty_number, this_empty_name in enumerate(tracked_point_names):-#+## this_point_connections_dict = mediapipe_definitions_dict["body"]['tracked_point_to_rigify_fk_rig_bone_connections'][this_empty_name]-#+## this_empty_location = Vector(mediapipe_skel_fr_mar_dim[frame_number,this_empty_number,:])-#+## if this_point_connections_dict:# for this_bone_name, this_bone_connection_dict in this_point_connections_dict.items():-#+## bpy.ops.object.mode_set(mode='EDIT')# if this_bone_connection_dict["track_type"] == "bone_tail":# if this_bone_name == "head" or this_bone_name == "toe":# this_bone_obj = bpy.data.objects["rig"].data.bones[this_bone_name]## this_bone_obj.tail = this_empty_location-# inspect(this_bone_obj, methods=True)+# inspect(this_bone_obj, methods=True)# break-#-#-#+#+#+#diff --git a/jon_scratch/opencv_camera.py b/jon_scratch/opencv_camera.pyindex 30656f0a..504b66b5 100644--- a/jon_scratch/opencv_camera.py+++ b/jon_scratch/opencv_camera.py@@ -1,8 +1,8 @@import logging+import mathimport platformimport threadingimport time-from typing import Any, Listimport cv2import numpy as np@@ -10,32 +10,15 @@ from pydantic import BaseModel, PrivateAttrfrom collections import dequelogger = logging.getLogger(__name__)-logger.level = logging.INFO---class NoCameraAvailableException(Exception):- pass---class FailedFrameGrabException(Exception):- pass---class TweakedModel(BaseModel):- class Config:- arbitrary_types_allowed = True# OpenCV Implementation of interacting with a camera-class OpenCVCamera(TweakedModel):+class OpenCVCamera(BaseModel):port_number: int = 0name: str = f"Camera {port_number}"- # exposure: int = 0exposure: int = -6resolution_width: int = 800resolution_height: int = 600- # resolution_width: int = 1280- # resolution_height: int = 720opencv_video_capture_object: cv2.VideoCapture = None_is_capturing_frames: bool = PrivateAttr(False)@@ -44,10 +27,18 @@ class OpenCVCamera(TweakedModel):_fps: float = PrivateAttr(0)_num_frames_processed: int = PrivateAttr(0)_frame: tuple = PrivateAttr(None)+ _elapsed: float = PrivateAttr(0)++ class Config:+ arbitrary_types_allowed = True@propertydef current_fps(self):- return self._fps+ if self._elapsed <= 0:+ return 0+ if self._num_frames_processed <= 0:+ return 0+ return int(math.ceil(self._num_frames_processed / self._elapsed))def connect(self):if platform.system() == "Windows":@@ -61,7 +52,6 @@ class OpenCVCamera(TweakedModel):success, image = self.opencv_video_capture_object.read()if not success:- # raise NoCameraAvailableException()logger.error("Could not connect to a camera at port# {}".format(self.port_number))@@ -83,6 +73,8 @@ class OpenCVCamera(TweakedModel):if success:logger.debug(f"Camera found at port number {self.port_number}")self.name = f"Camera {self.port_number}"+ fps_input_stream = int(self.opencv_video_capture_object.get(5))+ print("FPS of webcam hardware/input stream: {}".format(fps_input_stream))return successdef start_frame_capture(self):@@ -92,7 +84,6 @@ class OpenCVCamera(TweakedModel):def stop_frame_capture(self):self._is_capturing_frames = False- print("Thread should be stopped")def latest_frame(self):if not self._frame:@@ -107,13 +98,13 @@ class OpenCVCamera(TweakedModel):self._frame = (success, image, timestamp)self._last_100_frames.append((success, image, timestamp))self._num_frames_processed += 1- elapsed = time.time() - start- self._fps = self._num_frames_processed / elapsed+ self._elapsed = time.time() - startdef get_next_frame(self):timestamp_ns_pre_grab = time.time_ns()# Why grab not read? see ->- # https://stackoverflow.com/questions/57716962/difference-between-video-capture-read-and-grab+ # https://stackoverflow.com/questions/57716962/difference-between-video-capture-read-and+ # -grabgrab_success = self.opencv_video_capture_object.grab()timestamp_ns_post_grab = time.time_ns()timestamp_ns = (timestamp_ns_pre_grab + timestamp_ns_post_grab) / 2@@ -128,36 +119,4 @@ class OpenCVCamera(TweakedModel):def close(self):self.opencv_video_capture_object.release()- logger.info("Closed camera{}".format(self.name))---if __name__ == "__main__":- from rich.console import Console-- console = Console()- timestamps = []- try:- # Test the camera- camera = OpenCVCamera()- camera.connect()-- while True:- isSuccessful, image, timestamp_ns = camera.get_next_frame()- timestamps.append(timestamp_ns / 1e9)- if isSuccessful:- mean_fps = 1 / np.mean(np.diff(timestamps))- console.print(- f"{camera.name} grabbed a frame at timestamp {timestamp_ns / 1e9} : mean fps = {mean_fps}"- )- else:- console.print(- f"{camera.name} failed to grab a frame at timestamp {timestamp_ns / 1e9} : mean fps = {mean_fps}"- )- cv2.imshow(camera.name + " - Press ESC to close", image)- exit_key = cv2.waitKey(1)- if exit_key == 27:- break- cv2.destroyAllWindows()- camera.close()- except:- console.print_exception()+ logger.info("Closed camera {}".format(self.name))diff --git a/src/README.md b/src/README.mdnew file mode 100644index 00000000..4a40fe2d--- /dev/null+++ b/src/README.md@@ -0,0 +1,5 @@+++Installation instructions for Mac++- brew install python-tk\ No newline at end of filediff --git a/src/api/routes/camera/cam_data_ws.py b/src/api/routes/camera/cam_data_ws.pyindex b61264ae..1f2c60dd 100644--- a/src/api/routes/camera/cam_data_ws.py+++ b/src/api/routes/camera/cam_data_ws.py@@ -4,9 +4,9 @@ from time import perf_counterimport orjsonfrom fastapi import APIRouter, WebSocket-from jon_scratch.opencv_camera import OpenCVCamerafrom src.api.services.board_detect_service import BoardDetectServicefrom src.api.services.mediapipe_detect_service import MediapipeSkeletonDetectionService+from src.cameras.opencv_camera import OpenCVCameralogger = logging.getLogger(__name__)@@ -29,8 +29,7 @@ async def begin_mediapipe_skeleton_detection(model_complexity=2):"""model_complexity can be 1 (faster, less accurate) or 2 (slower, more accurate)"""- service = MediapipeSkeletonDetectionService()- service.model_complexity = model_complexity+ service = MediapipeSkeletonDetectionService(model_complexity)await service.run()@@ -40,7 +39,7 @@ async def websocket_endpoint(websocket: WebSocket, webcam_id: str):await websocket.accept()# TODO: Consider Spawning a new Process here - alleviate main thread issues# We could spawn a new Process here directly- cam = OpenCVCamera(port_number=webcam_id)+ cam = OpenCVCamera(webcam_id=webcam_id)cam.connect()try:while True:diff --git a/src/api/services/board_detect_service.py b/src/api/services/board_detect_service.pyindex eaff247c..c5fed6d1 100644--- a/src/api/services/board_detect_service.py+++ b/src/api/services/board_detect_service.py@@ -1,7 +1,6 @@-from src.core_processor.board_detection.detect import BoardDetection+from src.core_processor.board_detection.board_detection import BoardDetectionclass BoardDetectService:-async def run(self):await BoardDetection().process()diff --git a/src/api/services/mediapipe_detect_service.py b/src/api/services/mediapipe_detect_service.pyindex 8fe38027..af79c61a 100644--- a/src/api/services/mediapipe_detect_service.py+++ b/src/api/services/mediapipe_detect_service.py@@ -1,20 +1,11 @@-import asyncio--from aiomultiprocess.core import get_manager--from src.core_processor.board_detection.detect import BoardDetection-from src.core_processor.mediapipe_skeleton_detection.detect import MediapipeSkeletonDetection-from src.core_processor.processor import start_camera_capture+from src.core_processor.mediapipe_skeleton_detection.mediapipe_skeleton_detection import (+ MediapipeSkeletonDetection,+)class MediapipeSkeletonDetectionService:- model_complexity: int = 1+ def __init__(self, model_complexity: int = 1):+ self._model_complexity = model_complexityasync def run(self):- queue = get_manager().Queue()- await asyncio.gather(- # Producer- start_camera_capture(queue),- # Consumer- MediapipeSkeletonDetection().create_new_process_for_run(queue)- )+ await MediapipeSkeletonDetection().process()diff --git a/freemocap/prod/cam/detection/cam_detection.py b/src/cameras/cam_detection.pysimilarity index 64%rename from freemocap/prod/cam/detection/cam_detection.pyrename to src/cameras/cam_detection.pyindex e963ef78..ca94f124 100644--- a/freemocap/prod/cam/detection/cam_detection.py+++ b/src/cameras/cam_detection.py@@ -3,7 +3,7 @@ import platformimport cv2-from freemocap.prod.cam.detection.dto import FindAvailableResponse, RawCamera+from src.cameras.dto import FindAvailableResponse, RawCameraCAM_CHECK_NUM = 5@@ -11,7 +11,6 @@ logger = logging.getLogger(__name__)class DetectPossibleCameras:-def find_available_cameras(self) -> FindAvailableResponse:cv2_backend = self._determine_backend()@@ -21,11 +20,15 @@ class DetectPossibleCameras:success, image = cap.read()if success:- logger.info(f"camera at port {camNum} found an image with shape {image.shape}")+ logger.info(+ f"camera at port {camNum} found an image with shape {image.shape}"+ )try:- cams_to_use_list.append(RawCamera(- port_number=camNum,- ))+ cams_to_use_list.append(+ RawCamera(+ port_number=camNum,+ )+ )finally:cap.release()@@ -33,12 +36,11 @@ class DetectPossibleCameras:return FindAvailableResponse(camera_found_count=len(cams_to_use_list),cams_to_use=cams_to_use_list,- cv2_backend=cv2_backend+ cv2_backend=cv2_backend,)def _determine_backend(self):- if platform.system() == 'Windows':+ if platform.system() == "Windows":return cv2.CAP_DSHOWelse:return cv2.CAP_ANY-diff --git a/src/cameras/cam_factory.py b/src/cameras/cam_factory.pynew file mode 100644index 00000000..1d14ff56--- /dev/null+++ b/src/cameras/cam_factory.py@@ -0,0 +1,15 @@+from src.cameras.cam_singleton import get_or_create_cams+from src.cameras.opencv_camera import OpenCVCamera+++def create_opencv_cams():+ cams = get_or_create_cams()+ raw_webcam_obj = cams.cams_to_use+ cv_cams = [+ OpenCVCamera(webcam_id=webcam.webcam_id)+ for webcam in raw_webcam_obj+ if int(webcam.webcam_id) < 3+ ]+ for cv_cam in cv_cams:+ cv_cam.connect()+ return cv_camsdiff --git a/freemocap/prod/cam/detection/cam_singleton.py b/src/cameras/cam_singleton.pysimilarity index 81%rename from freemocap/prod/cam/detection/cam_singleton.pyrename to src/cameras/cam_singleton.pyindex 8ad03ebe..3f8fc982 100644--- a/freemocap/prod/cam/detection/cam_singleton.py+++ b/src/cameras/cam_singleton.py@@ -1,6 +1,7 @@-from freemocap.prod.cam.detection.cam_detection import DetectPossibleCameras+from src.cameras.cam_detection import DetectPossibleCameras# No consumer should call this "private" variable+_available_cameras = Nonediff --git a/src/cameras/opencv_camera.py b/src/cameras/opencv_camera.pynew file mode 100644index 00000000..cda8cd2f--- /dev/null+++ b/src/cameras/opencv_camera.py@@ -0,0 +1,126 @@+import logging+import math+import platform+import threading+import time++import cv2+from pydantic import BaseModel, PrivateAttr+from collections import deque++logger = logging.getLogger(__name__)+++class OpenCVCamera(BaseModel):+ """+ Performant implementation of video capture against webcams+ """++ webcam_id: int = 0+ name: str = f"Camera {webcam_id}"+ exposure: int = -6+ resolution_width: int = 800+ resolution_height: int = 600+ opencv_video_capture_object: cv2.VideoCapture = None++ _is_capturing_frames: bool = PrivateAttr(False)+ _running_thread = PrivateAttr(None)+ _last_100_frames: deque = PrivateAttr(deque(maxlen=100))+ _fps: float = PrivateAttr(0)+ _num_frames_processed: int = PrivateAttr(0)+ _frame: tuple = PrivateAttr(None)+ _elapsed: float = PrivateAttr(0)++ class Config:+ arbitrary_types_allowed = True++ @property+ def webcam_id_as_str(self):+ return self.webcam_id_as_str++ @property+ def current_fps(self):+ if self._elapsed <= 0:+ return 0+ if self._num_frames_processed <= 0:+ return 0+ return int(math.ceil(self._num_frames_processed / self._elapsed))++ def connect(self):+ if platform.system() == "Windows":+ cap_backend = cv2.CAP_DSHOW+ else:+ cap_backend = cv2.CAP_ANY++ self.opencv_video_capture_object = cv2.VideoCapture(self.webcam_id, cap_backend)+ success, image = self.opencv_video_capture_object.read()++ if not success:+ logger.error(+ "Could not connect to a camera at port# {}".format(self.webcam_id)+ )+ return success++ # set camera stream parameters+ # self.opencv_video_capture_object.set(cv2.CAP_PROP_EXPOSURE, self.exposure)+ # self.opencv_video_capture_object.set(+ # cv2.CAP_PROP_FRAME_WIDTH, self.resolution_width+ # )+ # self.opencv_video_capture_object.set(+ # cv2.CAP_PROP_FRAME_HEIGHT, self.resolution_height+ # )++ self.opencv_video_capture_object.set(+ cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc("M", "J", "P", "G")+ )++ if success:+ logger.debug(f"Camera found at port number {self.webcam_id}")+ self.name = f"Camera {self.webcam_id}"+ fps_input_stream = int(self.opencv_video_capture_object.get(5))+ print("FPS of webcam hardware/input stream: {}".format(fps_input_stream))+ return success++ def start_frame_capture(self):+ t = threading.Thread(target=self._start_frame_loop, daemon=True)+ t.start()+ self._running_thread = t++ def stop_frame_capture(self):+ self._is_capturing_frames = False++ def latest_frame(self):+ if not self._frame:+ return False, None, None+ return self._frame++ def _start_frame_loop(self):+ self._is_capturing_frames = True+ start = time.time()+ while self._is_capturing_frames:+ success, image, timestamp = self.get_next_frame()+ self._frame = (success, image, timestamp)+ self._last_100_frames.append((success, image, timestamp))+ self._num_frames_processed += 1+ self._elapsed = time.time() - start++ def get_next_frame(self):+ timestamp_ns_pre_grab = time.time_ns()+ # Why grab not read? see ->+ # https://stackoverflow.com/questions/57716962/difference-between-video-capture-read-and+ # -grab+ grab_success = self.opencv_video_capture_object.grab()+ timestamp_ns_post_grab = time.time_ns()+ timestamp_ns = (timestamp_ns_pre_grab + timestamp_ns_post_grab) / 2++ if grab_success:+ success, image = self.opencv_video_capture_object.retrieve()+ # logger.info('{} successfully grabbed a frame at timestamp {}'.format(self.name,+ # timestamp_ns/1e9))+ return success, image, timestamp_ns++ return False, None, None++ def close(self):+ self.opencv_video_capture_object.release()+ logger.info("Closed camera {}".format(self.name))diff --git a/src/config.py b/src/config.pyindex 5966b8d5..c30ca6d6 100644--- a/src/config.py+++ b/src/config.py@@ -1,5 +1,3 @@--import loggingimport logging.handlersimport sys@@ -40,4 +38,4 @@ def configure_logging():def disable_flask_logging(flask_app):flask_app.logger.handlers = []- flask_app.logger.propagate = False\ No newline at end of file+ flask_app.logger.propagate = Falsediff --git a/src/core_processor/board_detection/detect.py b/src/core_processor/board_detection/board_detection.pysimilarity index 86%rename from src/core_processor/board_detection/detect.pyrename to src/core_processor/board_detection/board_detection.pyindex 1d4d74f1..0023d94c 100644--- a/src/core_processor/board_detection/detect.py+++ b/src/core_processor/board_detection/board_detection.py@@ -4,11 +4,11 @@ import loggingimport cv2import numpy as np+from src.cameras.cam_factory import create_opencv_camsfrom src.core_processor.board_detection.base_pose_estimation import detect_charuco_boardfrom src.core_processor.board_detection.charuco_image_annotator import (annotate_image_with_charuco_data,)-from src.core_processor.processor import create_opencv_camsclass BoardDetection:@@ -26,12 +26,11 @@ class BoardDetection:for cv_cam in cv_cams:success, frame, timestamp = cv_cam.latest_frame()if not success:- print("fail")+ logger.error("CV2 failed to grab a frame")continueif frame is None:- print("no frame found")+ logger.error("Frame is empty")continue- port_number = str(cv_cam.port_number)# print(# f"got image of shape {frame.shape} from camera at port {port_number}"# )@@ -43,14 +42,14 @@ class BoardDetection:) = detect_charuco_board(frame)# TODO - Pull out timestamps per frame and calculate fps to display on imagesuccess_bool = annotate_image_with_charuco_data(- frame, port_number, charuco_corners, charuco_ids+ frame, cv_cam.webcam_id_as_str, charuco_corners, charuco_ids)cv2.polylines(frame, np.int32([charuco_corners]), True, (0, 100, 255), 2)self.apply_fps(frame, cv_cam.current_fps)- cv2.imshow(port_number, frame)+ cv2.imshow(cv_cam.webcam_id_as_str, frame)exit_key = cv2.waitKey(1)if exit_key == 27:breakdiff --git a/src/core_processor/main.py b/src/core_processor/main.pyindex ef93071f..e2b1e5c7 100644--- a/src/core_processor/main.py+++ b/src/core_processor/main.py@@ -23,7 +23,7 @@ async def start(webcam_id: str, queue):async def begin_realtime_processing(webcam_ids: Optional[List[str]] = None):if not webcam_ids:cams = DetectPossibleCameras().find_available_cameras().cams_to_use- webcam_ids = [cam.port_number for cam in cams]+ webcam_ids = [cam.webcam_id for cam in cams]manager = get_manager()app_queue = AppQueue(manager)diff --git a/src/core_processor/processor.py b/src/core_processor/processor.pydeleted file mode 100644index e0d23116..00000000--- a/src/core_processor/processor.py+++ /dev/null@@ -1,74 +0,0 @@-import logging-from typing import List, NamedTuple--import numpy as np-from aiomultiprocess import Process-from aiomultiprocess.types import Queue--from freemocap.prod.cam.detection.cam_singleton import get_or_create_cams-from jon_scratch.opencv_camera import OpenCVCamera---def create_opencv_cams():- cams = get_or_create_cams()- raw_webcam_obj = cams.cams_to_use- cv_cams = [- OpenCVCamera(port_number=webcam.port_number)- for webcam in raw_webcam_obj- if int(webcam.port_number) < 3- ]- for cv_cam in cv_cams:- cv_cam.connect()- return cv_cams---async def start_camera_capture(queue: Queue):- processes = []- creator = CameraCaptureProcess()- process = Process(- target=creator.run,- args=(queue,)- )- process.start()- processes.append(process)---class FramePayload(NamedTuple):- port_number: int- image: np.ndarray- timestamp: int---class ImagePayload(NamedTuple):- frames: List[FramePayload] = None---class CameraCaptureProcess:- async def run(self, queue):- cv_cams = create_opencv_cams()- _queue = queue- _logger = logging.getLogger(__name__)- _logger.info("Beginning Camera Capture Process")- while True:- image_list: List[FramePayload] = []- for cv_cam in cv_cams:- success, image, timestamp = cv_cam.get_next_frame()-- if not success:- print(f'failed to read frame from camera at port {str(cv_cam.port_number)}')- continue- if image is None:- print(f'Read `None` frame from camera at port {str(cv_cam.port_number)}')- continue- image_list.append(- FramePayload(- port_number=cv_cam.port_number,- image=image,- timestamp=timestamp- )- )-- _queue.put_nowait(- ImagePayload(frames=image_list)- )-diff --git a/src/core_processor/subscribers/subscribers.py b/src/core_processor/subscribers/subscribers.pyindex 5f6ea6b9..79505898 100644--- a/src/core_processor/subscribers/subscribers.py+++ b/src/core_processor/subscribers/subscribers.py@@ -1,3 +1,3 @@-from src.core_processor.board_detection.detect import BoardDetection+from src.core_processor.board_detection.board_detection import BoardDetectionsubscribers = [BoardDetection]