Skip to content

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

TypeScript

should work

Endurance Idehen82ed0031b0ff
diff --git a/requirements.txt b/requirements.txt
index be640f47..fb70ba7d 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -22,5 +22,5 @@ python-multipart==0.0.5
websockets==10.1
sqlalchemy==1.4.31
singleton-decorator==1.0.0
-
+orjson==3.6.7
# Requirements for core-processor
diff --git a/src/api/routes/camera/cam_data_ws.py b/src/api/routes/camera/cam_data_ws.py
index 0bea91b4..88579408 100644
--- a/src/api/routes/camera/cam_data_ws.py
+++ b/src/api/routes/camera/cam_data_ws.py
@@ -2,6 +2,7 @@ import base64
import logging
import cv2
+import orjson
from fastapi import APIRouter, WebSocket
from jon_scratch.opencv_camera import OpenCVCamera
@@ -13,6 +14,8 @@ cam_ws_router = APIRouter()
@cam_ws_router.websocket("/ws/{webcam_id}")
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 directlu
cam = OpenCVCamera(port_number=webcam_id)
cam.connect()
@@ -26,7 +29,13 @@ async def websocket_endpoint(websocket: WebSocket, webcam_id: str):
success, frame = cv2.imencode('.png', image)
if not success:
continue
- await websocket.send_bytes(base64.b64encode(frame.tobytes()))
+ d = {
+ "frameData": str(frame.tobytes()),
+ # "frameData": base64.b64encode(frame.tobytes()),
+ "timestamp": timestamp,
+ }
+ d = orjson.dumps(d)
+ await websocket.send_bytes(d)
except:
logger.info(f"Camera {webcam_id} is now closed.")
cam.close()
diff --git a/src/core_processor/app_events/app_queue.py b/src/core_processor/app_events/app_queue.py
index 29a2e73c..1797a255 100644
--- a/src/core_processor/app_events/app_queue.py
+++ b/src/core_processor/app_events/app_queue.py
@@ -1,3 +1,4 @@
+import asyncio
from asyncio import Queue
from singleton_decorator import singleton
@@ -15,7 +16,7 @@ class AppQueue:
if webcam_id not in self._queues:
self._queues[webcam_id] = {"queue": Queue()}
- def get_by_webcam_id(self, webcam_id) -> Queue:
+ def get_by_webcam_id(self, webcam_id: str) -> asyncio.Queue:
return self._queues[webcam_id]["queue"]
diff --git a/src/core_processor/app_events/event_notify.py b/src/core_processor/app_events/event_notify.py
index 53ce5e4d..393fafd8 100644
--- a/src/core_processor/app_events/event_notify.py
+++ b/src/core_processor/app_events/event_notify.py
@@ -1,3 +1,4 @@
+import asyncio
import logging
from src.core_processor.app_events.app_queue import AppQueue
@@ -20,7 +21,7 @@ class EventNotifier:
async def notify_all_subscribers(self):
while True:
for webcam_id in self._webcam_ids:
- queue = self._app_queues.get_by_webcam_id(webcam_id)
+ queue = self._app_queues.get_by_webcam_id(webcam_id) # type: asyncio.Queue
try:
message = await queue.get()
queue.task_done()
diff --git a/src/core_processor/board_detection/detect.py b/src/core_processor/board_detection/detect.py
index c6f3889e..a4ae9f74 100644
--- a/src/core_processor/board_detection/detect.py
+++ b/src/core_processor/board_detection/detect.py
@@ -1,3 +1,6 @@
+import logging
+
+logger = logging.getLogger(__name__)
class BoardDetection:
@@ -5,5 +8,5 @@ class BoardDetection:
def __init__(self):
pass
- def process(self, *args, **kwargs):
- pass
\ No newline at end of file
+ def process(self, message, *args, **kwargs):
+ print(message["timestamp"])
diff --git a/src/core_processor/ws_connection.py b/src/core_processor/ws_connection.py
index 012db03f..6500d5ea 100644
--- a/src/core_processor/ws_connection.py
+++ b/src/core_processor/ws_connection.py
@@ -1,6 +1,7 @@
import logging
import websockets
+from orjson import orjson
from src.core_processor.app_events.app_queue import AppQueue
@@ -20,7 +21,9 @@ class WSConnection:
async for websocket in websockets.connect(uri, max_size=None, ping_timeout=None):
try:
async for message in websocket:
- await queue.put(message)
+ # How expensive is it to parse a json string?
+ asJson = orjson.loads(message)
+ await queue.put(asJson)
print(f"webcam_id: {webcam_id}, msg rcv")
except websockets.ConnectionClosed as e:
logger.error(e)