-
Notifications
You must be signed in to change notification settings - Fork 2
/
vitals.py
64 lines (49 loc) · 1.96 KB
/
vitals.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
import tensorflow as tf
import numpy as np
import scipy.io
import os
import sys
import argparse
import h5py
import matplotlib.pyplot as plt
from scipy.signal import butter
from model import MTTS_CAN
from scipy.signal import savgol_filter
from process import preprocess_raw_video,detrend,remove_outliers
def hear_rate(peaklist,fs):
RR_list = []
cnt = 0
while (cnt < (len(peaklist)-1)):
RR_interval = (peaklist[cnt+1] - peaklist[cnt]) #Calculate distance between beats in # of samples
ms_dist = ((RR_interval / fs) * 1000.0) #Convert sample distances to ms distances
RR_list.append(ms_dist) #Append to list
cnt += 1
RR_list=np.array(RR_list)
# RR_list=remove_outliers(RR_list)
bpm = 60000 / np.mean(RR_list) #60000 ms (1 minute) / average R-R interval of signal
return bpm
img_rows = 36
img_cols = 36
frame_depth = 10
def load_model():
model_checkpoint = './mtts_can.hdf5'
model = MTTS_CAN(frame_depth, 32, 64, (img_rows, img_cols, 3))
model.load_weights(model_checkpoint)
return model
def predict_vitals(video_path,model):
batch_size = 100
sample_data_path = video_path
distance=10
dXsub,fs,sample_img = preprocess_raw_video(sample_data_path, dim=36)
dXsub_len = (dXsub.shape[0] // frame_depth) * frame_depth
dXsub = dXsub[:dXsub_len, :, :, :]
yptest = model.predict((dXsub[:, :, :, :3], dXsub[:, :, :, -3:]), batch_size=batch_size, verbose=1)
pulse_pred = yptest[0]
pulse_pred = detrend(np.cumsum(pulse_pred), 100)
[b_pulse, a_pulse] = butter(1, [0.65 / fs * 2, 2.5 / fs * 2], btype='bandpass')
pulse_pred = scipy.signal.filtfilt(b_pulse, a_pulse, np.double(pulse_pred))
resp_pred = yptest[1]
resp_pred = detrend(np.cumsum(resp_pred), 100)
[b_resp, a_resp] = butter(1, [0.08 / fs * 2, 0.5 / fs * 2], btype='bandpass')
resp_pred = scipy.signal.filtfilt(b_resp, a_resp, np.double(resp_pred))
return pulse_pred,resp_pred,fs,sample_img