MichalIwaniuk commited on
Commit
96b9863
1 Parent(s): 51fae2f
Files changed (1) hide show
  1. app.py +61 -27
app.py CHANGED
@@ -1,41 +1,75 @@
1
  import gradio as gr
2
- import tensorflow as tf
3
- import librosa
4
  import numpy as np
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Wczytanie modelu
7
  model = tf.keras.models.load_model("model.h5")
8
 
9
- # Lista klas (dostosuj do swojego przypadku)
10
- INSTRUMENTS = ['cel', 'cla', 'flu', 'gac', 'gel', 'org', 'pia', 'sax', 'tru', 'vio', 'voi'] # <- zmie艅 na w艂asne klasy
 
 
11
 
12
- # Parametry przetwarzania
13
- SR = 22050
14
- DURATION = 5
15
- N_MELS = 128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- def preprocess_audio(file):
18
- y, _ = librosa.load(file.name, sr=SR, duration=DURATION)
19
- if len(y) < SR * DURATION:
20
- y = np.pad(y, (0, SR * DURATION - len(y)))
21
- mel = librosa.feature.melspectrogram(y=y, sr=SR, n_mels=N_MELS)
22
- mel_db = librosa.power_to_db(mel, ref=np.max)
23
- mel_db = mel_db[..., np.newaxis] # Dodaj kana艂
24
- mel_db = np.expand_dims(mel_db, axis=0) # Dodaj batch
25
- return mel_db
26
-
27
- def predict_instrument(audio_file):
28
- mel_input = preprocess_audio(audio_file)
29
- preds = model.predict(mel_input)[0]
30
- result = {cls: float(score) for cls, score in zip(INSTRUMENTS, preds)}
31
- return result
32
 
 
33
  demo = gr.Interface(
34
- fn=predict_instrument,
35
  inputs=gr.Audio(type="filepath", label="Wgraj plik WAV"),
36
- outputs=gr.Label(num_top_classes=3),
37
- title="Rozpoznawanie Instrument贸w",
38
- description="Model rozpoznaje instrumenty muzyczne na podstawie pliku d藕wi臋kowego (.wav)."
 
 
 
39
  )
40
 
41
  if __name__ == "__main__":
 
1
  import gradio as gr
 
 
2
  import numpy as np
3
+ import librosa
4
+ import librosa.display
5
+ import tensorflow as tf
6
+ import matplotlib.pyplot as plt
7
+
8
+ # Parametry modelu
9
+ SR = 22050
10
+ N_MELS = 128
11
+ TARGET_FRAMES = 216
12
+ LABELS = ['cel', 'cla', 'flu', 'gac', 'gel', 'org', 'pia', 'sax', 'tru', 'vio', 'voi']
13
 
14
  # Wczytanie modelu
15
  model = tf.keras.models.load_model("model.h5")
16
 
17
+ def compute_melspectrogram(y, sr=SR, n_mels=N_MELS):
18
+ S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=n_mels)
19
+ S_DB = librosa.power_to_db(S, ref=np.max)
20
+ return S_DB
21
 
22
+ def resize_spectrogram(S, target_frames=TARGET_FRAMES):
23
+ if S.shape[1] < target_frames:
24
+ pad = target_frames - S.shape[1]
25
+ left = pad // 2; right = pad - left
26
+ S = np.pad(S, ((0, 0), (left, right)), mode='constant')
27
+ elif S.shape[1] > target_frames:
28
+ start = (S.shape[1] - target_frames) // 2
29
+ S = S[:, start:start+target_frames]
30
+ return S
31
+
32
+ def predict_and_plot(audio_path):
33
+ # Wczytaj audio
34
+ y, _ = librosa.load(audio_path, sr=SR)
35
+
36
+ # Oblicz spektrogram
37
+ S_full = compute_melspectrogram(y)
38
+ S = resize_spectrogram(S_full)
39
+
40
+ # Przygotuj do predykcji
41
+ x = S[np.newaxis, ..., np.newaxis]
42
+ probs = model.predict(x, verbose=0)[0]
43
+
44
+ # Przygotuj spektrogram jako obrazek
45
+ fig, ax = plt.subplots(figsize=(8, 4))
46
+ librosa.display.specshow(S_full, sr=SR, x_axis='time', y_axis='mel', cmap='magma', ax=ax)
47
+ ax.set_title("Mel-spektrogram")
48
+ ax.set_xlabel("Czas")
49
+ ax.set_ylabel("Cz臋stotliwo艣膰 (Mel)")
50
+ plt.tight_layout()
51
+
52
+ # Zapisz obrazek do zmiennej
53
+ import io
54
+ buf = io.BytesIO()
55
+ plt.savefig(buf, format='png')
56
+ plt.close(fig)
57
+ buf.seek(0)
58
 
59
+ # Zwr贸膰 predykcje i obraz
60
+ predictions = {label: float(p) for label, p in zip(LABELS, probs)}
61
+ return predictions, buf
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ # Gradio UI
64
  demo = gr.Interface(
65
+ fn=predict_and_plot,
66
  inputs=gr.Audio(type="filepath", label="Wgraj plik WAV"),
67
+ outputs=[
68
+ gr.Label(num_top_classes=5, label="Predykcje instrumentu"),
69
+ gr.Image(label="Spektrogram")
70
+ ],
71
+ title="Rozpoznawanie instrument贸w z d藕wi臋ku",
72
+ description="Model na podstawie spektrogramu melowego rozpoznaje instrument muzyczny. Obs艂ugiwane klasy: " + ", ".join(LABELS)
73
  )
74
 
75
  if __name__ == "__main__":