gnosticdev commited on
Commit
d93a0b9
·
verified ·
1 Parent(s): b770a5d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import librosa
4
+ from scipy.signal import find_peaks
5
+ from sklearn.cluster import KMeans
6
+
7
+ def decodificar(audio):
8
+
9
+ if audio is None:
10
+ return "No audio"
11
+
12
+ path = audio
13
+
14
+ y, sr = librosa.load(path, sr=None)
15
+
16
+ frame = int(sr * 0.04)
17
+
18
+ stft = np.abs(
19
+ librosa.stft(
20
+ y,
21
+ n_fft=frame*2,
22
+ hop_length=frame
23
+ )
24
+ )
25
+
26
+ freqs = librosa.fft_frequencies(sr=sr)
27
+
28
+ tonos = []
29
+
30
+ for f in stft.T:
31
+
32
+ if np.max(f) == 0:
33
+ continue
34
+
35
+ f = f / np.max(f)
36
+
37
+ peaks, _ = find_peaks(f, height=0.2)
38
+
39
+ if len(peaks) == 0:
40
+ continue
41
+
42
+ peak_freqs = freqs[peaks]
43
+
44
+ peak_freqs = peak_freqs[
45
+ (peak_freqs > 300) &
46
+ (peak_freqs < 4000)
47
+ ]
48
+
49
+ if len(peak_freqs):
50
+ tonos.append(peak_freqs[0])
51
+
52
+ if len(tonos) < 10:
53
+ return "Sin señal tonal clara"
54
+
55
+ tonos = np.array(tonos).reshape(-1,1)
56
+
57
+ kmeans = KMeans(n_clusters=12, n_init=10)
58
+ kmeans.fit(tonos)
59
+
60
+ centros = sorted(kmeans.cluster_centers_.flatten())
61
+
62
+ letras = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
63
+
64
+ texto=""
65
+
66
+ for f in tonos.flatten():
67
+
68
+ cercano = min(centros, key=lambda x: abs(x-f))
69
+ idx = centros.index(cercano)
70
+
71
+ if idx < len(letras):
72
+ texto += letras[idx]
73
+
74
+ return texto
75
+
76
+
77
+ with gr.Blocks() as demo:
78
+
79
+ gr.Markdown("# Decodificador de tonos estilo radio digital")
80
+
81
+ audio = gr.Audio(type="filepath")
82
+
83
+ boton = gr.Button("Decodificar")
84
+
85
+ salida = gr.Textbox(lines=10)
86
+
87
+ boton.click(
88
+ decodificar,
89
+ inputs=audio,
90
+ outputs=salida
91
+ )
92
+
93
+ demo.launch()