Created
April 6, 2019 18:31
-
-
Save deivguerrero/2dd3ec86d81f03849f11a86dd08b1262 to your computer and use it in GitHub Desktop.
Obtención de la transcripción de archivos de audio corto (.flac)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import audioread | |
import glob | |
import json | |
import os | |
from google.cloud import speech | |
from google.cloud.speech import enums | |
from google.cloud.speech import types | |
x = os.path.dirname(os.path.realpath(__file__)) | |
x = os.path.join(x, "*.flac") | |
audios = glob.glob(x) | |
transcripts = {} | |
config = { | |
"encoding": enums.RecognitionConfig.AudioEncoding.FLAC, | |
"language_code": "es-MX" | |
} | |
output = {} | |
client = speech.SpeechClient() | |
for af in audios: | |
with open(af, "rb") as audio_file: | |
audio_channels = 1 | |
with audioread.audio_open(af) as afc: | |
audio_channels = int(afc.channels) | |
content = audio_file.read() | |
af_name = os.path.basename(audio_file.name) | |
config.update({ | |
"audio_channel_count": audio_channels, | |
"enable_separate_recognition_per_channel": True | |
}) | |
rec_config = types.RecognitionConfig(**config) | |
audio = types.RecognitionAudio(content=content) | |
response = client.recognize(rec_config, audio) | |
output.update({af_name: {"transcripciones": [], "confianza": []}}) | |
print("\n\nArchivo: {}\n".format(af_name)) | |
for result in response.results: | |
for r in result.alternatives: | |
output[af_name]["transcripciones"].append(r.transcript) | |
output[af_name]["confianza"].append(r.transcript) | |
print(r.transcript) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment