diff --git a/client/models/pose_detection/frame_capturer.py b/client/models/pose_detection/frame_capturer.py new file mode 100644 index 0000000..f58d029 --- /dev/null +++ b/client/models/pose_detection/frame_capturer.py @@ -0,0 +1,32 @@ +from abc import ABC, abstractmethod + +import numpy as np +import cv2 + + +class FrameCapturer(ABC): + """Provides an interface to video frames for a PostureTracker.""" + + @abstractmethod + def get_frame(self) -> tuple[np.ndarray, int]: + """ + Returns: + (frame, timestamp), image is an image frame in the format HxWxC. Where the channels are + in RGB format. Timestamp is in milliseconds. + """ + + +class OpenCVCapturer(FrameCapturer): + """FrameCapturer using OpenCV to read from camera.""" + + def __init__(self) -> None: + self._cam = cv2.VideoCapture(0) + + def get_frame(self) -> tuple[np.ndarray, int]: + _, frame = self._cam.read() + timestamp = self._cam.get(cv2.CAP_PROP_POS_MSEC) + return frame, timestamp + + def release(self) -> None: + """Release the camera.""" + self._cam.release()