File size: 2,453 Bytes
f317798
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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()