Python 录音转文字

在 Python 中,可以使用第三方库来实现录音文件转文字的功能。一个常用的库是 speech_recognition

import speech_recognition as sr

# 创建语音识别器
r = sr.Recognizer()

# 从录音文件读取音频
with sr.AudioFile('audio_file.wav') as source:
    audio_data = r.record(source)

# 使用 Google Speech Recognition 进行语音识别
try:
    text = r.recognize_google(audio_data, language='zh-CN')
    print("Transcription:", text)
except sr.UnknownValueError:
    print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
    print("Could not request results from Google Speech Recognition service; {0}".format(e))

具体步骤如下:

  1. 导入 speech_recognition 库。
  2. 创建一个 Recognizer 对象,用于识别语音。
  3. 使用 AudioFile 对象读取录音文件中的音频数据。
  4. 调用 recognize_google() 方法,使用 Google 语音识别服务将音频转换为文字。
  5. 如果识

你可能感兴趣的:(python,开发语言)