Skip to content

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

TypeScript

Save Video code ready for basic review

Endurance Idehend5bf61a08ebd
diff --git a/src/api/routes/startup/startup.py b/src/api/routes/startup/startup.py
index d8d84358..d259fec3 100644
--- a/src/api/routes/startup/startup.py
+++ b/src/api/routes/startup/startup.py
@@ -1,10 +1,10 @@
-import aiomultiprocess
from fastapi import APIRouter
+from src.config.data_paths import create_home_data_directory
+
startup_router = APIRouter()
@startup_router.on_event("startup")
async def handle_startup():
- aiomultiprocess.set_start_method("spawn")
- # get_or_create_cams()
+ create_home_data_directory()
diff --git a/src/cameras/__init__.py b/src/cameras/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/cameras/cv_camera_manager.py b/src/cameras/cv_camera_manager.py
index d2fb5798..30be736a 100644
--- a/src/cameras/cv_camera_manager.py
+++ b/src/cameras/cv_camera_manager.py
@@ -10,6 +10,7 @@ from src.api.services.user_config import UserConfigService
from src.cameras.cam_singleton import get_or_create_cams
from src.cameras.dtos.create_writer_options import CreateWriterOptions
from src.cameras.opencv_camera import OpenCVCamera
+from src.cameras.video_writer.video_writer import VideoWriter
logger = logging.getLogger(__name__)
@@ -18,7 +19,7 @@ _cv_cams = None
class CamAndWriterResponse(BaseModel):
cv_cam: OpenCVCamera
- writer: cv2.VideoWriter
+ writer: VideoWriter
class Config:
arbitrary_types_allowed = True
@@ -38,6 +39,10 @@ class CVCameraManager:
logger.info("Reusing already created resources cam resources.")
self._cv_cams = _cv_cams
+ @property
+ def available_webcam_ids(self):
+ return [cv_cam.webcam_id_as_str for cv_cam in self._cv_cams]
+
@property
def cv_cams(self):
return self._cv_cams
@@ -58,35 +63,27 @@ class CVCameraManager:
@contextmanager
def start_capture_session_single_cam(
- self, webcam_id: str = None, create_writer_options: CreateWriterOptions = None
+ self, webcam_id: str = None
) -> ContextManager[CamAndWriterResponse]:
"""
Context manager for easy start up, usage, and cleanup of camera resources.
Can capture frames from a single webcam or all webcams detected.
"""
- writer = None
try:
- writer = self._start_frame_capture_on_cam_id(
- webcam_id, create_writer_options
- )
+ writer = self._start_frame_capture_on_cam_id(webcam_id)
cv_cam = self.cv_cam_by_id(webcam_id)
yield CamAndWriterResponse(cv_cam=cv_cam, writer=writer)
self._stop_frame_capture_all_cams()
except:
logger.error("Printing traceback from starting capture session by cam")
traceback.print_exc()
- finally:
- if writer:
- writer.release()
- logger.debug("Cleaning up capture session")
@contextmanager
def start_capture_session_all_cams(
- self, create_writer_options: CreateWriterOptions = None
+ self,
) -> ContextManager[List[CamAndWriterResponse]]:
- writer_dict = None
try:
- writer_dict = self._start_frame_capture_all_cams(create_writer_options)
+ writer_dict = self._start_frame_capture_all_cams()
responses = [
CamAndWriterResponse(cv_cam=self.cv_cam_by_id(webcam_id), writer=writer)
for webcam_id, writer in writer_dict.items()
@@ -96,28 +93,17 @@ class CVCameraManager:
except:
logger.error("Printing traceback from starting capture session by cam")
traceback.print_exc()
- finally:
- if writer_dict:
- for writer in writer_dict.values():
- writer.release()
- logger.debug("Cleaning up capture session")
-
- def _start_frame_capture_all_cams(
- self, create_writer_options: CreateWriterOptions = None
- ) -> Dict[str, cv2.VideoWriter]:
+
+ def _start_frame_capture_all_cams(self) -> Dict[str, VideoWriter]:
d = {}
for cv_cam in self._cv_cams:
cv_cam.connect()
cv_cam.start_frame_capture()
- d[cv_cam.webcam_id_as_str] = cv_cam.create_video_writer(
- create_writer_options
- )
+ d[cv_cam.webcam_id_as_str] = VideoWriter()
return d
- def _start_frame_capture_on_cam_id(
- self, webcam_id: str, create_writer_options: CreateWriterOptions = None
- ) -> cv2.VideoWriter:
+ def _start_frame_capture_on_cam_id(self, webcam_id: str) -> VideoWriter:
filtered_cams = list(
filter(lambda c: c.webcam_id_as_str == str(webcam_id), self._cv_cams)
)
@@ -127,7 +113,7 @@ class CVCameraManager:
cv_cam = filtered_cams[0]
cv_cam.connect()
cv_cam.start_frame_capture()
- return cv_cam.create_video_writer(create_writer_options)
+ return VideoWriter()
def _stop_frame_capture_all_cams(self):
for cv_cam in self._cv_cams:
diff --git a/src/cameras/dtos/create_writer_options.py b/src/cameras/dtos/create_writer_options.py
index 66977797..30c8b256 100644
--- a/src/cameras/dtos/create_writer_options.py
+++ b/src/cameras/dtos/create_writer_options.py
@@ -5,16 +5,16 @@ from pydantic import BaseModel
class CreateWriterOptions(BaseModel):
- filename_and_ext: str
- parent_folder: str
+ writer_dir: Path
def get_canonical_time_str():
- return time.strftime("%Y%m%d_%H%M%S")
+ return time.strftime("%m_%d_%Y_%H%M%S")
def get_base_options():
return CreateWriterOptions(
- filename_and_ext=f"{get_canonical_time_str()}.avi",
- parent_folder="raw_frame_capture",
+ writer_dir=Path().joinpath(
+ "raw_frame_capture", f"{get_canonical_time_str()}.mp4"
+ )
)
diff --git a/src/cameras/frame_grabber.py b/src/cameras/frame_grabber.py
index 20f7f394..fa9ae6cc 100644
--- a/src/cameras/frame_grabber.py
+++ b/src/cameras/frame_grabber.py
@@ -2,26 +2,39 @@ import logging
import threading
import time
import traceback
-
-import cv2
+from pathlib import Path
+from uuid import uuid4
from src.cameras.dto import FramePayload
+from src.cameras.dtos.create_writer_options import get_canonical_time_str
+from src.cameras.video_writer.video_writer import SaveOptions, VideoWriter
+from src.config.data_paths import freemocap_data_path
logger = logging.getLogger(__name__)
class FrameThread(threading.Thread):
- def __init__(self, get_next_frame, writer: cv2.VideoWriter, save_video=False):
+ def __init__(
+ self,
+ webcam_id: str,
+ get_next_frame,
+ frame_width: int,
+ frame_height: int,
+ save_video=False,
+ ):
super().__init__()
+ self._webcam_id = webcam_id
+ self._frame_width = frame_width
+ self._frame_height = frame_height
self._save_video = save_video
self._is_capturing_frames = False
self._get_next_frame = get_next_frame
self._num_frames_processed = 0
self._elapsed = 0
self._frame: FramePayload = FramePayload()
+ self._session_id = uuid4()
+ self._session_timestr = get_canonical_time_str()
self.setDaemon(True)
- if save_video:
- self._writer = writer
@property
def current_fps(self):
@@ -31,6 +44,12 @@ class FrameThread(threading.Thread):
return 0
return self._num_frames_processed / self._elapsed
+ @property
+ def session_writer_path(self):
+ return Path().joinpath(
+ freemocap_data_path, f"{self._session_timestr}_{self._session_id}"
+ )
+
@property
def latest_frame(self):
return self._frame
@@ -46,14 +65,15 @@ class FrameThread(threading.Thread):
self._start_frame_loop(self._save_video)
def _start_frame_loop(self, save_video=False):
+ video_writer = VideoWriter()
self._is_capturing_frames = True
start = time.time()
try:
while self._is_capturing_frames:
success, image, timestamp = self._get_next_frame()
- if save_video:
- self._writer.write(image)
self._frame = FramePayload(success, image, timestamp)
+ if save_video:
+ video_writer.write(self._frame)
self._num_frames_processed += 1
self._elapsed = time.time() - start
except:
@@ -62,4 +82,14 @@ class FrameThread(threading.Thread):
else:
logger.info("Frame loop thread exited.")
finally:
- self._writer.release()
+ options = SaveOptions(
+ writer_dir=Path().joinpath(
+ self.session_writer_path,
+ "raw_frame_capture",
+ f"webcam_{self._webcam_id}",
+ ),
+ fps=self.current_fps,
+ frame_width=self._frame_width,
+ frame_height=self._frame_height,
+ )
+ video_writer.save(options)
diff --git a/src/cameras/opencv_camera.py b/src/cameras/opencv_camera.py
index b20bba98..263a9e64 100644
--- a/src/cameras/opencv_camera.py
+++ b/src/cameras/opencv_camera.py
@@ -9,7 +9,6 @@ from pydantic import BaseModel
from src.cameras.dto import FramePayload
from src.cameras.frame_grabber import FrameThread
-from src.cameras.open_cv_cam_writer import CreateWriterOptions, OpenCVCamWriter
logger = logging.getLogger(__name__)
@@ -24,7 +23,8 @@ class WebcamConfig(BaseModel):
resolution_width: int = 800
resolution_height: int = 600
save_video: bool = True
- fourcc: str = "MJPG"
+ fourcc: str = "MP4V"
+ # fourcc: str = "MJPG"
base_save_video_dir = _get_home_dir()
@@ -35,12 +35,10 @@ class OpenCVCamera:
_opencv_video_capture_object: cv2.VideoCapture = None
_running_thread: FrameThread = None
- _writer: OpenCVCamWriter
def __init__(self, config: WebcamConfig):
self._config = config
self._name = f"Camera {self._config.webcam_id}"
- self._writer = OpenCVCamWriter()
@property
def webcam_id_as_str(self):
@@ -66,6 +64,10 @@ class OpenCVCamera:
def latest_frame(self):
return self._running_thread.latest_frame
+ @property
+ def session_writer_base_path(self):
+ return self._running_thread.session_writer_path
+
def connect(self):
if platform.system() == "Windows":
cap_backend = cv2.CAP_DSHOW
@@ -90,11 +92,6 @@ class OpenCVCamera:
logger.debug("FPS of webcam hardware/input stream: {}".format(fps_input_stream))
return success
- def create_video_writer(self, create_writer_options: CreateWriterOptions):
- return self._writer.create_writer(
- self._opencv_video_capture_object, options=create_writer_options
- )
-
def start_frame_capture(self):
if self.is_capturing_frames:
logger.debug(
@@ -109,11 +106,19 @@ class OpenCVCamera:
def _create_thread(self):
return FrameThread(
+ webcam_id=self.webcam_id_as_str,
get_next_frame=self.get_next_frame,
- writer=self._writer.create_writer(self._opencv_video_capture_object),
save_video=self._config.save_video,
+ frame_width=self.get_frame_width(),
+ frame_height=self.get_frame_height(),
)
+ def get_frame_width(self):
+ return int(self._opencv_video_capture_object.get(3))
+
+ def get_frame_height(self):
+ return int(self._opencv_video_capture_object.get(4))
+
def _apply_configuration(self):
# set camera stream parameters
self._opencv_video_capture_object.set(
diff --git a/src/cameras/video_writer/__init__.py b/src/cameras/video_writer/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/cameras/open_cv_cam_writer.py b/src/cameras/video_writer/open_cv_cam_writer.py
similarity index 82%
rename from src/cameras/open_cv_cam_writer.py
rename to src/cameras/video_writer/open_cv_cam_writer.py
index a6ae676c..78d3ff5d 100644
--- a/src/cameras/open_cv_cam_writer.py
+++ b/src/cameras/video_writer/open_cv_cam_writer.py
@@ -1,10 +1,10 @@
import logging
-import os
from pathlib import Path
import cv2
from src.cameras.dtos.create_writer_options import CreateWriterOptions, get_base_options
+from src.config.data_paths import freemocap_data_path
logger = logging.getLogger(__name__)
@@ -25,11 +25,11 @@ class OpenCVCamWriter:
frame_height = int(video_capture_obj.get(4))
fps = int(video_capture_obj.get(5))
- final_path = (
- Path()
- .joinpath(os.getcwd(), options.parent_folder, options.filename_and_ext)
- .resolve()
- )
+ if options:
+ final_path = Path().joinpath(freemocap_data_path, options.writer_dir)
+ else:
+ assert False, "A path for writer dir must be sent."
+
logger.debug(f"Video save path: {final_path}")
return cv2.VideoWriter(
str(final_path),
diff --git a/src/cameras/video_writer/video_writer.py b/src/cameras/video_writer/video_writer.py
new file mode 100644
index 00000000..d3206b52
--- /dev/null
+++ b/src/cameras/video_writer/video_writer.py
@@ -0,0 +1,55 @@
+import logging
+import os
+import traceback
+from pathlib import Path
+from typing import List
+
+import cv2
+from pydantic import BaseModel
+
+from src.cameras.dto import FramePayload
+
+logger = logging.getLogger(__name__)
+
+
+class SaveOptions(BaseModel):
+ writer_dir: Path
+ filename: str = "movie.mp4"
+ fps: float
+ fourcc: str = "MP4V"
+ frame_width: int
+ frame_height: int
+
+ @property
+ def full_path(self):
+ return Path().joinpath(self.writer_dir, self.filename)
+
+
+class VideoWriter:
+ def __init__(self):
+ self._frames: List[FramePayload] = []
+
+ def write(self, frame_payload: FramePayload):
+ self._frames.append(frame_payload)
+
+ def save(self, options: SaveOptions):
+ if not os.path.exists(options.writer_dir):
+ os.makedirs(options.writer_dir.resolve())
+
+ cv2_writer = self._create_cv2_video_writer(options)
+ try:
+ for frame in self._frames:
+ cv2_writer.write(frame.image)
+ except:
+ logger.debug("Failed during save in video writer")
+ traceback.print_exc()
+ finally:
+ cv2_writer.release()
+
+ def _create_cv2_video_writer(self, options: SaveOptions):
+ return cv2.VideoWriter(
+ str(options.full_path.resolve()),
+ cv2.VideoWriter_fourcc(*options.fourcc),
+ options.fps,
+ (options.frame_width, options.frame_height),
+ )
diff --git a/src/config/data_paths.py b/src/config/data_paths.py
new file mode 100644
index 00000000..8b7229b4
--- /dev/null
+++ b/src/config/data_paths.py
@@ -0,0 +1,12 @@
+import os
+from pathlib import Path
+
+BASE_FOLDER_NAME = "freemocap_data"
+
+freemocap_data_path = Path().joinpath(Path.home(), BASE_FOLDER_NAME)
+
+
+def create_home_data_directory():
+ path_as_str = str(freemocap_data_path)
+ if not os.path.exists(path_as_str):
+ os.makedirs(path_as_str)
diff --git a/src/core_processor/board_detection/board_detection.py b/src/core_processor/board_detection/board_detection.py
index fadb7366..5e5b86e8 100644
--- a/src/core_processor/board_detection/board_detection.py
+++ b/src/core_processor/board_detection/board_detection.py
@@ -1,14 +1,19 @@
import logging
+import time
import traceback
+from pathlib import Path
import cv2
import numpy as np
from src.cameras.cv_camera_manager import CVCameraManager
+from src.cameras.dto import FramePayload
+from src.cameras.video_writer.video_writer import SaveOptions
from src.core_processor.board_detection.base_pose_estimation import detect_charuco_board
from src.core_processor.board_detection.charuco_image_annotator import (
annotate_image_with_charuco_data,
)
+from src.core_processor.fps.fps_counter import FPSCamCounter
from src.core_processor.utils.image_fps_writer import write_fps_to_image
logger = logging.getLogger(__name__)
@@ -20,19 +25,41 @@ class BoardDetection:
async def process_by_cam_id(self, webcam_id: str, cb):
with self._cam_manager.start_capture_session_single_cam(
- webcam_id, save_video=True
- ) as cam_response:
- cv_cam = cam_response.cv_cam
+ webcam_id=webcam_id
+ ) as session_obj:
+ fps_manager = FPSCamCounter(self._cam_manager.available_webcam_ids)
+ fps_manager.start_all()
+ cv_cam = session_obj.cv_cam
+ writer = session_obj.writer
try:
while True:
if not cv_cam.is_capturing_frames:
return
- image = self._process_single_cam_frame(cv_cam)
- if cb and image is not None:
- await cb(image)
- except:
+ response = self._process_single_cam_frame(cv_cam)
+ if cb and response.image is not None:
+ writer.write(
+ FramePayload(
+ image=response.image, timestamp=response.timestamp
+ )
+ )
+ await cb(response.image)
+ except Exception as e:
logger.error("Printing traceback")
traceback.print_exc()
+ raise e
+ finally:
+ writer = session_obj.writer
+ options = SaveOptions(
+ writer_dir=Path().joinpath(
+ cv_cam.session_writer_base_path,
+ "board_detection",
+ f"webcam_{cv_cam.webcam_id_as_str}",
+ ),
+ fps=fps_manager.current_fps_for(cv_cam.webcam_id_as_str),
+ frame_width=cv_cam.get_frame_width(),
+ frame_height=cv_cam.get_frame_height(),
+ )
+ writer.save(options)
def process(self):
"""
@@ -41,9 +68,9 @@ class BoardDetection:
"""
# if its already capturing frames, this is a no-op
- with self._cam_manager.start_capture_session_all_cams(
- save_video=True
- ) as session_obj:
+ with self._cam_manager.start_capture_session_all_cams() as session_obj:
+ fps_manager = FPSCamCounter(self._cam_manager.available_webcam_ids)
+ fps_manager.start_all()
try:
while True:
exit_key = cv2.waitKey(1)
@@ -53,16 +80,35 @@ class BoardDetection:
for response in session_obj:
cv_cam = response.cv_cam
writer = response.writer
- frame = self._process_single_cam_frame(cv_cam)
- if frame is not None:
- writer.write(frame)
- cv2.imshow(cv_cam.webcam_id_as_str, frame)
+ payload = self._process_single_cam_frame(cv_cam)
+ current_webcam_id = cv_cam.webcam_id_as_str
+
+ if payload is not None:
+ writer.write(payload)
+ fps_manager.increment_frame_processed_for(current_webcam_id)
+ write_fps_to_image(
+ payload.image,
+ fps_manager.current_fps_for(current_webcam_id),
+ )
+ cv2.imshow(current_webcam_id, payload.image)
except:
logger.error("Printing traceback")
traceback.print_exc()
finally:
for response in session_obj:
cv_cam = response.cv_cam
+ writer = response.writer
+ options = SaveOptions(
+ writer_dir=Path().joinpath(
+ cv_cam.session_writer_base_path,
+ "board_detection",
+ f"webcam_{cv_cam.webcam_id_as_str}",
+ ),
+ fps=fps_manager.current_fps_for(cv_cam.webcam_id_as_str),
+ frame_width=cv_cam.get_frame_width(),
+ frame_height=cv_cam.get_frame_height(),
+ )
+ writer.save(options)
logger.info(f"Destroy window {cv_cam.webcam_id_as_str}")
cv2.destroyWindow(cv_cam.webcam_id_as_str)
cv2.waitKey(1)
@@ -86,11 +132,9 @@ class BoardDetection:
frame, cv_cam.webcam_id_as_str, charuco_corners, charuco_ids
)
cv2.polylines(frame, np.int32([charuco_corners]), True, (0, 100, 255), 2)
- write_fps_to_image(frame, cv_cam.current_fps_short)
- return frame
+ return FramePayload(image=frame, timestamp=time.time_ns())
if __name__ == "__main__":
## Jon, Run me to easily start a charuco board detect run
- # asyncio.run(BoardDetection().process())
BoardDetection().process()
diff --git a/src/core_processor/fps/fps_counter.py b/src/core_processor/fps/fps_counter.py
new file mode 100644
index 00000000..001d0ca0
--- /dev/null
+++ b/src/core_processor/fps/fps_counter.py
@@ -0,0 +1,60 @@
+import time
+from typing import Dict, List
+
+
+class FPSCounter:
+ _num_frames_processed: int
+ _start_time: float
+
+ def __init__(self):
+ self._start_time = 0
+ self._num_frames_processed = 0
+
+ def is_started(self):
+ return self._start_time > 0
+
+ def start(self):
+ self._start_time = time.time()
+
+ @property
+ def current_fps(self):
+ if self.elapsed() <= 0:
+ return 0
+ if self._num_frames_processed <= 0:
+ return 0
+ return self._num_frames_processed / self.elapsed()
+
+ def elapsed(self):
+ return time.time() - self._start_time
+
+ def increment_frame_processed(self):
+ self._num_frames_processed += 1
+
+
+class FPSCamCounter:
+ def __init__(self, webcam_ids: List[str]):
+ self._webcam_ids = webcam_ids
+ self._counters: Dict[str, FPSCounter] = self._init_counters(webcam_ids)
+
+ def _init_counters(self, webcam_ids: List[str]):
+ d = {}
+ for webcam_id in webcam_ids:
+ d[webcam_id] = FPSCounter()
+
+ return d
+
+ def increment_frame_processed_for(self, webcam_id):
+ self._counters[webcam_id].increment_frame_processed()
+
+ def current_fps_for(self, webcam_id):
+ return self._counters[webcam_id].current_fps
+
+ def start_all(self):
+ for webcam_id in self._webcam_ids:
+ self.start_for(webcam_id)
+
+ def start_for(self, webcam_id):
+ if self._counters[webcam_id].is_started():
+ return
+
+ self._counters[webcam_id].start()
diff --git a/src/core_processor/mediapipe_skeleton_detection/mediapipe_skeleton_detection.py b/src/core_processor/mediapipe_skeleton_detection/mediapipe_skeleton_detection.py
index 31b13f7c..a563c232 100644
--- a/src/core_processor/mediapipe_skeleton_detection/mediapipe_skeleton_detection.py
+++ b/src/core_processor/mediapipe_skeleton_detection/mediapipe_skeleton_detection.py
@@ -1,17 +1,18 @@
import logging
-import os
+import time
import traceback
from pathlib import Path
-from uuid import uuid4
+from typing import Any
import cv2
import mediapipe as mp
+import numpy as np
from src.cameras.cv_camera_manager import CVCameraManager
-from src.cameras.dtos.create_writer_options import (
- CreateWriterOptions,
- get_canonical_time_str,
-)
+from src.cameras.dto import FramePayload
+from src.cameras.video_writer.video_writer import SaveOptions
+from src.config.data_paths import create_home_data_directory
+from src.core_processor.fps.fps_counter import FPSCamCounter
from src.core_processor.utils.image_fps_writer import write_fps_to_image
mp_holistic = mp.solutions.holistic
@@ -21,18 +22,22 @@ mp_drawing_styles = mp.solutions.drawing_styles
logger = logging.getLogger(__name__)
+class ProcessResponse:
+ frame_payload: FramePayload
+ processed_image: Any
+
+ def __init__(self, frame_payload: FramePayload, processed_image: np.array):
+ self.frame_payload = frame_payload
+ self.processed_image = processed_image
+
+
class MediapipeSkeletonDetection:
def __init__(self, cam_manager: CVCameraManager):
self._cam_manager = cam_manager
async def process_as_frame_loop(self, webcam_id, cb, model_complexity: int = 1):
- timestr = get_canonical_time_str()
- options = CreateWriterOptions(
- filename_and_ext=f"{uuid4()}.{timestr}.avi",
- relative_folder_path=Path().joinpath("skeleton_detection"),
- )
with self._cam_manager.start_capture_session_single_cam(
- webcam_id=webcam_id, create_writer_options=options
+ webcam_id=webcam_id
) as session_obj:
with mp_holistic.Holistic(
min_detection_confidence=0.5,
@@ -41,29 +46,30 @@ class MediapipeSkeletonDetection:
) as holistic:
try:
while True:
- image = self._process_single_cam_frame(
+ response = self._process_single_cam_frame(
holistic, session_obj.cv_cam
)
- session_obj.writer.write(image)
- if cb and image is not None:
- await cb(image)
+ session_obj.writer.write(
+ FramePayload(
+ image=response.processed_image, timestamp=time.time_ns()
+ )
+ )
+ if cb and response.processed_image is not None:
+ await cb(response.processed_image)
except Exception as e:
logger.error("Printing traceback")
traceback.print_exc()
raise e
def process(self, model_complexity: int = 1):
- timestr = get_canonical_time_str()
- options = CreateWriterOptions(
- filename_and_ext=f"{uuid4()}.{timestr}.avi",
- parent_folder="skeleton_detection",
- )
- with self._cam_manager.start_capture_session_all_cams(options) as session_obj:
+ with self._cam_manager.start_capture_session_all_cams() as session_obj:
+ fps_manager = FPSCamCounter(self._cam_manager.available_webcam_ids)
with mp_holistic.Holistic(
min_detection_confidence=0.5,
min_tracking_confidence=0.5,
model_complexity=model_complexity,
) as holistic:
+ fps_manager.start_all()
try:
while True:
exit_key = cv2.waitKey(1)
@@ -73,37 +79,59 @@ class MediapipeSkeletonDetection:
for response in session_obj:
cv_cam = response.cv_cam
writer = response.writer
- image = self._process_single_cam_frame(holistic, cv_cam)
- writer.write(image)
- try:
- cv2.imshow(cv_cam.webcam_id_as_str, image)
- except:
- logger.debug(
- "Failed to draw image for some reason for cv2.imshow"
+ response = self._process_single_cam_frame(holistic, cv_cam)
+ if not response:
+ continue
+ fps_manager.increment_frame_processed_for(
+ cv_cam.webcam_id_as_str
+ )
+ write_fps_to_image(
+ response.processed_image,
+ fps_manager.current_fps_for(cv_cam.webcam_id_as_str),
+ )
+ writer.write(
+ FramePayload(
+ image=response.processed_image,
+ timestamp=time.time_ns(),
)
- traceback.print_exc()
+ )
+
+ cv2.imshow(
+ cv_cam.webcam_id_as_str, response.processed_image
+ )
except:
logger.error("Printing traceback")
traceback.print_exc()
finally:
for response in session_obj:
cv_cam = response.cv_cam
+ options = SaveOptions(
+ writer_dir=Path().joinpath(
+ cv_cam.session_writer_base_path,
+ "skeleton_detection",
+ f"webcam_{cv_cam.webcam_id_as_str}",
+ ),
+ fps=fps_manager.current_fps_for(cv_cam.webcam_id_as_str),
+ frame_width=cv_cam.get_frame_width(),
+ frame_height=cv_cam.get_frame_height(),
+ )
+ response.writer.save(options)
logger.info(f"Destroy window {cv_cam.webcam_id_as_str}")
cv2.destroyWindow(cv_cam.webcam_id_as_str)
cv2.waitKey(1)
def _process_single_cam_frame(self, holistic, cv_cam):
- success, frame, timestamp = cv_cam.latest_frame
- if not success:
- # logger.error("CV2 failed to grab a frame")
+ frame_payload = cv_cam.latest_frame
+ if not frame_payload.success:
return
- if frame is None:
+ if frame_payload.image is None:
logger.error("Frame is empty")
return
- image = self.detect_mediapipe_skeleton(holistic, frame)
- write_fps_to_image(image, cv_cam.current_fps_short)
- return image
+ processed_image = self.detect_mediapipe_skeleton(holistic, frame_payload.image)
+ return ProcessResponse(
+ frame_payload=frame_payload, processed_image=processed_image
+ )
def detect_mediapipe_skeleton(self, holistic, image):
# adapted from 'webcam' part of demo code here -
@@ -136,4 +164,5 @@ class MediapipeSkeletonDetection:
if __name__ == "__main__":
+ create_home_data_directory()
MediapipeSkeletonDetection(CVCameraManager()).process()