Skip to content

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

TypeScript

make the thing work

Endurance Idehenb18c9d243a7c
diff --git a/src/api/routes/camera/cam_data_ws.py b/src/api/routes/camera/cam_data_ws.py
index 3e97d5a1..bca8b1a4 100644
--- a/src/api/routes/camera/cam_data_ws.py
+++ b/src/api/routes/camera/cam_data_ws.py
@@ -1,14 +1,11 @@
-import asyncio
import logging
from time import perf_counter
import orjson
-from aiomultiprocess.core import get_manager
from fastapi import APIRouter, WebSocket
from jon_scratch.opencv_camera import OpenCVCamera
-from src.core_processor.board_detection.detect import BoardDetection
-from src.core_processor.processor import open_process
+from src.api.services.board_detect_service import BoardDetectService
logger = logging.getLogger(__name__)
@@ -22,15 +19,11 @@ async def start_realtime_capture():
@cam_ws_router.get("/begin_board_detection")
async def begin_board_detection():
- manager = get_manager()
- queue = manager.Queue()
- d = BoardDetection()
- await asyncio.gather(
- open_process(queue),
- d.process(queue)
- )
+ service = BoardDetectService()
+ await service.run()
+## This is for the frontend - this is for showing cam data to something external. nothing else
@cam_ws_router.websocket("/ws/{webcam_id}")
async def websocket_endpoint(websocket: WebSocket, webcam_id: str):
await websocket.accept()
diff --git a/src/api/services/board_detect_service.py b/src/api/services/board_detect_service.py
new file mode 100644
index 00000000..414a87dc
--- /dev/null
+++ b/src/api/services/board_detect_service.py
@@ -0,0 +1,21 @@
+import asyncio
+
+from aiomultiprocess.core import get_manager
+
+from src.core_processor.board_detection.detect import BoardDetection
+from src.core_processor.processor import capture_cam_images_new_process
+
+
+class BoardDetectService:
+
+ async def run(self):
+ manager = get_manager()
+ queue = manager.Queue()
+
+ await asyncio.gather(
+ # Producer
+ capture_cam_images_new_process(queue),
+ # Consumer
+ BoardDetection().create_new_process_for_run(queue)
+ )
+
diff --git a/src/api/services/capture_images.py b/src/api/services/capture_images.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/core_processor/board_detection/detect.py b/src/core_processor/board_detection/detect.py
index 12c8398b..92c8f098 100644
--- a/src/core_processor/board_detection/detect.py
+++ b/src/core_processor/board_detection/detect.py
@@ -1,20 +1,27 @@
import logging
from time import perf_counter
+from aiomultiprocess import Process
from orjson import orjson
logger = logging.getLogger(__name__)
class BoardDetection:
+
+ async def create_new_process_for_run(self, queue):
+ p = Process(target=self.process, args=(queue, ))
+ p.start()
+ await p.join()
+
async def process(self, queue):
while True:
try:
- t1_start = perf_counter()
message = queue.get(timeout=.01)
- message = orjson.loads(message)
- t1_stop = perf_counter()
- logger.info(t1_stop - t1_start)
+ frameData = orjson.loads(message)
+ # frameData["image"]
+ # frameData["timestamp"]
+ # Do your thing
except:
pass
diff --git a/src/core_processor/main.py b/src/core_processor/main.py
index 0163d8df..ef93071f 100644
--- a/src/core_processor/main.py
+++ b/src/core_processor/main.py
@@ -1,5 +1,4 @@
#!/usr/bin/env python
-
import asyncio
import logging
from typing import List, Optional
@@ -30,8 +29,6 @@ async def begin_realtime_processing(webcam_ids: Optional[List[str]] = None):
app_queue = AppQueue(manager)
app_queue.create_all(webcam_ids)
- ev = EventNotifier(webcam_ids)
-
tasks = []
for webcam_id in webcam_ids:
queue = app_queue.get_by_webcam_id(webcam_id)
@@ -39,11 +36,12 @@ async def begin_realtime_processing(webcam_ids: Optional[List[str]] = None):
p.start()
tasks.append(p.join())
- for webcam_id in webcam_ids:
- queue = app_queue.get_by_webcam_id(webcam_id)
- p = Process(target=ev.notify_all_subscribers, args=(queue,))
- p.start()
- tasks.append(p.join())
+ ev = EventNotifier(webcam_ids)
+ # for webcam_id in webcam_ids:
+ # queue = app_queue.get_by_webcam_id(webcam_id)
+ # p = Process(target=ev.notify_all_subscribers, args=(queue,))
+ # p.start()
+ # tasks.append(p.join())
logger.info(f"Process count: {len(tasks)}")
return await asyncio.gather(*tasks)
diff --git a/src/core_processor/processor.py b/src/core_processor/processor.py
index d3890a36..375dbd58 100644
--- a/src/core_processor/processor.py
+++ b/src/core_processor/processor.py
@@ -1,3 +1,6 @@
+import logging
+import time
+
from aiomultiprocess import Process
from orjson import orjson
@@ -7,40 +10,41 @@ from jon_scratch.opencv_camera import OpenCVCamera
def create_opencv_cams():
cams = get_or_create_cams()
- webcam_ids = cams.cams_to_use
+ raw_webcam_obj = cams.cams_to_use
cv_cams = [
OpenCVCamera(port_number=webcam.port_number)
- for webcam in webcam_ids
+ for webcam in raw_webcam_obj
]
for cv_cam in cv_cams:
cv_cam.connect()
return cv_cams
-async def open_process(queue):
- p = Process(target=start_processing, args=(queue,))
- p.start()
- await p.join()
-
-
-async def open_board_detection_process(queue):
- p = Process(target=start_processing, args=(queue,))
+async def capture_cam_images_new_process(queue):
+ p = Process(target=_start_camera_capture, args=(queue,))
p.start()
await p.join()
-async def start_processing(queue):
+async def _start_camera_capture(queue):
cv_cams = create_opencv_cams()
+ logger = logging.getLogger(__name__)
+ # threading could be added here!
while True:
for cv_cam in cv_cams:
+ t1_start = time.perf_counter()
success, image, timestamp = cv_cam.get_next_frame()
if not success:
continue
if image is None:
continue
+ # Whats the performance difference between tuple queue passing vs dict
d = {
- "frameData": str(image.tobytes()),
+ "image": image,
"timestamp": timestamp,
}
d = orjson.dumps(d)
queue.put_nowait(d)
+ t1_stop = time.perf_counter()
+ logger.info(f"Elapsed time per insert into queue: {t1_stop - t1_start}")
+