File size: 1,633 Bytes
3c0932f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import signal
import os
import wfdb
import shutil
import gradio as gr
from models.inception import *

SAMPLE_FREQUENCY = 512
TIME = 10
NEW_SAMPLE_FREQUENCY = 100

def load_age_model(sample_frequency,recording_time, num_leads):
    cwd = os.getcwd()
    weights = f"{cwd}/models/weights/model_weights_leadI_age.h5"
    model = build_age_model((sample_frequency * recording_time, num_leads), 1)
    model.load_weights(weights)
    return model

def load_gender_model(sample_frequency,recording_time, num_leads):
    cwd = os.getcwd()
    weights = f"{cwd}/models/weights/model_weights_leadI_gender.h5"
    model = build_gender_model((sample_frequency * recording_time, num_leads), 1)
    model.load_weights(weights)
    return model

age_model = load_age_model(100,10,1)
gender_model = load_gender_model(100,10,1)
ecg_path = "../../apple_health_export/electrocardiograms/"
for i in os.listdir(ecg_path):
    df= pd.read_csv(os.path.join(ecg_path,i), skiprows=12, sep=";", header=None,  decimal=',')
    ecg = np.asarray(df[0].str.replace(',', '.').str.replace('−', '-').astype(float))
    new_ecg = signal.resample(ecg[(TIME*SAMPLE_FREQUENCY):(TIME*SAMPLE_FREQUENCY*2)],TIME * NEW_SAMPLE_FREQUENCY)
    # from uV to mV
    new_ecg = new_ecg/1000
    age_estimate = age_model.predict(np.expand_dims(new_ecg,0)).ravel()[0]
    gender_prediction = gender_model.predict(np.expand_dims(new_ecg,0)).ravel()[0]
    print("Estimated age: ", age_estimate)
    print("Predicted gender: Male = {}, Female = {}".format((1- gender_prediction),(gender_prediction)))