Update app.py
Browse files
app.py
CHANGED
@@ -24,10 +24,11 @@ def analyze_audio(audio_array, threshold=DEFAULT_THRESHOLD):
|
|
24 |
- spectrogram visualization
|
25 |
"""
|
26 |
try:
|
27 |
-
#
|
28 |
if isinstance(audio_array, tuple):
|
29 |
sr, audio = audio_array
|
30 |
-
|
|
|
31 |
else:
|
32 |
audio = audio_array
|
33 |
|
@@ -57,17 +58,22 @@ def analyze_audio(audio_array, threshold=DEFAULT_THRESHOLD):
|
|
57 |
spectrogram = librosa.feature.melspectrogram(
|
58 |
y=audio,
|
59 |
sr=SAMPLING_RATE,
|
60 |
-
n_mels=128
|
61 |
fmax=8000
|
62 |
)
|
63 |
db_spec = librosa.power_to_db(spectrogram, ref=np.max)
|
64 |
|
65 |
-
plt.
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
71 |
plt.tight_layout()
|
72 |
plt.savefig('spec.png', bbox_inches='tight')
|
73 |
plt.close()
|
@@ -93,9 +99,7 @@ with gr.Blocks(title="Industrial Audio Analyzer", theme=gr.themes.Soft()) as dem
|
|
93 |
with gr.Column():
|
94 |
audio_input = gr.Audio(
|
95 |
label="Upload Equipment Audio Recording",
|
96 |
-
type="numpy"
|
97 |
-
source="upload",
|
98 |
-
show_download_button=True
|
99 |
)
|
100 |
threshold = gr.Slider(
|
101 |
minimum=0.5,
|
@@ -107,12 +111,6 @@ with gr.Blocks(title="Industrial Audio Analyzer", theme=gr.themes.Soft()) as dem
|
|
107 |
)
|
108 |
analyze_btn = gr.Button("π Analyze Sound", variant="primary")
|
109 |
|
110 |
-
gr.Examples(
|
111 |
-
examples=["examples/normal_machine.wav", "examples/anomalous_machine.wav"],
|
112 |
-
inputs=audio_input,
|
113 |
-
label="Sample Recordings"
|
114 |
-
)
|
115 |
-
|
116 |
with gr.Column():
|
117 |
result_label = gr.Label(label="Detection Result")
|
118 |
confidence = gr.Textbox(label="Confidence Score")
|
|
|
24 |
- spectrogram visualization
|
25 |
"""
|
26 |
try:
|
27 |
+
# Handle different audio input formats
|
28 |
if isinstance(audio_array, tuple):
|
29 |
sr, audio = audio_array
|
30 |
+
if sr != SAMPLING_RATE:
|
31 |
+
audio = librosa.resample(audio, orig_sr=sr, target_sr=SAMPLING_RATE)
|
32 |
else:
|
33 |
audio = audio_array
|
34 |
|
|
|
58 |
spectrogram = librosa.feature.melspectrogram(
|
59 |
y=audio,
|
60 |
sr=SAMPLING_RATE,
|
61 |
+
n_mels=64, # Reduced from 128 to avoid warning
|
62 |
fmax=8000
|
63 |
)
|
64 |
db_spec = librosa.power_to_db(spectrogram, ref=np.max)
|
65 |
|
66 |
+
fig, ax = plt.subplots(figsize=(10, 4))
|
67 |
+
img = librosa.display.specshow(
|
68 |
+
db_spec,
|
69 |
+
x_axis='time',
|
70 |
+
y_axis='mel',
|
71 |
+
sr=SAMPLING_RATE,
|
72 |
+
fmax=8000,
|
73 |
+
ax=ax
|
74 |
+
)
|
75 |
+
fig.colorbar(img, ax=ax, format='%+2.0f dB')
|
76 |
+
ax.set(title='Mel Spectrogram')
|
77 |
plt.tight_layout()
|
78 |
plt.savefig('spec.png', bbox_inches='tight')
|
79 |
plt.close()
|
|
|
99 |
with gr.Column():
|
100 |
audio_input = gr.Audio(
|
101 |
label="Upload Equipment Audio Recording",
|
102 |
+
type="numpy"
|
|
|
|
|
103 |
)
|
104 |
threshold = gr.Slider(
|
105 |
minimum=0.5,
|
|
|
111 |
)
|
112 |
analyze_btn = gr.Button("π Analyze Sound", variant="primary")
|
113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
with gr.Column():
|
115 |
result_label = gr.Label(label="Detection Result")
|
116 |
confidence = gr.Textbox(label="Confidence Score")
|