1. 程式人生 > >語音合成和語音識別

語音合成和語音識別

1、語音合成

    預先準備工作:

    安裝所需的依賴包

    1:pip install baidu-aip 

    2:百度雲建立語音合成專案,https://ai.baidu.com/

  

 1 from aip import AipSpeech
 2 
 3 """ 你的 APPID AK SK """
 4 APP_ID = '14940739'
 5 API_KEY = 'xCnr5K8ESsmOVaA5bl5ot5QY'
 6 SECRET_KEY = '4wH7W92hPUp8V7ogY4BZzV2pcZ3nC8LH '
 7 
 8
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) 9 10 11 result = client.synthesis('你好百度', 'zh', 1, { 12 'vol': 5, 13 'per':5 14 }) 15 16 # 識別正確返回語音二進位制 錯誤則返回dict 參照下面錯誤碼 17 if not isinstance(result, dict): 18 with open('auido.mp3', 'wb') as f: 19 f.write(result)
程式碼例項

 

2、語音識別

  預先準備工作:

  1安裝轉碼工具  

  1.FFmpeg:

  連結:https://pan.baidu.com/s/1jonSAa_TG2XuaJEy3iTmHg

  密碼:w6hk

   轉碼命令:ffmpeg -y  -i audio.wav  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 audio.pcm

 1 from aip import AipSpeech
 2 import  os
 3 """ 你的 APPID AK SK """
 4 APP_ID = '
14940739' 5 API_KEY = 'xCnr5K8ESsmOVaA5bl5ot5QY' 6 SECRET_KEY = '4wH7W92hPUp8V7ogY4BZzV2pcZ3nC8LH ' 7 8 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) 9 10 # contemp=f"ffmpeg -y -i audio.wav -acodec pcm_s16le -f s16le -ac 1 -ar 16000 audio.pcm" 11 12 # 讀取檔案 13 def get_file_content(filePath): 14 os.system(f"ffmpeg -y -i {filePath} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filePath}.pcm") 15 with open(f"{filePath}.pcm", 'rb') as fp: 16 return fp.read() 17 18 # 識別本地檔案 19 res=client.asr(get_file_content('auido.mp3'), 'pcm', 16000, { 20 'dev_pid': 1536, 21 }) 22 print(res.get("result")[0])
程式碼例項

 

3、語音對話

 1 from aip import AipSpeech
 2 import os
 3 """ 你的 APPID AK SK """
 4 APP_ID = '14940739'
 5 API_KEY = 'xCnr5K8ESsmOVaA5bl5ot5QY'
 6 SECRET_KEY = '4wH7W92hPUp8V7ogY4BZzV2pcZ3nC8LH '
 7 
 8 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
 9 
10 def get_file_content(filename):
11     # os.system(f"ffmpeg -y  -i {filename}  -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filename}.pcm")
12     # with open(f"{filename}.pcm", 'rb') as fp:
13     with open("sd.pcm", 'rb') as fp:
14         res = client.asr(fp.read(), 'pcm', 16000, {
15             'dev_pid': 1536,
16         })
17         return  res.get("result")[0]
18 
19 def  synthesis(text):
20     result = client.synthesis(text, 'zh', 1, {
21         "spd": 4,
22         'vol': 5,
23         "pit": 8,
24         "per": 4
25     })
26 
27     # 識別正確返回語音二進位制 錯誤則返回dict 參照下面錯誤碼
28     if not isinstance(result, dict):
29         with open('auido.mp3', 'wb') as f:
30             f.write(result)
31     os.system("auido.mp3")
32 
33 text=get_file_content("auido.mp3")
34 if "傻屌" in text:
35     synthesis("我挺好的")
36 else:
37     synthesis(f"你剛才是不是說,{text}")
程式碼例項