如何将转录的 .wav 完全转换为文本。 - Google 演讲 API
How can I conver a transcribed .wav into txt in full extent. - Google Speech API
我在将完整的转录语音转换为文本文件时遇到了问题。最终,我得到了我需要的,但不是音频文件中的全部文本。让我记下这个 (1 Pic), I can see the whole text when I use print() function but get only one line of that text when I try to write it to .txt file (2 Pic).
此外,如果您需要其他信息和内容,可以查看我的代码。提前致谢!
from google.cloud import speech
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'PATH'
client = speech.SpeechClient()
with open('sample.wav', "rb") as audio_file:
content = audio_file.read()
audio = speech.RecognitionAudio(content=content)
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=8000,
language_code="en-US",
# Enable automatic punctuation
enable_automatic_punctuation=True,
)
response = client.recognize(config=config, audio=audio)
for result in response.results:
extr = result.alternatives[0].transcript
print(extr)
with open("guru9.txt","w+") as f:
f.write(extr)
f.close()
您的代码中发生的情况是,每次迭代您打开、写入、关闭文件。您应该将文件的打开和关闭移到循环之外。
myfile = open("guru9.txt","w+")
for result in response.results:
extr = result.alternatives[0].transcript
myfile.write(extr)
myfile.close()
我在将完整的转录语音转换为文本文件时遇到了问题。最终,我得到了我需要的,但不是音频文件中的全部文本。让我记下这个 (1 Pic),
此外,如果您需要其他信息和内容,可以查看我的代码。提前致谢!
from google.cloud import speech
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'PATH'
client = speech.SpeechClient()
with open('sample.wav', "rb") as audio_file:
content = audio_file.read()
audio = speech.RecognitionAudio(content=content)
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=8000,
language_code="en-US",
# Enable automatic punctuation
enable_automatic_punctuation=True,
)
response = client.recognize(config=config, audio=audio)
for result in response.results:
extr = result.alternatives[0].transcript
print(extr)
with open("guru9.txt","w+") as f:
f.write(extr)
f.close()
您的代码中发生的情况是,每次迭代您打开、写入、关闭文件。您应该将文件的打开和关闭移到循环之外。
myfile = open("guru9.txt","w+")
for result in response.results:
extr = result.alternatives[0].transcript
myfile.write(extr)
myfile.close()