即时翻译官

任务一:录制语音 imp...

任务一:录制语音

import speech_recognition as sr
import wave


# 初始化识别器
r = sr.Recognizer()

with sr.Microphone(sample_rate=16000, chunk_size=1024) as source:
    print("开始录音...")
    audio = r.listen(source, timeout=6)

    # 调整识别器的灵敏度
    r.adjust_for_ambient_noise(source)

    with open('recording.wav', 'wb') as f:
      f.write(audio.get_wav_data())
    
    print("录音完成。")      

任务二:语音翻译

 
import base64
import urllib
import requests
import json
# import pyttsx3
# engine = pyttsx3.init()
from playsound import playsound

# 百度 AK SK 
API_KEY = "DIi3RgZLEf6ZkioybsF40qA3"
SECRET_KEY = "BEH1SLajbFxplEz6tqUaDulSrWOkDfwq"

########################
# 百度语音翻译代码
def main():
    url = "https://aip.baidubce.com/rpc/2.0/mt/v2/speech-translation?access_token=" + get_access_token()
    
    # voice 可以通过 get_file_content_as_base64("recording.wav",False) 方法获取
    payload = json.dumps({
        "from": "zh",
        "to": "en",
        "format": "wav",
        "voice": get_file_content_as_base64('recording.wav')
    })
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload)
    
    print(response.text)
    text = response.json()['result']['target']
    target_tts = response.json()['result']['target_tts']
    # engine.say(text)
    # engine.runAndWait()
    save_and_play_mp3(target_tts)


def get_file_content_as_base64(path, urlencoded=False):
    """
    获取文件base64编码
    :param path: 文件路径
    :param urlencoded: 是否对结果进行urlencoded 
    :return: base64编码信息
    """
    with open(path, "rb") as f:
        content = base64.b64encode(f.read()).decode("utf8")
        if urlencoded:
            content = urllib.parse.quote_plus(content)
    return content


def get_access_token():
    """
    使用 AK,SK 生成鉴权签名(Access Token)
    :return: access_token,或是None(如果错误)
    """
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    return str(requests.post(url, params=params).json().get("access_token"))
########################


# 保存 Base64编码为MP3文件 并播放
def save_and_play_mp3(target_tts_base64):
    # 解码base64  转为二进制格式
    mp3_data = base64.b64decode(target_tts_base64)
    
    # 保存mp3文件
    with open('translated_audio.mp3', 'wb') as mp3_file:
        mp3_file.write(mp3_data)
    
    # 播放mp3文件
    playsound('translated_audio.mp3')

if __name__ == '__main__':
    main()

综合任务:即时翻译助手

Python – GUI Programming (tutorialspoint.com)

import tkinter as tk

def record():
    print('开始录音...')

if __name__ == "__main__":
    root = tk.Tk()
    root.geometry("300x200")
    root.title("语音翻译器")

    button_record = tk.Button(root, text="录制声音",command=record)
    button_record.pack(padx=10, pady=50)


    root.mainloop()