Python は音声テキスト変換を実装します

Python は音声テキスト変換を実装します

Python はさまざまな方法で音声テキスト変換を実装できますが、そのうちの 2 つについては以下で説明します。

方法 1: Google Speech API を使用する

Google Speech API は、2012 年に Google によって開始された API であり、音声テキスト変換の実装に使用できます。Google Speech API を使用するには、SpeechRecognitionライブラリ。これは pip を使用してインストールできます。

pip install SpeechRecognition

インストールが完了したら、次のコードを使用して音声テキスト変換を実装できます。

import speech_recognition as sr

# 设置音频文件的位置
audio_file = './audio.wav'

# 创建 SpeechRecognition 对象
r = sr.Recognizer()

# 读取音频文件
with sr.AudioFile(audio_file) as source:
    audio = r.record(source)

# 识别音频文件
try:
    print(r.recognize_google(audio, language='zh-CN'))
except sr.UnknownValueError:
    raise 'Google Speech Recognition could not understand audio'
except sr.RequestError as e:
    raise 'Could not request results from Google Speech Recognition Service'

方法 2: Baidu 音声認識を使用する

Google Speech API に加えて、Baidu 音声認識を使用して音声テキスト変換を実装することもできます。Baidu 音声認識を使用するにはBaidu-Aipライブラリ。pip を使用してインストールできます。

pip install Baidu-Aip

インストールが完了したら、次のコードを使用して音声テキスト変換を実装できます。

from aip import AipSpeech

# 设置 APPID、API Key 和 Secret Key
APP_ID = 'your_app_id'
API_KEY = 'your_api_key'
SECRET_KEY = 'your_secret_key'

# 初始化 AipSpeech 对象
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

# 设置音频文件的位置
audio_file = './audio.wav'

# 读取音频文件
with open(audio_file, 'rb') as fp:
    audio_data = fp.read()

# 识别音频文件
res = client.asr(audio_data, 'wav', 16000, {
    
    
    'dev_pid': 1536,
})
if res['err_no'] == 0:
    print(res['result'][0])

上記は、Python を使用して音声テキスト変換を実装する 2 つの方法です。

おすすめ

転載: blog.csdn.net/weixin_50814640/article/details/129449238
おすすめ