Skip to content

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

TypeScript

added janky but functional `begin_mediapipe_skeleton_detection` api route

Jon1a5568655968
diff --git a/src/api/routes/camera/cam_data_ws.py b/src/api/routes/camera/cam_data_ws.py
index b80df51f..6d824ada 100644
--- a/src/api/routes/camera/cam_data_ws.py
+++ b/src/api/routes/camera/cam_data_ws.py
@@ -7,6 +7,7 @@ from fastapi import APIRouter, WebSocket
from jon_scratch.opencv_camera import OpenCVCamera
from src.api.services.board_detect_service import BoardDetectService
+from src.api.services.mediapipe_detect_service import MediapipeSkeletonDetectionService
logger = logging.getLogger(__name__)
@@ -23,6 +24,15 @@ async def begin_board_detection():
service = BoardDetectService()
await service.run()
+@cam_ws_router.get("/begin_mediapipe_skeleton_detection")
+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
+ 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}")
diff --git a/src/api/services/mediapipe_detect_service.py b/src/api/services/mediapipe_detect_service.py
new file mode 100644
index 00000000..ad36fe27
--- /dev/null
+++ b/src/api/services/mediapipe_detect_service.py
@@ -0,0 +1,20 @@
+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
+
+
+class MediapipeSkeletonDetectionService:
+ model_complexity: int=1
+
+ async def run(self):
+ queue = get_manager().Queue()
+ await asyncio.gather(
+ # Producer
+ start_camera_capture(queue),
+ # Consumer
+ MediapipeSkeletonDetection().create_new_process_for_run(queue)
+ )
diff --git a/src/core_processor/mediapipe_skeleton_detection/detect.py b/src/core_processor/mediapipe_skeleton_detection/detect.py
new file mode 100644
index 00000000..70917138
--- /dev/null
+++ b/src/core_processor/mediapipe_skeleton_detection/detect.py
@@ -0,0 +1,102 @@
+import logging
+
+import cv2
+import numpy as np
+
+import cv2
+import mediapipe as mp
+
+
+from aiomultiprocess import Process
+from aiomultiprocess.types import Queue
+
+from src.core_processor.processor import ImagePayload
+
+
+from rich import inspect
+
+
+class MediapipeSkeletonDetection():
+ async def create_new_process_for_run(self, queue: Queue):
+ p = Process(target=self.process, args=(queue,))
+ p.start()
+ await p.join()
+
+ async def process(self, queue: Queue):
+ logger = logging.getLogger(__name__)
+ while True:
+ message = None
+ try:
+ message = queue.get(timeout=1) # type: ImagePayload
+
+ except Exception as e:
+ pass
+
+
+ if not message:
+ print('No Message recieved!')
+ continue
+
+ frames = message.frames
+
+ for f in frames:
+ print(f'got image of shape {f.image.shape} from camera at port {f.port_number}, queueue size: {queue.qsize()}')
+
+ image = self.detect_mediapipe_skeleton(f.image)
+ # image = f.image
+ text_to_write_on_this_camera = "image queueue size = " + str(queue.qsize())
+ cv2.putText(
+ image, # numpy array on which text is written
+ text_to_write_on_this_camera, # text
+ (10,50), # position at which writing has to start
+ cv2.FONT_HERSHEY_SIMPLEX, # font family
+ .5, # font size
+ (30, 10, 0, 255), # font color
+ 1) # font stroke
+
+ cv2.imshow(str(f.port_number), image)
+ exit_key = cv2.waitKey(1)
+ if exit_key == 27:
+ break
+
+
+ def detect_mediapipe_skeleton(self, image):
+ #adapted from 'webcam' part of demo code here - https://google.github.io/mediapipe/solutions/holistic.html
+
+
+ mp_drawing = mp.solutions.drawing_utils
+ mp_drawing_styles = mp.solutions.drawing_styles
+ mp_holistic = mp.solutions.holistic
+
+ # For webcam input:
+ with mp_holistic.Holistic(
+ min_detection_confidence=0.5,
+ min_tracking_confidence=0.5,
+ model_complexity=0) as holistic:
+
+ # To improve performance, optionally mark the image as not writeable to
+ # pass by reference.
+ image.flags.writeable = False
+ image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
+ results = holistic.process(image)
+
+ # Draw landmark annotation on the image.
+ image.flags.writeable = True
+ image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
+ mp_drawing.draw_landmarks(
+ image,
+ results.face_landmarks,
+ mp_holistic.FACEMESH_CONTOURS,
+ landmark_drawing_spec=None,
+ connection_drawing_spec=mp_drawing_styles
+ .get_default_face_mesh_contours_style())
+ mp_drawing.draw_landmarks(
+ image,
+ results.pose_landmarks,
+ mp_holistic.POSE_CONNECTIONS,
+ landmark_drawing_spec=mp_drawing_styles
+ .get_default_pose_landmarks_style())
+ # Flip the image horizontally for a selfie-view display.
+ # cv2.imshow('MediaPipe Holistic', cv2.flip(image, 1))
+
+ return image
diff --git a/src/core_processor/processor.py b/src/core_processor/processor.py
index d2b7f7ca..c64c4988 100644
--- a/src/core_processor/processor.py
+++ b/src/core_processor/processor.py
@@ -55,8 +55,10 @@ class CameraCaptureProcess:
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(