-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHeart_Rate.py
294 lines (224 loc) · 9.49 KB
/
Heart_Rate.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# ===================================================================================================================================
# Code: Calculation of average heart rate
# ===================================================================================================================================
# -----------------------------------------------------------------------------------------------------------------------------------
# Importing Libraries
import numpy as np
import matplotlib.pyplot as plt
import time
import datetime
import cv2
facecascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# -----------------------------------------------------------------------------------------------------------------------------------
# Functions
def get_signal(cap):
"""
Parameters
----------
cap : VideoCapture
Retrieves the information transmitted by the camera.
Returns
-------
signal : Array of float64
Matrix of the evolution of pixels over time.
"""
# counter
i = 0
# if state = False the while loope stops
state = True
# matrix of pixel evolution over time
signal = np.zeros((n , nb_pixels_l * nb_pixels_h + nb_pixel_bruit ** 2 + 1))
wait = "Wait"
while state :
# image acquisition
check , frame = cap.read()
# image symmetry
frame = cv2.flip(frame , 1)
# gray shade transformation
gray = cv2.cvtColor(frame , cv2.COLOR_BGR2GRAY)
# face detection
faces = facecascade.detectMultiScale(gray , 1.3 , 5)
if len(faces) != 0 :
for (x , y , w , h) in faces :
if h > 0 and w > 0 :
# face contour
cv2.rectangle(frame , (x , y) ,
(x + w , y + h) , (255 , 0 , 0) , 2)
# pixel selection outline
cv2.rectangle(frame , (x + w // 2 - nb_pixels_l // 2 , y + h // 10 - nb_pixels_h // 2) ,
(x + w // 2 + nb_pixels_l // 2, y + h // 10 + nb_pixels_h // 2) , (255 , 255 , 0) , 2)
pixels = gray[y + h // 10 - nb_pixels_h // 2: y + h // 10 + nb_pixels_h // 2 ,
x + w // 2 - nb_pixels_l // 2: x + w // 2 + nb_pixels_l // 2]
# resizing to be able to create the signal matrix
# at time i, we add the pixels in the form of line vectors
signal[i, :nb_pixels_h * nb_pixels_l] = pixels.ravel()
# pixel in the center of the forehead
cv2.rectangle(frame , (x + w // 2 , y + h // 10) ,
(x + w // 2 + 1 , y + h // 10 + 1) , (255 , 0 , 255) , 2)
# image noise pixel
cv2.rectangle(frame , (20 , 20) ,
(20 + nb_pixel_bruit , 20 + nb_pixel_bruit) , (255 , 255 , 255) , 2)
bruit = gray[20 + nb_pixel_bruit , 20 + nb_pixel_bruit]
signal[i , nb_pixels_h * nb_pixels_l:-1] = bruit.ravel()
# the last noise value is the average of the image
signal[i , -1] = gray.mean()
i += 1
# picture display
font = cv2.FONT_HERSHEY_SIMPLEX # font for the date
text = 'Date : ' + str(datetime.datetime.now())
frame = cv2.putText(frame , text , (10 , 60) , font , .7 , (0 , 255 , 255) , 2 , cv2.LINE_AA)# Adding the date
frame = cv2.putText(frame , "Please don't move" , (10 , 80) , font , .7 , (0 , 255 , 255) , 2 , cv2.LINE_AA)# Adding the date
key = cv2.waitKey(1)
if key == ord('q') : # condition which allows the program to be stopped by pressing the "q" key
break
# progress of pixel acquisition
if i / n * 100 % 5 == 0 :
wait = f'Please wait : {int(i / n * 100)} %'
frame = cv2.putText(frame , wait , (10 , 100) , font , .7 , (0 , 255 , 255) , 2 , cv2.LINE_AA)# Adding the date
cv2.imshow('Heart Health' , frame)
# once the matrix is completed we stop the video loop
if i == n :
state = False
cv2.destroyAllWindows()
return signal
def fourier(signal , n , step) :
"""
Parameters
----------
signal : Array of float64
Matrix of the evolution of pixels over time.
n : int
number of measurements.
step : float
sampling step.
Returns
-------
freq : Array of float64
frequency of sampling points per minute.
sp : Array of float64
discrete Fourier transformation of the signal.
"""
# step in second thus freq is in Hz
freq = np.fft.fftfreq(n , d = step)
sp = np.abs(np.fft.fft(signal , axis=0))
# converting frequency to bpm
freq *= 60
return freq , sp
def filtre(freq , sp , f1 , f2) :
"""
Parameters
----------
freq : Array of float64
frequency of sampling points in bpm.
sp : Array of float64
discrete Fourier transformation of the signal.
f1 : int
minimum frequency allowed.
f2 : int
maximum frequency allowed.
Returns
-------
freq : Array of float64
frequency of sampling points in Hz after being filtered.
sp : Array of float64
Discrete Fourier transformation of the signal after being filtered.
"""
mask = [i and j for i , j in zip(freq > f1 , freq < f2)]
freq = freq[mask]
sp = sp[mask]
return freq , sp
def traitement(signal , sp) :
"""
Parameters
----------
signal : Array of float64
Matrix of the evolution of pixels over time.
sp_filtre : Array of float64
Discrete Fourier transformation of the signal after being filtered.
Returns
-------
temporel : Array of float64
Vector a series of three signal vectors of noises 1 and 2 in the time domain.
frequentiel : Array of float64
Result of the temporal Fourier transform.
"""
# we average all the measurements
pixels = signal[: , : nb_pixels_l * nb_pixels_h].mean(axis = 1)
sp_pixels = sp[: , : nb_pixels_l * nb_pixels_h].mean(axis = 1)
bruit_1 = signal[: , nb_pixels_l * nb_pixels_h : -1].mean(axis = 1)
sp_bruit_1 = sp[: , nb_pixels_l * nb_pixels_h : -1].mean(axis = 1)
bruit_2 = signal[: , -1]
sp_bruit_2 = sp[: , -1]
# we normalize all the results between 0 and 1
pixels = pixels / pixels.max()
sp_pixels = sp_pixels / sp_pixels.max()
bruit_1 = bruit_1 / bruit_1.max()
sp_bruit_1 = sp_bruit_1 / sp_bruit_1.max()
bruit_2 = bruit_2 / bruit_2.max()
sp_bruit_2 = sp_bruit_2 / sp_bruit_2.max()
temporel = np.array([pixels, bruit_1, bruit_2]).T
frequentiel = np.array([sp_pixels, sp_bruit_1, sp_bruit_2]).T
bpm = freq[max(enumerate(sp_pixels) , key = lambda x : x[1])[0]]
return temporel , frequentiel , bpm
def show(freq , temporel , frequentiel) :
"""
Parameters
----------
freq : Array of float64
frequency of sampling points in bpm.
temporel : Array of float64
Vector a series of three signal vectors of noises 1 and 2 in the time domain.
frequentiel : Array of float64
Result of the temporal fourier transform.
Returns
-------
Graphic display.
"""
color = ['b' , 'g' , 'm']
legend_1 = ['pixels' , 'noise_1' , 'noise_2']
legend_2 = ['sp_pixels' , 'sp_noise_1' , 'sp_noise_2']
plt.figure("Graphic representations")
# -------------------------- time domain --------------------------
plt.subplot(211)
plt.plot(np.linspace(0 , end - start , n) , temporel)
plt.xlabel('time (in s)')
plt.legend(legend_1)
# ------------------------- frequency domain -----------------------
plt.subplot(212)
plt.plot(freq , frequentiel)
plt.xlabel('fréquency (in bpm)')
plt.legend(legend_2)
plt.show()
# -----------------------------------------------------------------------------------------------------------------------------------
# Constants
L = 640 # camera return length
H = 480 # camera return height
n = 1000 # number of measurements (duration ~ = 30s for n = 1000)
nb_pixels_l = 100 # measuring surface (even number)
nb_pixels_h = 6
nb_pixel_bruit = 10 # square length (10 * 10 pixels)
f1 = 50 # we keep that the frequencies between f1 and f2
f2 = 180
# video stream initialization
cap = cv2.VideoCapture(0)
cap.set(3 , L)
cap.set(4 , H)
# -----------------------------------------------------------------------------------------------------------------------------------
# Main program
# we start the stopwatch for the sampling frequency
start = time.time()
# we gather the pixels of the face over time
signal = get_signal(cap)
# we stop the chrono and calculate the sampling step
end = time.time()
step = (end - start) / n
# Fourier transform of signals and pixel noise
freq , sp = fourier(signal , n , step)
# filter between f1 and f2 in bpm
freq, sp = filtre(freq , sp , f1 , f2)
# signal processing
temporel , frequentiel, bpm = traitement(signal , sp)
# result display
show(freq , temporel , frequentiel)
# heart rate display on the console
print(f'\n heart rate : {int(bpm)} bpm')