Spaces:
Sleeping
Sleeping
Delete main_audio.py
Browse files- main_audio.py +0 -89
main_audio.py
DELETED
|
@@ -1,89 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import librosa
|
| 3 |
-
import librosa.display
|
| 4 |
-
import matplotlib.pyplot as plt
|
| 5 |
-
import numpy as np
|
| 6 |
-
import tensorflow as tf
|
| 7 |
-
from tensorflow.keras import models
|
| 8 |
-
|
| 9 |
-
# ========== Utility: Save mel spectrogram ==========
|
| 10 |
-
def save_mel_spectrogram(file_path, save_dir="temp_specs", sr=22050, n_mels=128, hop_length=512, n_fft=2048):
|
| 11 |
-
y, sr = librosa.load(file_path, sr=sr, mono=True)
|
| 12 |
-
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=n_mels, n_fft=n_fft, hop_length=hop_length)
|
| 13 |
-
S_db = librosa.power_to_db(S, ref=np.max)
|
| 14 |
-
|
| 15 |
-
os.makedirs(save_dir, exist_ok=True)
|
| 16 |
-
save_path = os.path.join(save_dir, os.path.basename(file_path).replace(".wav", ".png"))
|
| 17 |
-
|
| 18 |
-
plt.figure(figsize=(4, 4))
|
| 19 |
-
librosa.display.specshow(S_db, sr=sr, hop_length=hop_length, x_axis='time', y_axis='mel', cmap='magma')
|
| 20 |
-
plt.axis("off")
|
| 21 |
-
plt.savefig(save_path, bbox_inches="tight", pad_inches=0)
|
| 22 |
-
plt.close()
|
| 23 |
-
|
| 24 |
-
return save_path
|
| 25 |
-
|
| 26 |
-
# ========== Hierarchical Classifier ==========
|
| 27 |
-
class HierarchicalClassifier:
|
| 28 |
-
def __init__(self, stage1_model, abnormal_model, normal_model,
|
| 29 |
-
stage1_classes, abnormal_classes, normal_classes, img_size=(224, 224)):
|
| 30 |
-
self.img_size = img_size
|
| 31 |
-
self.stage1_model = stage1_model
|
| 32 |
-
self.abnormal_model = abnormal_model
|
| 33 |
-
self.normal_model = normal_model
|
| 34 |
-
self.stage1_classes = stage1_classes
|
| 35 |
-
self.abnormal_classes = abnormal_classes
|
| 36 |
-
self.normal_classes = normal_classes
|
| 37 |
-
|
| 38 |
-
def preprocess(self, image_path):
|
| 39 |
-
img = tf.keras.utils.load_img(image_path, target_size=self.img_size)
|
| 40 |
-
img_array = tf.keras.utils.img_to_array(img) / 255.0
|
| 41 |
-
img_array = tf.expand_dims(img_array, 0)
|
| 42 |
-
return img_array
|
| 43 |
-
|
| 44 |
-
def predict(self, image_path):
|
| 45 |
-
img_array = self.preprocess(image_path)
|
| 46 |
-
stage1_pred = self.stage1_model.predict(img_array, verbose=0)
|
| 47 |
-
stage1_idx = np.argmax(stage1_pred)
|
| 48 |
-
main_class = self.stage1_classes[stage1_idx]
|
| 49 |
-
|
| 50 |
-
if main_class == "00 - Abnormal":
|
| 51 |
-
sub_pred = self.abnormal_model.predict(img_array, verbose=0)
|
| 52 |
-
sub_idx = np.argmax(sub_pred)
|
| 53 |
-
sub_class = self.abnormal_classes[sub_idx]
|
| 54 |
-
else:
|
| 55 |
-
sub_pred = self.normal_model.predict(img_array, verbose=0)
|
| 56 |
-
sub_idx = np.argmax(sub_pred)
|
| 57 |
-
sub_class = self.normal_classes[sub_idx]
|
| 58 |
-
|
| 59 |
-
return {
|
| 60 |
-
"stage1_class": main_class,
|
| 61 |
-
"stage1_confidence": float(np.max(stage1_pred)),
|
| 62 |
-
"stage2_class": sub_class,
|
| 63 |
-
"stage2_confidence": float(np.max(sub_pred)),
|
| 64 |
-
"final_prediction": f"{main_class} → {sub_class}"
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
# ========== Load Models ==========
|
| 68 |
-
stage1_model = models.load_model("saved_models/stage1_model.h5")
|
| 69 |
-
abnormal_model = models.load_model("saved_models/abnormal_model.h5")
|
| 70 |
-
normal_model = models.load_model("saved_models/normal_model.h5")
|
| 71 |
-
|
| 72 |
-
# Define class lists (same order as training!)
|
| 73 |
-
stage1_classes = ["00 - Abnormal", "01 - Normal"]
|
| 74 |
-
abnormal_classes = os.listdir("MelSpectrograms/00 - Abnormal")
|
| 75 |
-
normal_classes = os.listdir("MelSpectrograms/01 - Normal")
|
| 76 |
-
|
| 77 |
-
classifier = HierarchicalClassifier(stage1_model, abnormal_model, normal_model,
|
| 78 |
-
stage1_classes, abnormal_classes, normal_classes)
|
| 79 |
-
|
| 80 |
-
# ========== Example Inference ==========
|
| 81 |
-
audio_file = "C:/Users/dell/3D Objects/Samsung Prism/Brain\Audio/audio-washing-machine/Washing machine/00 - Abnormal/00-2 - Dehydration mode noise/04.wav" # 🔹 Replace with your audio file
|
| 82 |
-
spec_path = save_mel_spectrogram(audio_file)
|
| 83 |
-
|
| 84 |
-
result = classifier.predict(spec_path)
|
| 85 |
-
print("🎯 Final Prediction:", result["final_prediction"])
|
| 86 |
-
print("Stage 1:", result["stage1_class"], "| Confidence:", result["stage1_confidence"])
|
| 87 |
-
print("Stage 2:", result["stage2_class"], "| Confidence:", result["stage2_confidence"])
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|