Webcam Heart-Rate Monitor
When the heart beats, blood is pumped around the body causing a momentary change in skin colour. This is not visible to the human-eye but counter surprisingly is visible to a cheap webcam. Using simple signal processing techniques, the frequency at which the colour of the skin changes, and thus the heart-rate can be extracted and monitored completely non-invasive.
Video
Here is me (wearing a ridiculous impromptu headband to expose my forehead) demonstrating the heart-rate monitor in action.
Detailed Explanation
What follows is a relatively in-depth explanation of what is going on in my program.
Finding A Face
The first task faced by the program is to work out where you (or more precisely, your skin) is in its field of view. Many techniques exist for quickly finding faces in pictures, these are often so good that cheap digital cameras with little on board processing power can find faces in the frame in real-time. In particular I'm using a function provided by the computer vision library OpenCV.
Once the face is found I simply select a square of skin on the forehead as it is a convenient and large area of just skin.
You can see that my face has been automatically highlighted in yellow (as well as the area of my forehead from which the colour is taken).
What Colour Is My Skin?
When you look at a computer webcam you most likely think smugly, "what terrible resolution that has compared to my eyes", and you'd be right: the human eye has an impressive amount of 'spatial' resolution. That is, you can make out distinct shapes that are very small. What most people don't realise is that the human eye has remarkably limited poor 'chromatic' resolution, that is, we're not very good at seeing colours. As it happens this is something webcams are comparatively very good at. So good in fact that they can detect the colour of your skin changing as your heart beats.
Based on the section of forehead found in the previous step, the average colour of this patch of skin is used. Webcams give us this colour in a form known as "RGB" format. That is, the amount of red, green and blue light which when mixed together make that colour1. Of these three colours, it is the amount of green which changes the most when your heart beats and so in my program I only look at this number.
Finding The Pulse
The next challenge is to work out the heart rate by watching how the amount of green in the picture changes over time. In signal-processing terminology we want to turn information in the time-domain, that is, how the green changes over time, into the frequency domain: the frequency at which the colour is changing. This is a surprisingly tricky task but luckily a technique known as the 'Fast Fourier Transform' or FFT can be used to quickly do exactly this.
The FFT sadly doesn't just give us the heart-beat without a fight. Though the colour of the skin changes due to the heart rate it also changes due to other factors such as the changing light in a room and the low-quality in the webcam picture. These other factors also contribute to the way that the skin changes colour over time. As a result, the FFT also picks out the frequencies at which these change too. To filter out these extraneous frequencies my program simply ignores "impossible" frequencies at which no heart-rate would ever beat.
In the video the green graph shows a section of the FFT's result, in particular the part representing "reasonable" heart-rates. You can see that there is a noticeable spike where my heart-rate can be seen.
Download
The (extremely messy, experimental) code can be downloaded from GitHub which requires the following Python libraries:
Sadly, as you might imagine the software isn't particularly robust and the video was the result of several failed takes.
-
Actually the red, green and blue light mixed together doesn't make the original colour seen by the webcam. It is merely an artefact of the human eye that means that they look the same to us. ↩