KasaHealth / analyze_certainty.py
78anand's picture
Upload folder using huggingface_hub
f317798 verified
import os
import sys
import numpy as np
import librosa
from tensorflow.keras.models import load_model
# Import project utils
sys.path.append(os.getcwd())
from utils.hear_extractor import HeARExtractor
from utils.audio_preprocessor import advanced_preprocess
# Config
MODEL_PATH = r"c:\Users\ASUS\lung_ai_project\models\hear_classifier_advanced.h5"
CLASSES_PATH = r"c:\Users\ASUS\lung_ai_project\models\hear_classes_advanced.npy"
files = [
r"C:\Users\ASUS\Downloads\WhatsApp Audio 2026-02-20 at 1.46.51 PM.mpeg", # Correct Healthy (79%)
r"C:\Users\ASUS\Downloads\WhatsApp Audio 2026-02-20 at 1.52.19 PM.mpeg", # Correct Healthy (67%)
r"C:\Users\ASUS\Downloads\WhatsApp Audio 2026-02-20 at 2.06.03 PM.mpeg" # Misclassified Healthy (52% Sick)
]
def analyze_certainty():
extractor = HeARExtractor()
model = load_model(MODEL_PATH, compile=False)
classes = np.load(CLASSES_PATH)
print(f"{'File Name':<35} | {'Pred':<8} | {'Prob Healthy':<13} | {'Prob Sick':<10}")
print("-" * 75)
for f in files:
if not os.path.exists(f):
print(f"File {f} not found")
continue
y, sr = librosa.load(f, sr=16000, duration=5.0)
y_clean = advanced_preprocess(y, sr)
emb = extractor.extract(y_clean)
if emb is not None:
probs = model.predict(emb[np.newaxis, ...], verbose=0)[0]
# Assumes classes are ['healthy', 'sick']
h_prob = probs[0] if classes[0] == 'healthy' else probs[1]
s_prob = probs[1] if classes[1] == 'sick' else probs[0]
pred = classes[np.argmax(probs)]
print(f"{os.path.basename(f):<35} | {pred:<8} | {h_prob*100:>11.2f}% | {s_prob*100:>8.2f}%")
if __name__ == "__main__":
analyze_certainty()