-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.html
161 lines (144 loc) · 4.67 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<html>
<head>
<!-- 2016 Gordon Williams, [email protected]
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<meta charset="utf-8">
<meta name="viewport" content="width=320, initial-scale=1">
<title>Online Heart Rate Monitor</title>
</head>
<body>
<p>Allow this website to use your webcam, then place your finger lightly over the camera and wait for the trace to stabilise.</p>
<p>You will have most success when there is light behind your finger.</p>
<video id="v" width="100" height="100" style="display:none" muted></video>
<canvas id="c" width="100" height="100" style="display:none"></canvas>
<canvas id="g" width="320" height="30"></canvas><br/>
<div id="bpm">--</div>
<p><a href="https://github.com/gfwilliams/HeartRate">GitHub</a></p>
<p>Also, check out <a href="http://www.puck-js.com/">Puck.js</a></p>
<script>
var video, width, height, context, graphCanvas, graphContext, bpm;
var hist = [];
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
var constraints = {video: true, audio:false};
function initialize() {
navigator.mediaDevices.enumerateDevices().then(function(devices) {
devices.forEach(function(device) {
console.log(device.kind + ": " + device.label +
" id = " + device.deviceId/*, JSON.stringify(device,null,2)*/);
if (device.kind=="videoinput" /*&& constraints.video===true*/)
constraints.video = { optional: [{sourceId: device.deviceId}, { fillLightMode: "on" }] };
});
initialize2();
}).catch(function(err) {
console.log(err.name + ": " + err.message);
});
}
function initialize2() {
// The source video.
video = document.getElementById("v");
width = video.width;
height = video.height;
// The target canvas.
var canvas = document.getElementById("c");
context = canvas.getContext("2d");
// The canvas for the graph
graphCanvas = document.getElementById("g");
graphContext = graphCanvas.getContext("2d");
// The bpm meter
bpm = document.getElementById("bpm");
// Get the webcam's stream.
if (navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia(constraints)
.then(startStream)
.catch(console.error)
} else {
navigator.getUserMedia(constraints, startStream, function () {});
}
}
function startStream(stream) {
video.srcObject = stream;
video.play();
// Ready! Let's start drawing.
requestAnimationFrame(draw);
}
function draw() {
var frame = readFrame();
if (frame) {
getIntensity(frame.data);
}
// Wait for the next frame.
requestAnimationFrame(draw);
}
function readFrame() {
try {
context.drawImage(video, 0, 0, width, height);
} catch (e) {
// The video may not be ready, yet.
return null;
}
return context.getImageData(0, 0, width, height);
}
function getIntensity(data) {
var len = data.length;
var sum = 0;
for (var i = 0, j = 0; j < len; i++, j += 4) {
sum += data[j] + data[j+1] + data[j+2];
}
//console.log(sum / len);
hist.push({ bright : sum/len, time : Date.now() });
while (hist.length>graphCanvas.width) hist.shift();
// max and min
var max = hist[0].bright;
var min = hist[0].bright;
hist.forEach(function(v) {
if (v.bright>max) max=v.bright;
if (v.bright<min) min=v.bright;
});
// thresholds for bpm
var lo = min*0.6 + max*0.4;
var hi = min*0.4 + max*0.6;
var pulseAvr = 0, pulseCnt = 0;
// draw
var ctx = graphContext;
ctx.clearRect(0, 0, graphCanvas.width, graphCanvas.height);
ctx.beginPath();
ctx.moveTo(0,0);
hist.forEach(function(v,x) {
var y = graphCanvas.height*(v.bright-min)/(max-min);
ctx.lineTo(x,y);
});
ctx.stroke();
// work out bpm
var isHi = undefined;
var lastHi = undefined;
var lastLo = undefined;
ctx.fillStyle = "red";
hist.forEach(function(v, x) {
if (isHi!=true && v.bright>hi) {
isHi = true;
lastLo = x;
}
if (isHi!=false && v.bright<lo) {
if (lastHi !== undefined && lastLo !== undefined) {
pulseAvr += hist[x].time-hist[lastHi].time;
pulseCnt++;
ctx.fillRect(lastLo,graphCanvas.height-4,lastHi-lastLo,4);
}
isHi = false;
lastHi = x;
}
});
// write bpm
if (pulseCnt) {
var pulseRate = 60000 / (pulseAvr / pulseCnt);
bpm.innerHTML = pulseRate.toFixed(0)+" BPM ("+pulseCnt+" pulses)";
} else {
bpm.innerHTML = "-- BPM";
}
}
addEventListener("DOMContentLoaded", initialize);
</script>
</body>
</html>