Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,66 +1,77 @@
|
|
1 |
import tempfile
|
2 |
import webrtcvad
|
3 |
import speech_recognition as sr
|
|
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
8 |
|
9 |
-
# Creamos un objeto VAD y un reconocedor de voz
|
10 |
-
vad = webrtcvad.Vad()
|
11 |
-
recognizer = sr.Recognizer()
|
12 |
|
13 |
-
#
|
14 |
-
|
15 |
-
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
# Procesamos el
|
32 |
-
|
33 |
-
|
34 |
-
frame = audio_data[i:i+frame_size]
|
35 |
|
36 |
-
#
|
37 |
-
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
vad_active = True
|
42 |
-
speech_detected = True
|
43 |
-
print("️ Detección de voz iniciada")
|
44 |
-
elif not is_speech and vad_active:
|
45 |
-
vad_active = False
|
46 |
-
print("⏹️ Detección de voz finalizada")
|
47 |
-
|
48 |
-
# Si se ha detectado voz y hay un silencio, transcribimos la frase
|
49 |
-
if speech_detected and not is_speech:
|
50 |
-
# Transcribimos la frase
|
51 |
-
with sr.AudioData(frame, sample_rate) as source:
|
52 |
-
audio = recognizer.record(source)
|
53 |
-
try:
|
54 |
-
text = recognizer.recognize_google(audio)
|
55 |
-
phrase += f" {text}"
|
56 |
-
print(f"️ {text}")
|
57 |
-
except sr.RequestError:
|
58 |
-
print("⚠️ Error al transcribir la frase")
|
59 |
-
except sr.UnknownValueError:
|
60 |
-
print("⚠️ No se ha reconocido la frase")
|
61 |
-
|
62 |
-
# Reiniciamos el indicador de frase
|
63 |
-
speech_detected = False
|
64 |
|
65 |
-
#
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import tempfile
|
2 |
import webrtcvad
|
3 |
import speech_recognition as sr
|
4 |
+
import os
|
5 |
|
6 |
+
def process_audio_file(audio_file_path):
|
7 |
+
# Configuramos la tasa de muestreo y el tamaño del frame
|
8 |
+
sample_rate = 16000
|
9 |
+
frame_size = 30
|
10 |
|
11 |
+
# Creamos un objeto VAD y un reconocedor de voz
|
12 |
+
vad = webrtcvad.Vad()
|
13 |
+
recognizer = sr.Recognizer()
|
14 |
|
15 |
+
# Indicadores de estado
|
16 |
+
vad_active = False
|
17 |
+
speech_detected = False
|
18 |
+
phrase = ""
|
19 |
|
20 |
+
try:
|
21 |
+
# 1. Load the audio data from the original file:
|
22 |
+
with open(audio_file_path, "rb") as f:
|
23 |
+
audio_data = f.read()
|
24 |
|
25 |
+
except FileNotFoundError:
|
26 |
+
print(f"Error: File not found - {audio_file_path}")
|
27 |
+
return
|
28 |
|
29 |
+
# 2. Use a temporary file to process the audio data:
|
30 |
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=True) as temp_file:
|
31 |
+
temp_file.write(audio_data)
|
32 |
+
temp_file.flush()
|
33 |
|
34 |
+
# Procesamos el archivo temporal
|
35 |
+
with open(temp_file.name, "rb") as f:
|
36 |
+
audio_data = f.read()
|
|
|
37 |
|
38 |
+
# Procesamos el audio en frames
|
39 |
+
for i in range(0, len(audio_data), frame_size):
|
40 |
+
# Obtenemos el frame actual
|
41 |
+
frame = audio_data[i:i + frame_size]
|
42 |
|
43 |
+
# Detectamos si hay voz en el frame
|
44 |
+
is_speech = vad.is_speech(frame, sample_rate)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
+
# Actualizamos los indicadores de estado
|
47 |
+
if is_speech and not vad_active:
|
48 |
+
vad_active = True
|
49 |
+
speech_detected = True
|
50 |
+
print("️ Detección de voz iniciada")
|
51 |
+
elif not is_speech and vad_active:
|
52 |
+
vad_active = False
|
53 |
+
print("⏹️ Detección de voz finalizada")
|
54 |
+
|
55 |
+
# Si se ha detectado voz y hay un silencio, transcribimos la frase
|
56 |
+
if speech_detected and not is_speech:
|
57 |
+
# Transcribimos la frase
|
58 |
+
with sr.AudioData(frame, sample_rate) as source:
|
59 |
+
audio = recognizer.record(source)
|
60 |
+
try:
|
61 |
+
text = recognizer.recognize_google(audio)
|
62 |
+
phrase += f" {text}"
|
63 |
+
print(f"️ {text}")
|
64 |
+
except sr.RequestError:
|
65 |
+
print("⚠️ Error al transcribir la frase")
|
66 |
+
except sr.UnknownValueError:
|
67 |
+
print("⚠️ No se ha reconocido la frase")
|
68 |
+
|
69 |
+
# Reiniciamos el indicador de frase
|
70 |
+
speech_detected = False
|
71 |
+
|
72 |
+
# Imprimimos la frase completa
|
73 |
+
print(f"Transcripción completa: {phrase}")
|
74 |
+
|
75 |
+
# Example usage:
|
76 |
+
audio_file_path = os.path.join(os.getcwd(), "audio.wav") # Replace "audio.wav" with your actual file name
|
77 |
+
process_audio_file(audio_file_path)
|