Free Motion Capture for Everyone πβ¨
TypeScript
pass all queues into board detection routine
Endurance Idehene46eb59752ad
diff --git a/src/api/services/board_detect_service.py b/src/api/services/board_detect_service.pyindex 414a87dc..b5324fa0 100644--- a/src/api/services/board_detect_service.py+++ b/src/api/services/board_detect_service.py@@ -1,7 +1,7 @@import asyncio-from aiomultiprocess.core import get_manager-+from freemocap.prod.cam.detection.cam_singleton import get_or_create_cams+from src.core_processor.app_events.app_queue import AppQueuefrom src.core_processor.board_detection.detect import BoardDetectionfrom src.core_processor.processor import capture_cam_images_new_process@@ -9,13 +9,16 @@ from src.core_processor.processor import capture_cam_images_new_processclass BoardDetectService:async def run(self):- manager = get_manager()- queue = manager.Queue()-+ cams = get_or_create_cams()+ webcam_ids = [+ rawCam.port_number for rawCam in cams.cams_to_use+ ]+ app_queue = AppQueue()+ app_queue.create_all(webcam_ids)+ queues = app_queue.queuesawait asyncio.gather(# Producer- capture_cam_images_new_process(queue),+ capture_cam_images_new_process(queues),# Consumer- BoardDetection().create_new_process_for_run(queue)+ BoardDetection().create_new_process_for_run(queues))-diff --git a/src/core_processor/app_events/app_queue.py b/src/core_processor/app_events/app_queue.pyindex 414e6314..08bef7be 100644--- a/src/core_processor/app_events/app_queue.py+++ b/src/core_processor/app_events/app_queue.py@@ -1,12 +1,25 @@import multiprocessing-from typing import List-# from faster_fifo import Queue+from multiprocessing import Queue+from typing import Dict, List, Optional++from aiomultiprocess.core import get_manager+++class CameraFrameQueue:+ webcam_id: str+ queue: Optional[Queue]++ def __init__(self, webcam_id: str, queue: Queue):+ self.webcam_id = webcam_id+ self.queue = queueclass AppQueue:- _queues = {}+ _queues: Dict[str, CameraFrameQueue] = {}- def __init__(self, manager: multiprocessing.Manager):+ def __init__(self, manager: multiprocessing.Manager = None):+ if not manager:+ manager = get_manager()self._manager: multiprocessing.Manager = managerdef create_all(self, webcam_ids: List[str]):@@ -15,11 +28,17 @@ class AppQueue:def create(self, webcam_id: str):if webcam_id not in self._queues:- self._queues[webcam_id] = {"queue": self._manager.Queue()}+ self._queues[webcam_id] = CameraFrameQueue(+ webcam_id=webcam_id,+ queue=self._manager.Queue()+ )def get_by_webcam_id(self, webcam_id: str):- return self._queues[webcam_id]["queue"]+ return self._queues[webcam_id].queue+ @property+ def queues(self):+ return self._queuesdef create_or_get_queue():passdiff --git a/src/core_processor/board_detection/detect.py b/src/core_processor/board_detection/detect.pyindex 2f419aad..0a69bd1b 100644--- a/src/core_processor/board_detection/detect.py+++ b/src/core_processor/board_detection/detect.py@@ -1,25 +1,30 @@import logging+from typing import Dictimport cv2from aiomultiprocess import Process+from src.core_processor.app_events.app_queue import CameraFrameQueue+logger = logging.getLogger(__name__)class BoardDetection:- async def create_new_process_for_run(self, queue):- p = Process(target=self.process, args=(queue,))+ async def create_new_process_for_run(self, queues: Dict[str, CameraFrameQueue]):+ p = Process(target=self.process, args=(queues,))p.start()await p.join()- async def process(self, queue):+ async def process(self, queues: Dict[str, CameraFrameQueue]):while True:- try:- image, timestamp = queue.get(timeout=.01)- cv2.imshow('blah', image)- exit_key = cv2.waitKey(1)- if exit_key == 27:- break- except:- pass+ for webcam_id in queues:+ queue = queues[webcam_id].queue+ try:+ image, timestamp = queue.get(timeout=.01)+ cv2.imshow(webcam_id, image)+ exit_key = cv2.waitKey(1)+ if exit_key == 27:+ break+ except:+ passdiff --git a/src/core_processor/processor.py b/src/core_processor/processor.pyindex e9f8b976..9e3af2e5 100644--- a/src/core_processor/processor.py+++ b/src/core_processor/processor.py@@ -1,11 +1,12 @@import loggingimport time+from typing import Dictfrom aiomultiprocess import Process-from orjson import orjsonfrom freemocap.prod.cam.detection.cam_singleton import get_or_create_camsfrom jon_scratch.opencv_camera import OpenCVCamera+from src.core_processor.app_events.app_queue import CameraFrameQueuedef create_opencv_cams():@@ -26,12 +27,14 @@ async def capture_cam_images_new_process(queue):await p.join()-async def _start_camera_capture(queue):+async def _start_camera_capture(queues: Dict[str, CameraFrameQueue]):cv_cams = create_opencv_cams()logger = logging.getLogger(__name__)# threading could be added here!while True:for cv_cam in cv_cams:+ webcam_id = str(cv_cam.port_number)+ queue = queues[webcam_id].queuet1_start = time.perf_counter()success, image, timestamp = cv_cam.get_next_frame()if not success: