Code I used when filming Steve Mould’s heart rate! https://www.youtube.com/watch?v=BFZxlauizx0
Here’s the code we used in my video with Steve Mould to extract his heart rate from a video of his face!
It’s very much hacked-together-for-a-video level code, but it does run. If you’d like to clean this up and make it slightly more usable, please feel free to make a pull request. :)
If you want a sample video to test it on, you can download the one we used in the video here!
stattohr.py
import
statements at the top of the script.python stattohr.py -f steve-face-hr.mp4
to extract the heart rate from a file.This code just takes the skin RGB values and subtracts two moving averages: a long-period one of around a second to smooth over gross changes (eg if the subject moves their head) and then a short-period one of a few frames to smooth over how noisy the data usually are and make the output look a bit nicer.
More advanced code uses FFTs to extract heart rate frequencies, or clever machine learning to do the same. I tried the former and found it a bit finicky for use in this video!
With apologies, there are a bunch of commented out or otherwise unfinished or unusable bits in this code. There are a few missing components for doing this on a live webcam image, but it would be pretty easy to implement. There’s also some code at the bottom which is commented out which just runs a ‘Welch’ analysis to get frequencies from the smoothed RGB data. This works really surprisingly well! But it’s also far slower than real-time, so I actually did this when I got home, and it never made it into the video anyway. :) It would be pretty easy to make this work either optionally after analysing the video or with the relevant command line flag.
ppg-plots.R
This is just a few simple ggplot
graphs to show the data more beautifully. It’s meant to be run interactively in RStudio, just for checking stuff out purposes.
It also includes the (very simple!) code to import a file from Polar Sensor Logger which is what we used to get the ECG from the Polar H10.
Inspired by pyVHR which implements a load of algorithms to extract heart rate in a far more general way than this script does!