Tracking Your Heart Rate Via Webcam

I remember being fascinated by the Eulerian Video Magnification work when some of the videos were being spread around, so I was excited to see the Webcam Pulse Detector project pop up on Quantified Self as I was scrolling through some missed feeds this morning.

It didn’t seem too difficult to setup for somebody with some linux familiarity and I set off to make it happen on my laptop.

The entire process took a couple hours. Some of that was due to missteps in installing OpenCV or not using sudo in the right place. The rest was due to the unavoidable—some packages just take a long time to install.

Seeing it finally work is really, really cool. Using my forehead, the app seemed to consistently track my heart rate at around 54-57bpm. At the same time I measured my pulse at my wrist as 60bm. I’ll need to track the consistency over time and with non-resting heart rates as well, but that seems like an acceptable variance so far. Pretty cool stuff.

If you want to give it a go and you’re running OS X 10.8.3 on your machine, I’m embedding a gist with the commands I had to use to make this work along with some comments inline.

# Starting with...
# OS X 10.8.3
# python 2.7.2 // python --version
# c++ 4.0      // c++ --version
# g++ 4.2      // g++ --version
#
# Update/Install XCode command line utils
# c++ 4.2      // c++ --version
#
# Doing all my work in ~/Development
cd ~/Development

# Update brew packages
brew update

# Install Python Package Index
sudo easy_install pip

# Install NumPy - was already installed for me
sudo pip install numpy

# Verify that NumPy is available to Python
python
import numpy
quit()
# Would see a file import error if not successful

# SciPy requires a Fortran compiler, available via brew
brew install gfortran

# Install SciPy
sudo pip install scipy

# Verify that SciPy is available to Python
python
import scipy
quit()
# Would see a file import error if not successful

# Install matplotlib
sudo pip install matplotlib

# OpenCV 2.4.5
# 
# Download tar file for Mac/Linux from http://opencv.org/downloads.html
# to ~/Development
tar -xvf opencv-2.4.5.tar.gz
cd opencv-2.4.5
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_PYTHON_SUPPORT=ON -D BUILD_EXAMPLES=ON ..
make
make install

# OpenCV was still not available to Python until I added
# it to the PYTHONPATH variable
PYTHONPATH="/usr/local/python-2.7.2"
export PYTHONPATH

# Verify that cv2 is available to Python
python
import cv2
quit()
# Would see a file import error if not successful

# OpenMDAO 0.6.0
# 
# Download go-openmdao.py from http://openmdao.org/downloads-2/recent/
# to ~/Development
python go-openmdao.py

# Clone Webcam Pulse Detector repo
git clone git://github.com/thearn/webcam-pulse-detector.git

# Launch OpenMDAO terminal
. openmdao-0.6.0/bin/activate

# Launch Webcam Pulse Detector
cd webcam-pulse-detector
python get_pulse.py

There were also plenty of resources that proved invaluable in actually finding the right answers for installing some of these software packages:

Leave a Reply