Skip to content

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

TypeScript

correctly opens all cams and starts stuffing frames into a queueue!

Jon26c2de2d9b06
diff --git a/jon_scratch/multi_camera_handler.py b/jon_scratch/multi_camera_handler.py
new file mode 100644
index 00000000..5b295a9a
--- /dev/null
+++ b/jon_scratch/multi_camera_handler.py
@@ -0,0 +1,112 @@
+import logging
+from typing import List
+import threading
+import queue
+import concurrent.futures
+
+from pydantic import BaseModel
+import cv2
+import numpy as np
+import matplotlib.pyplot as plt
+
+from opencv_camera import OpenCVCamera
+from freemocap.prod.cam.cam_detection import DetectPossibleCameras
+
+logger = logging.getLogger(__name__)
+logger.level = logging.INFO
+
+MAX_NUMBER_OF_CAMERAS = 20
+
+def stuff_incoming_frames_into_a_queue(camera: OpenCVCamera, thread_queue: queue.Queue):
+ print('stuff_incoming_frames_into_a_queue starting for {}'.format(camera.name))
+
+ while thread_exit_event.is_set() is False:
+ success, image, timestamp_ns = camera.get_next_frame()
+ logger.info('{} grabed a frame at timestamp {}'.format(camera.name, timestamp_ns/1e9))
+ if not success:
+ logger.error('{} failed to grab a frame at timestamp {}'.format(camera.name, timestamp_ns/1e9))
+ thread_queue.put((success, camera.name, image, timestamp_ns))
+
+
+
+
+def start_camera_thread(camera: OpenCVCamera, incoming_frames_queue: queue.Queue):
+ print('run_camera_in_thread starting for {}'.format(camera.name))
+ camera_thread = threading.Thread(
+ target=stuff_incoming_frames_into_a_queue,
+ args=(camera, incoming_frames_queue),
+ name=camera.name+'-thread')
+ camera_thread.start()
+ return camera_thread
+
+
+
+
+def create_figure(number_of_cameras: int, camera_names: List[str]):
+ plt.ion()
+ fig = plt.figure()
+ data_ax = fig.add_subplot(1,number_of_cameras+1,1)
+ data_ax.set_title('Camera Timestamps')
+ data_ax.set_xlabel('Frame #')
+ data_ax.set_ylabel('Timestamp (ns)')
+
+ return fig
+
+# def update_figure(fig, incoming_frame_tuple):
+# data_ax.cla()
+# data_ax.
+
+
+if __name__ == '__main__':
+
+ camera_found_bool = True
+ camera_list = []
+ this_port_number = -1
+ while camera_found_bool:
+ this_port_number+=1
+ camera = OpenCVCamera(port_number=this_port_number)
+ camera_found_bool = camera.connect()
+ if camera_found_bool:
+ camera_list.append(camera)
+
+ global thread_exit_event
+ thread_exit_event = threading.Event()
+ loop_count = 0
+ camera_thread_list = [None]*len(camera_list)
+ incoming_frames_queue = queue.Queue()
+
+ for this_camera in camera_list:
+ camera_thread_list[this_camera.port_number] = start_camera_thread(this_camera, incoming_frames_queue)
+ # camera_thread_list[this_camera.port_number].join()
+
+ dict_of_timestamp_lists = {}
+ for this_camera in camera_list:
+ dict_of_timestamp_lists[this_camera.name] = []
+
+ while thread_exit_event.is_set() is False:
+ if not incoming_frames_queue.empty():
+ this_frame_tuple = incoming_frames_queue.get()
+ this_success_bool, this_camera_name, this_image, this_timestamp_ns = this_frame_tuple
+ dict_of_timestamp_lists[this_camera_name].append(this_timestamp_ns)
+
+
+
+
+
+
+
+ # with concurrent.futures.ThreadPoolExecutor(max_workers=len(camera_list)) as executor:
+ # for this_camera_number, camera in enumerate(camera_list):
+ # camera_future_list[this_camera_number] = executor.submit(show_camera_stream, camera)
+ # while thread_exit_event.is_set() is False:
+ # loop_count+=1
+ # print('loop_count = {}'.format(loop_count))
+ # for this_camera_number, camera in enumerate(camera_list):
+ # camera_future_list[this_camera_number].result()
+
+
+ # for this_camera_number, this_camera in enumerate(camera_list):
+ # print('starting thread for {}'.format(this_camera.name))
+ # camera_thread_list[this_camera_number] = threading.Thread(target=show_camera_stream(this_camera), name=this_camera.name)
+ # camera_thread_list[this_camera_number].start()
+
diff --git a/jon_scratch/opencv_camera.py b/jon_scratch/opencv_camera.py
index 1a8c4cfe..2f8694f8 100644
--- a/jon_scratch/opencv_camera.py
+++ b/jon_scratch/opencv_camera.py
@@ -8,14 +8,7 @@ import cv2
from pydantic import BaseModel
logger = logging.getLogger(__name__)
-
-MAX_PORTS_TO_CHECK = 20
-
-# class CameraFrame(BaseModel):
-# success: bool
-# image: List
-# timestamp_ns: int #mean of timestamps taken before and after `grab`
-# timestamp: float #timestamp in floating point (seconds), e.g. timestamp_ns / 1e9
+logger.level = logging.INFO
class NoCameraAvailableException(Exception):
@@ -24,45 +17,47 @@ class NoCameraAvailableException(Exception):
class FailedFrameGrabException(Exception):
pass
+class TweakedModel(BaseModel):
+ class Config:
+ arbitrary_types_allowed = True
+
# OpenCV Implementation of interacting with a camera
-class OpenCVCamera:
+class OpenCVCamera(TweakedModel):
port_number: int=0
name: str = 'camera0' # `camera{}`.format(port_number)
exposure: int = -6
resolution_width: int = 1280
resolution_height: int = 720
-
+ opencv_video_capture_object: cv2.VideoCapture = None
+
def connect(self)->bool:
if platform.system() == 'Windows':
cap_backend = cv2.CAP_DSHOW
else:
cap_backend = cv2.CAP_ANY
- logger.info('Starting to look for an available camera')
- success = False
- while not success and self.port_number<MAX_PORTS_TO_CHECK:
- self.port_number+=1
- self.opencv_video_capture_object = cv2.VideoCapture(self.port_number, cap_backend)
- success, image = self.opencv_video_capture_object.read()
+ self.opencv_video_capture_object = cv2.VideoCapture(self.port_number, cap_backend)
+ success, image = self.opencv_video_capture_object.read()
- #set camera stream paramters
- 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)
+ #set camera stream paramters
+ 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'))
+ self.opencv_video_capture_object.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
if success:
logger.error('Camera found at port number {}'.format(self.port_number))
self.name = 'camera{}'.format(self.port_number)
return success
else:
- NoCameraAvailableException()
- logger.error('No camera was available!')
+ # raise NoCameraAvailableException()
+ logger.error('Could not connect to a camera at port# {}'.format(self.port_number))
return success
-
+
def get_next_frame(self)->dict:
+
timestamp_ns_pre_grab = time.time_ns()
grab_success = self.opencv_video_capture_object.grab() #Why grab not read? see -> https://stackoverflow.com/questions/57716962/difference-between-video-capture-read-and-grab
timestamp_ns_post_grab = time.time_ns()
@@ -72,9 +67,8 @@ class OpenCVCamera:
success, image = self.opencv_video_capture_object.retrieve()
logger.info('{} successfully grabbed a frame at timestamp {}'.format(self.name, timestamp_ns/1e9))
else:
- FailedFrameGrabException()
+ # raise FailedFrameGrabException()
logger.error('{} failed to grab a frame at timestamp {}'.format(timestamp_ns/1e9))
-
return success, image, timestamp_ns
def close(self):