Spaces:
Running
Running
| import os | |
| import sys | |
| import numpy as np | |
| import pandas as pd | |
| import librosa | |
| import soundfile as sf | |
| from tensorflow.keras.models import load_model | |
| import random | |
| sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
| 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" | |
| RESP_BASE = r"c:\Users\ASUS\lung_ai_project\data\extracted_cough\Respiratory_Sound_Dataset-main" | |
| COS_BASE = r"c:\Users\ASUS\lung_ai_project\data\coswara" | |
| def run_debug_test(): | |
| print("DEBUG: Initializing...") | |
| extractor = HeARExtractor() | |
| print("DEBUG: Loading Model...") | |
| model = load_model(MODEL_PATH, compile=False) | |
| classes = np.load(CLASSES_PATH) | |
| print(f"DEBUG: Classes are {classes}") | |
| # Pick one known sample | |
| sample_path = r"c:\Users\ASUS\lung_ai_project\data\extracted_cough\Respiratory_Sound_Dataset-main\audio_and_txt_files\104_1b1_Al_sc_Litt3200.wav" | |
| true_label = "sick" | |
| print(f"DEBUG: Testing on {sample_path}") | |
| if not os.path.exists(sample_path): | |
| print("DEBUG: Sample path not found!") | |
| return | |
| # 1. Load Audio | |
| y, sr = librosa.load(sample_path, sr=16000, duration=5.0) | |
| print(f"DEBUG: Loaded audio, shape {y.shape}") | |
| # 2. Preprocess | |
| y_clean = advanced_preprocess(y, sr) | |
| print(f"DEBUG: Preprocessed audio, length {len(y_clean)}") | |
| # 3. Save to Temp | |
| temp_path = "debug_temp.wav" | |
| sf.write(temp_path, y_clean, 16000) | |
| print(f"DEBUG: Saved temp file") | |
| # 4. Extract | |
| embedding = extractor.extract(temp_path) | |
| if embedding is not None: | |
| print(f"DEBUG: Extracted embedding, shape {embedding.shape}") | |
| X = embedding[np.newaxis, ...] | |
| preds = model.predict(X, verbose=0) | |
| print(f"DEBUG: Raw predictions: {preds}") | |
| pred_idx = np.argmax(preds[0]) | |
| pred_label = classes[pred_idx] | |
| print(f"DEBUG: Predicted label: {pred_label}") | |
| status = "OK" if pred_label == true_label else "MIS" | |
| print(f"DEBUG: Result: {status}") | |
| else: | |
| print("DEBUG: Embedding extraction FAILED") | |
| if __name__ == "__main__": | |
| run_debug_test() | |