-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
45 lines (36 loc) · 1.58 KB
/
predict.py
File metadata and controls
45 lines (36 loc) · 1.58 KB
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
# Goal: Predict whether a cough is COVID-19 positive or negative
import sys
import skimage
import tensorflow as tf
import numpy as np
import os
import librosa
# Settings
n_mels = 64 # Number of Mel banks to generate
n_fft = 1024 # Interval we consider to apply FFT. Measured in # of samples
hop_length = 441 # Sliding window for FFT. Measured in # of samples
fmax = 22 # Maximum frequency we want to consider
sampling_rate = 44100 # 44.1kHz sampling rate
fmax = 22050
def extract_melspectrogram(audio_path):
audio, sr = librosa.load(audio_path)
melspectrogram = librosa.feature.melspectrogram(y=audio, sr=sr, n_mels=n_mels, n_fft=n_fft, hop_length=hop_length, fmax=fmax)
melspectrogram = np.log(melspectrogram + 1e-9) # add small number to avoid log(0)
return melspectrogram
def resize_melspectrogram(melspectrogram):
return skimage.transform.resize(melspectrogram, (128, 128))
if __name__ == '__main__':
# Get mel-spectrogram from audio file
audio_path = sys.argv[1]
melspectrogram = extract_melspectrogram(audio_path)
melspectrogram = resize_melspectrogram(melspectrogram)
# Load model
model = tf.keras.models.load_model('covid_cough_classifier.h5')
# Make prediction
prediction = model.predict(melspectrogram.reshape(1, 128, 128, 1))
print("Prediction: covid-19 positive = {:.2f}%, covid-19 negative = {:.2f}%".format(prediction[0][1] * 100 , prediction[0][0] * 100))
print("Conclusion:")
if prediction[0][1] > prediction[0][0]:
print(" Cough is COVID-19 positive")
else:
print(" Cough is COVID-19 negative")