-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathheartbeat.py
171 lines (137 loc) · 4.87 KB
/
heartbeat.py
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
162
163
164
165
166
167
168
169
170
171
import pickle
import sys
import cv2
import matplotlib.pyplot as plt
import numpy as np
# Helper Methods
def buildGauss(frame, levels):
pyramid = [frame]
for level in range(levels):
frame = cv2.pyrDown(frame)
pyramid.append(frame)
return pyramid
def reconstructFrame(pyramid, index, levels):
filteredFrame = pyramid[index]
for level in range(levels):
filteredFrame = cv2.pyrUp(filteredFrame)
filteredFrame = filteredFrame[:videoHeight, :videoWidth]
return filteredFrame
def graph():
range = (20, 120)
bins = 10
# plotting a histogram
plt.hist(bpmBuffer, bins, range, color='green',
histtype='bar', rwidth=0.8)
# x-axis label
plt.xlabel('Heart rate')
# frequency label
plt.ylabel('Time')
# plot title
plt.title('Rate')
# function to show the plot
plt.show()
# Webcam Parameters
webcam = None
if len(sys.argv) == 2:
webcam = cv2.VideoCapture(sys.argv[1])
else:
webcam = cv2.VideoCapture(0)
realWidth = 320
realHeight = 240
videoWidth = 160
videoHeight = 120
videoChannels = 3
videoFrameRate = 15
webcam.set(3, realWidth);
webcam.set(4, realHeight);
# Output Videos
if len(sys.argv) != 2:
originalVideoFilename = "original.mov"
originalVideoWriter = cv2.VideoWriter()
originalVideoWriter.open(originalVideoFilename, cv2.VideoWriter_fourcc('j', 'p', 'e', 'g'), videoFrameRate,
(realWidth, realHeight), True)
outputVideoFilename = "output.mov"
outputVideoWriter = cv2.VideoWriter()
outputVideoWriter.open(outputVideoFilename, cv2.VideoWriter_fourcc('j', 'p', 'e', 'g'), videoFrameRate,
(realWidth, realHeight), True)
# Color Magnification Parameters
levels = 3
alpha = 170
minFrequency = 1
maxFrequency = 2
bufferSize = 150
bufferIndex = 0
# Output Display Parameters
font = cv2.FONT_HERSHEY_SIMPLEX
loadingTextLocation = (20, 30)
bpmTextLocation = (videoWidth // 2 + 5, 30)
fontScale = 1
fontColor = (255, 255, 255)
lineType = 2
boxColor = (0, 255, 0)
boxWeight = 3
# Initialize Gaussian Pyramid
firstFrame = np.zeros((videoHeight, videoWidth, videoChannels))
firstGauss = buildGauss(firstFrame, levels + 1)[levels]
videoGauss = np.zeros((bufferSize, firstGauss.shape[0], firstGauss.shape[1], videoChannels))
fourierTransformAvg = np.zeros((bufferSize))
# Bandpass Filter for Specified Frequencies
frequencies = (1 * videoFrameRate) * np.arange(bufferSize) / (1 * bufferSize)
mask = (frequencies >= minFrequency) & (frequencies <= maxFrequency)
# Heart Rate Calculation Variables
bpmCalculationFrequency = 15
bpmBufferIndex = 0
bpmBufferSize = 10
bpmBuffer = np.zeros((bpmBufferSize))
i = 0
while (True):
ret, frame = webcam.read()
if ret == False:
break
if len(sys.argv) != 2:
originalFrame = frame.copy()
originalVideoWriter.write(originalFrame)
detectionFrame = frame[int(videoHeight / 2):int(realHeight - videoHeight / 2),
int(videoWidth / 2):int(realWidth - videoWidth / 2), :]
# Construct Gaussian Pyramid
videoGauss[bufferIndex] = buildGauss(detectionFrame, levels + 1)[levels]
fourierTransform = np.fft.fft(videoGauss, axis=0)
# Bandpass Filter
fourierTransform[mask == False] = 0
# Grab a Pulse
if bufferIndex % bpmCalculationFrequency == 0:
i = i + 1
for buf in range(bufferSize):
fourierTransformAvg[buf] = np.real(fourierTransform[buf]).mean()
hz = frequencies[np.argmax(fourierTransformAvg)]
bpm = 60.0 * hz
bpmBuffer[bpmBufferIndex] = bpm
bpmBufferIndex = (bpmBufferIndex + 1) % bpmBufferSize
# Amplify
filtered = np.real(np.fft.ifft(fourierTransform, axis=0))
filtered = filtered * alpha
# Reconstruct Resulting Frame
filteredFrame = reconstructFrame(filtered, bufferIndex, levels)
outputFrame = detectionFrame + filteredFrame
outputFrame = cv2.convertScaleAbs(outputFrame)
bufferIndex = (bufferIndex + 1) % bufferSize
frame[int(videoHeight / 2):int(realHeight - videoHeight / 2), int(videoWidth / 2):int(realWidth - videoWidth / 2),
:] = outputFrame
cv2.rectangle(frame, (int(videoWidth / 2), int(videoHeight / 2)),
(int(realWidth - videoWidth / 2), int(realHeight - videoHeight / 2)), boxColor, boxWeight)
if i > bpmBufferSize:
cv2.putText(frame, "BPM: %d" % bpmBuffer.mean(), bpmTextLocation, font, fontScale, fontColor, lineType)
else:
cv2.putText(frame, "Calculating BPM...", loadingTextLocation, font, fontScale, fontColor, lineType)
outputVideoWriter.write(frame)
if len(sys.argv) != 2:
cv2.imshow("Webcam Heart Rate Monitor", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
graph()
break
val = (int)(bpmBuffer.mean())
with open('pickles/heart_rate.pickle','wb') as file:
pickle.dump(val, file)
webcam.release()
cv2.destroyAllWindows()
outputVideoWriter.release()