0


Python 实现语音转文本

Python 实现语音转文本

Python可以使用多种方式来实现语音转文本,下面介绍其中两种。

方法一:使用Google Speech API

Google Speech API 是 Google 在 2012 年推出的一个 API,可以用于实现语音转文本。使用 Google Speech API 需要安装

  1. SpeechRecognition

库,可以使用 pip 安装:

  1. pip install SpeechRecognition

安装完成后,可以使用下面的代码实现语音转文本:

  1. import speech_recognition as sr
  2. # 设置音频文件的位置
  3. audio_file ='./audio.wav'# 创建 SpeechRecognition 对象
  4. r = sr.Recognizer()# 读取音频文件with sr.AudioFile(audio_file)as source:
  5. 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'

方法二:使用百度语音识别

除了 Google Speech API 外,还可以使用百度语音识别来实现语音转文本。使用百度语音识别需要安装

  1. Baidu-Aip

库,可以使用 pip 安装:

  1. pip install Baidu-Aip

安装完成后,可以使用下面的代码实现语音转文本:

  1. from aip import AipSpeech
  2. # 设置 APPID、API Key 和 Secret Key
  3. APP_ID ='your_app_id'
  4. API_KEY ='your_api_key'
  5. SECRET_KEY ='your_secret_key'# 初始化 AipSpeech 对象
  6. client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)# 设置音频文件的位置
  7. audio_file ='./audio.wav'# 读取音频文件withopen(audio_file,'rb')as fp:
  8. audio_data = fp.read()# 识别音频文件
  9. res = client.asr(audio_data,'wav',16000,{'dev_pid':1536,})if res['err_no']==0:print(res['result'][0])

以上就是使用 Python 实现语音转文本的两种方法。


本文转载自: https://blog.csdn.net/weixin_50814640/article/details/129449238
版权归原作者 I am not people 所有, 如有侵权,请联系我们删除。

“Python 实现语音转文本”的评论:

还没有评论