快活林资源网 Design By www.csstdc.com

一、所需库安装

Python结合百度语音识别实现实时翻译软件的实现

pip install PyAudio
pip install SpeechRecognition
pip install baidu-aip
pip install Wave
pip install Wheel
pip install Pyinstaller

二、百度官网申请服务

Python结合百度语音识别实现实时翻译软件的实现

三、源代码分享

import pyaudio
import wave
from aip import AipSpeech
import time




# 用Pyaudio库录制音频
#  out_file:输出音频文件名
#  rec_time:音频录制时间(秒)
def audio_record(out_file, rec_time):
  CHUNK = 1024
  FORMAT = pyaudio.paInt16 # 16bit编码格式
  CHANNELS = 1 # 单声道
  RATE = 16000 # 16000采样频率

  p = pyaudio.PyAudio()
  # 创建音频流
  stream = p.open(format=FORMAT, # 音频流wav格式
          channels=CHANNELS, # 单声道
          rate=RATE, # 采样率16000
          input=True,
          frames_per_buffer=CHUNK)

  print("开始记录语音{0}秒后开始识别...".format(rec_time))

  frames = [] # 录制的音频流
  # 录制音频数据
  for i in range(0, int(RATE / CHUNK * rec_time)):
    data = stream.read(CHUNK)
    frames.append(data)

  # 录制完成
  stream.stop_stream()
  stream.close()
  p.terminate()

  print("结束识别")

  # 保存音频文件
  wf = wave.open(out_file, 'wb')
  wf.setnchannels(CHANNELS)
  wf.setsampwidth(p.get_sample_size(FORMAT))
  wf.setframerate(RATE)
  wf.writeframes(b''.join(frames))
  wf.close()


def audio_recog(recogFile):
  # 读取文件
  def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
      return fp.read()


  # 识别本地文件
  result = client.asr(get_file_content(recogFile), 'wav', 16000, {'dev_pid': 1537,})
  return result

def write_file(file,text):
  import time
  time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
  f = open(file, 'a')
  f.write(time+':'+text+'\n')
  f.close()

audioFile="audio.wav"
textFile="识别结果.txt"

""" 你的 APPID AK SK """
APP_ID = '你的APP_ID'
API_KEY = '你的API_KEY'
SECRET_KEY = '你的SECRET_KEY'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)


if __name__ == '__main__':
  while True:
    audio_record(audioFile, 5)
    textResult = audio_recog("audio.wav")
    if textResult['err_msg'] =="success.":
      print(textResult['result'])
      write_file(textFile,str(textResult['result']))

四、打包成软件

进入到目录执行下面命令:

pyinstaller -F main.py
快活林资源网 Design By www.csstdc.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
快活林资源网 Design By www.csstdc.com