Skip to content

Data containers

Defines some containers passed between objects of the yarPPG application.

Color dataclass

Defines a color in RGB(A) format.

Source code in src\yarppg\containers.py
@dataclass
class Color:
    """Defines a color in RGB(A) format."""

    r: float
    g: float
    b: float

    @classmethod
    def null(cls):
        """Create empty color with NaN values."""
        return cls(np.nan, np.nan, np.nan)

    def __array__(self):
        return np.array([self.r, self.g, self.b])

    @classmethod
    def from_array(cls, arr: np.ndarray):
        """Convert numpy array to `Color` object."""
        if len(arr) in {3, 4} and arr.ndim == 1:
            return cls(*arr)
        raise ValueError(f"Cannot interpret {arr=!r}")

from_array(arr) classmethod

Convert numpy array to Color object.

Source code in src\yarppg\containers.py
@classmethod
def from_array(cls, arr: np.ndarray):
    """Convert numpy array to `Color` object."""
    if len(arr) in {3, 4} and arr.ndim == 1:
        return cls(*arr)
    raise ValueError(f"Cannot interpret {arr=!r}")

null() classmethod

Create empty color with NaN values.

Source code in src\yarppg\containers.py
@classmethod
def null(cls):
    """Create empty color with NaN values."""
    return cls(np.nan, np.nan, np.nan)

RegionOfInterest dataclass

Container for defining the region of interest (and background) in an image.

Source code in src\yarppg\containers.py
@dataclass
class RegionOfInterest:
    """Container for defining the region of interest (and background) in an image."""

    mask: np.ndarray
    baseimg: np.ndarray
    bg_mask: np.ndarray | None = None
    face_rect: tuple[int, int, int, int] | None = None
    """Bounding box of the detected face (x, y, w, h)."""

face_rect: tuple[int, int, int, int] | None = None class-attribute instance-attribute

Bounding box of the detected face (x, y, w, h).

RppgResult dataclass

Container for rPPG computation results.

Calling np.array on this container will return a 8-element vector containing the rPPG signal value, RGB values of the ROI, RGB values of the background (or nans) and the HR. to_series produces a clearer representation of the values with named indices.

Note that both __array__ and to_series ignore the roi attribute.

Source code in src\yarppg\containers.py
@dataclass
class RppgResult:
    """Container for rPPG computation results.

    Calling `np.array` on this container will return a 8-element vector containing
    the rPPG signal value, RGB values of the ROI, RGB values of the background (or nans)
    and the HR. `to_series` produces a clearer representation of the values with named
    indices.

    Note that both `__array__` and `to_series` ignore the `roi` attribute.
    """

    value: float
    """Output value of the rPPG signal extractor."""
    roi: RegionOfInterest
    """Region of interest identified in the current frame."""
    roi_mean: Color
    """Mean color of the ROI."""
    bg_mean: Color
    """Mean color of the background."""
    hr: float = np.nan
    """Heart rate estimate in frames per beat."""

    def __array__(self):
        return np.r_[self.value, self.roi_mean, self.bg_mean, self.hr]

    def to_series(self):
        """Extract the rPPG signal values into a Pandas series."""
        return pd.Series(
            np.array(self),
            index=["value", "roi_r", "roi_g", "roi_b", "bg_r", "bg_g", "bg_b", "hr"],
        )

bg_mean: Color instance-attribute

Mean color of the background.

hr: float = np.nan class-attribute instance-attribute

Heart rate estimate in frames per beat.

roi: RegionOfInterest instance-attribute

Region of interest identified in the current frame.

roi_mean: Color instance-attribute

Mean color of the ROI.

value: float instance-attribute

Output value of the rPPG signal extractor.

to_series()

Extract the rPPG signal values into a Pandas series.

Source code in src\yarppg\containers.py
def to_series(self):
    """Extract the rPPG signal values into a Pandas series."""
    return pd.Series(
        np.array(self),
        index=["value", "roi_r", "roi_g", "roi_b", "bg_r", "bg_g", "bg_b", "hr"],
    )