diff --git a/yarppg/rppg/rppg.py b/yarppg/rppg/rppg.py index dfd37c1..6fc1a21 100644 --- a/yarppg/rppg/rppg.py +++ b/yarppg/rppg/rppg.py @@ -1,5 +1,6 @@ +from typing import Union from collections import namedtuple -from datetime import datetime +import time import pathlib import numpy as np @@ -9,7 +10,7 @@ from yarppg.rppg.camera import Camera -def write_dataframe(path, df): +def write_dataframe(path: Union[str, pathlib.Path], df: pd.DataFrame) -> None: path = pathlib.Path(path) if path.suffix.lower() == ".csv": df.to_csv(path, float_format="%.7f", index=False) @@ -44,11 +45,9 @@ def __init__(self, roi_detector, parent=None, camera=None, self._set_camera(camera) self._dts = [] - self.last_update = datetime.now() - - self.output_frame = None + self.last_update = time.perf_counter() + self.hr_calculator = hr_calculator - if self.hr_calculator is not None: self.new_hr = self.hr_calculator.new_hr else: @@ -64,7 +63,6 @@ def add_processor(self, processor): self._processors.append(processor) def on_frame_received(self, frame): - self.output_frame = frame self.roi = self._roi_detector(frame) for processor in self._processors: @@ -79,8 +77,8 @@ def on_frame_received(self, frame): ts=self.get_ts, fps=self.get_fps())) def _update_time(self): - dt = (datetime.now() - self.last_update).total_seconds() - self.last_update = datetime.now() + dt = time.perf_counter()- self.last_update + self.last_update = time.perf_counter() self._dts.append(dt) return dt @@ -103,14 +101,16 @@ def get_fps(self, n=5): return 1/np.mean(self._dts[-n:]) def save_signals(self): + if self.output_filename is None: + return + path = pathlib.Path(self.output_filename) path.parent.mkdir(parents=True, exist_ok=True) - df = self.get_dataframe() - write_dataframe(path) + write_dataframe(path, self.get_dataframe()) def get_dataframe(self): - names = ["ts"] + ["p%d" % i for i in range(self.num_processors)] + names = ["ts"] + [str(p) for p in self._processors] data = np.vstack((self.get_ts(),) + tuple(self.get_vs())).T return pd.DataFrame(data=data, columns=names) @@ -128,6 +128,5 @@ def start(self): def finish(self): print("finishing up...") - if self.output_filename is not None: - self.save_signals() + self.save_signals() # save if filename was given. self._cam.stop() diff --git a/yarppg/ui/mainwindow.py b/yarppg/ui/mainwindow.py index 8e69477..eae0539 100644 --- a/yarppg/ui/mainwindow.py +++ b/yarppg/ui/mainwindow.py @@ -3,13 +3,13 @@ from PyQt5.QtWidgets import QMainWindow, QGridLayout, QHBoxLayout, QLabel import pyqtgraph as pg -# from yarppg.rppg import RPPG +from yarppg.rppg import RPPG from . import helpers class MainWindow(QMainWindow): - def __init__(self, app, rppg, winsize=(1000, 400), graphwin=150, + def __init__(self, app, rppg: RPPG, winsize=(1000, 400), graphwin=150, legend=False, blur_roi=-1): QMainWindow.__init__(self) self._app = app