Raiff1982 commited on
Commit
c324513
·
verified ·
1 Parent(s): f23140a

Create tb_audio_processor.py

Browse files
Files changed (1) hide show
  1. modules/tb_audio_processor.py +40 -0
modules/tb_audio_processor.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import librosa
2
+ import numpy as np
3
+ import logging
4
+ from tensorflow.keras.models import load_model
5
+
6
+ logging.basicConfig(level=logging.INFO)
7
+ logger = logging.getLogger(__name__)
8
+
9
+ class TBAudioProcessor:
10
+ """Processes real cough audio for TB detection"""
11
+
12
+ def __init__(self, model_path="tb_cough_model.h5"):
13
+ try:
14
+ self.model = load_model(model_path)
15
+ logger.info("TB Audio Processor Model Loaded Successfully.")
16
+ except Exception as e:
17
+ logger.error(f"Failed to load TB Audio Model: {e}")
18
+ self.model = None
19
+
20
+ def process_audio(self, audio_path):
21
+ """Analyze cough audio and return TB risk assessment."""
22
+ if not self.model:
23
+ return {"error": "Model not loaded. Cannot process audio."}
24
+
25
+ try:
26
+ y, sr = librosa.load(audio_path, sr=16000)
27
+ mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)
28
+ mfccs = np.mean(mfccs.T, axis=0).reshape(1, -1) # Flatten MFCCs
29
+
30
+ prediction = self.model.predict(mfccs)
31
+ confidence = float(prediction[0][0])
32
+ result = "TB Detected" if confidence > 0.5 else "No TB"
33
+
34
+ return {
35
+ "result": result,
36
+ "confidence": confidence
37
+ }
38
+ except Exception as e:
39
+ logger.error(f"Error processing audio: {e}")
40
+ return {"error": "Audio processing failed."}