Skip to main content

Vital sign estimation from facial video

Project description

vitallens-python

Tests PyPI Downloads Website DOI

Estimate vital signs such as heart rate and respiratory rate from video.

vitallens-python is a Python client for the VitalLens API, using the same neural net for inference as our free iOS app VitalLens. Furthermore, it includes fast implementations of several other heart rate estimation methods from video such as G, CHROM, and POS.

  • Accepts as input either a video filepath or an in-memory video as np.ndarray
  • Performs fast face detection if required - you can also pass existing detections
  • vitallens.Method.VITALLENS supports heart rate, respiratory rate, pulse waveform, and respiratory waveform estimation. In addition, it returns an estimation confidence for each vital. We are working to support more vital signs in the future.
  • vitallens.Method.{G/CHROM/POS} support faster, but less accurate heart rate and pulse waveform estimation.
  • While VITALLENS requires an API Key, G, CHROM, and POS do not. Register on our website to get a free API Key.

Estimate vitals in a few lines of code:

from vitallens import VitalLens, Method

vl = VitalLens(method=Method.VITALLENS, api_key="YOUR_API_KEY")
result = vl("video.mp4")
print(result)

Disclaimer

vitallens-python provides vital sign estimates for general wellness purposes only. It is not intended for medical use. Always consult with your doctor for any health concerns or for medically precise measurement.

See also our Terms of Service for the VitalLens API and our Privacy Policy.

Installation

General prerequisites are python>=3.8 and ffmpeg installed and accessible via the $PATH environment variable.

The easiest way to install the latest version of vitallens-python and its Python dependencies:

pip install vitallens

Alternatively, it can be done by cloning the source:

git clone https://github.com/Rouast-Labs/vitallens-python.git
pip install ./vitallens-python

How to use

To start using vitallens-python, first create an instance of vitallens.VitalLens. It can be configured using the following parameters:

Parameter Description Default
method Inference method. {Method.VITALLENS, Method.POS, Method.CHROM or Method.G} Method.VITALLENS
api_key Usage key for the VitalLens API (required for Method.VITALLENS) None
detect_faces True if faces need to be detected, otherwise False. True
estimate_running_vitals Set True to compute running vitals (e.g., running_heart_rate). True
fdet_max_faces The maximum number of faces to detect (if necessary). 1
fdet_fs Frequency [Hz] at which faces should be scanned - otherwise linearly interpolated. 1.0
export_to_json If True, write results to a json file. True
export_dir The directory to which json files are written. .

Once instantiated, vitallens.VitalLens can be called to estimate vitals. This can also be configured using the following parameters:

Parameter Description Default
video The video to analyze. Either a path to a video file or np.ndarray. More info here.
faces Face detections. Ignored unless detect_faces=False. More info here. None
fps Sampling frequency of the input video. Required if video is np.ndarray. None
override_fps_target Target frequency for inference (optional - use methods's default otherwise). None
export_filename Filename for json export if applicable. None

The estimation results are returned as a list. It contains a dict for each distinct face, with the following structure:

[
  {
    'face': {
      'coordinates': <Face coordinates for each frame as np.ndarray of shape (n_frames, 4)>,
      'confidence': <Face live confidence for each frame as np.ndarray of shape (n_frames,)>,
      'note': <Explanatory note>
    },
    'vital_signs': {
      'heart_rate': {
        'value': <Estimated global value as float scalar>,
        'unit': <Value unit>,
        'confidence': <Estimation confidence as float scalar>,
        'note': <Explanatory note>
      },
      'respiratory_rate': {
        'value': <Estimated global value as float scalar>,
        'unit': <Value unit>,
        'confidence': <Estimation confidence as float scalar>,
        'note': <Explanatory note>
      },
      'ppg_waveform': {
        'data': <Estimated waveform value for each frame as np.ndarray of shape (n_frames,)>,
        'unit': <Data unit>,
        'confidence': <Estimation confidence for each frame as np.ndarray of shape (n_frames,)>,
        'note': <Explanatory note>
      },
      'respiratory_waveform': {
        'data': <Estimated waveform value for each frame as np.ndarray of shape (n_frames,)>,
        'unit': <Data unit>,
        'confidence': <Estimation confidence for each frame as np.ndarray of shape (n_frames,)>,
        'note': <Explanatory note>
      },
    },
    "message": <Message about estimates>
  },
  { 
    <same structure for face 2 if present>
  },
  ...
]

If the video is long enough and estimate_running_vitals=True, the results additionally contain running vitals:

[
  {
    ...
    'vital_signs': {
      ...
      'running_heart_rate': {
        'data': <Estimated value for each frame as np.ndarray of shape (n_frames,)>,
        'unit': <Value unit>,
        'confidence': <Estimation confidence for each frame as np.ndarray of shape (n_frames,)>,
        'note': <Explanatory note>
      },
      'running_respiratory_rate': {
        'data': <Estimated value for each frame as np.ndarray of shape (n_frames,)>,
        'unit': <Value unit>,
        'confidence': <Estimation confidence for each frame as np.ndarray of shape (n_frames,)>,
        'note': <Explanatory note>
      }
    }
  ...
  },
  ...
]

Example: Compare results with gold-standard labels using our example script

There is an example Python script in examples/test.py which lets you run vitals estimation and plot the predictions against ground truth labels recorded with gold-standard medical equipment. Some options are available:

  • method: Choose from [VITALLENS, POS, G, CHROM] (Default: VITALLENS)
  • video_path: Path to video (Default: examples/sample_video_1.mp4)
  • vitals_path: Path to gold-standard vitals (Default: examples/sample_vitals_1.csv)
  • api_key: Pass your API Key. Required if using method=VITALLENS.

For example, to reproduce the results from the banner image on the VitalLens API Webpage:

python examples/test.py --method=VITALLENS --video_path=examples/sample_video_2.mp4 --vitals_path=examples/sample_vitals_2.csv --api_key=YOUR_API_KEY

This sample is kindly provided by the VitalVideos dataset.

Example: Use VitalLens API to estimate vitals from a video file

from vitallens import VitalLens, Method

vl = VitalLens(method=Method.VITALLENS, api_key="YOUR_API_KEY")
result = vl("video.mp4")

Example: Use POS method on an np.ndarray of video frames

from vitallens import VitalLens, Method

my_video_arr = ...
my_video_fps = 30
vl = VitalLens(method=Method.POS)
result = vl(my_video_arr, fps=my_video_fps)

Linting and tests

Before running tests, please make sure that you have an environment variable VITALLENS_DEV_API_KEY set to a valid API Key. To lint and run tests:

flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
pytest

Build

To build:

python -m build

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

vitallens-0.3.4.tar.gz (1.1 MB view details)

Uploaded Source

Built Distribution

vitallens-0.3.4-py3-none-any.whl (1.1 MB view details)

Uploaded Python 3

File details

Details for the file vitallens-0.3.4.tar.gz.

File metadata

  • Download URL: vitallens-0.3.4.tar.gz
  • Upload date:
  • Size: 1.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.13

File hashes

Hashes for vitallens-0.3.4.tar.gz
Algorithm Hash digest
SHA256 8d0e916d979b371d0671b7015fa64d8fe827b7cff037f2591e025302747e0377
MD5 a5ddb5e8bd758ac5f18c8ac569844bd3
BLAKE2b-256 49a627e773040b92e836a39980a82204aac51e975b86fec668a8899d251b8bda

See more details on using hashes here.

Provenance

File details

Details for the file vitallens-0.3.4-py3-none-any.whl.

File metadata

  • Download URL: vitallens-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.13

File hashes

Hashes for vitallens-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 16857d6de8241cb52e236eda0863a500f1aee0fda0889a001d21791746476c34
MD5 418a299ab07389faaf01d2379d03c8f4
BLAKE2b-256 65b4b3724328b52e2a3ba88f2969a838b0547f23b2d5b1c5791fe8af59ddebc2

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page