Python 实时翻译软件 Python结合百度语音识别实现实时翻译软件的实现
小灰灰搞电子 人气:0一、所需库安装
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
加载全部内容