100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python识别英语语音翻译器_Python结合百度语音识别实现实时翻译软件的实现

python识别英语语音翻译器_Python结合百度语音识别实现实时翻译软件的实现

时间:2022-10-23 23:42:54

相关推荐

python识别英语语音翻译器_Python结合百度语音识别实现实时翻译软件的实现

一、所需库安装

pip install PyAudio

pip install SpeechRecognition

pip install baidu-aip

pip install Wave

pip install Wheel

pip install Pyinstaller

二、百度官网申请服务

三、源代码分享

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

到此这篇关于Python结合百度语音识别实现实时翻译软件的实现的文章就介绍到这了,更多相关Python 实时翻译软件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。