簡體   English   中英

如何將長語音轉換為文本。 Python 中的 Google 異步語音識別

[英]How to convert long speech to text. Google asyncronous speech recognition in Python

有誰知道如何通過使用谷歌語音到文本異步 API 將 wav 或 MP3 格式的長語音轉換為文本? 我正在使用下面的代碼,但在查找 upload_blob() function 時總是出錯。

提前致謝。

from pydub import AudioSegment
import io
import os
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types
import wave
from google.cloud import storage

filepath = "/home/marte/Documentos/Python_Scripts/Law/" 
output_filepath = "/home/marte/Documentos/Python_Scripts/Law/" 
bucketname = "callsaudiofiles" 
def frame_rate_channel(audio_file_name):
    with wave.open(audio_file_name, "rb") as wave_file:
        frame_rate = wave_file.getframerate()
        channels = wave_file.getnchannels()
        return frame_rate,channels
def google_transcribe(audio_file_name):
    file_name = filepath + audio_file_name 
    frame_rate, channels = frame_rate_channel(audio_file_name)
    bucket_name = bucketname
    source_file_name = filepath + audio_file_name
    destination_blob_name = audio_file_name
    upload_blob(bucket_name, source_file_name, destination_blob_name)
    gcs_uri = 'gs://' + bucketname + '/' + audio_file_name
    transcript = ''
    client = speech.SpeechClient()
    audio = types.RecognitionAudio(uri=gcs_uri)
    config = types.RecognitionConfig(
    encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz=frame_rate,
    language_code='es-CO')
    operation = client.long_running_recognize(config, audio)
    response = operation.result(timeout=10000)
    for result in response.results:
        transcript += result.alternatives[0].transcript
    delete_blob(bucket_name, destination_blob_name)
    return transcript
def write_transcripts(transcript_filename,transcript):
    f= open(output_filepath + transcript_filename,"w+")
    f.write(transcript)
    f.close()
sentencia = google_transcribe("onu-santos.wav")
write_transcripts("onu-santos.txt",sentencia)

我終於設法通過在我的 Google Cloud Storage 帳戶中設置一個新的“段”並將憑據設置為本地變量來解決問題:

 export GOOGLE_APPLICATION_CREDENTIALS='/home/marte/Documentos/Python_Scripts/Speech/sequoia-31501208ed06.json'

此鏈接很有用: https://cloud.google.com/docs/authentication/getting-started

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM