語音識別,文本轉語音,語音轉文本

 

1. 語音合成.
	百度AipSpeech:
	synthesis():
		1: 與合成語音的文本.
		2. 語言.
		3. 客戶端類型,1 
		4.option{語音合成參數}: pit 聲調  spd 語速 vol 音量 per cosplay 合成語音角色.
		
2. 語音識別
	百度 AipSpeech
		 asr():
			1. 音頻文件流 
			2. 音頻文件格式(pcm)
			3. 音頻採樣率(16000, 8k/16k)
			4. option{識別語種}: dev_pid :1537  包含簡單英文的普通話識別. 
		
		錄音文件基本上沒有直接pcm格式,須要對音頻文件進行轉換.
		ffmpeg:FFmpeg公司.涉及audio 處理的基本上都用它了.
		經過os.system(FFmpeg_cmdstr)獲得新的pcm文件.
		

 

1.  百度註冊帳號.

 

案例1 .文本轉語音

from aip import AipSpeech
from aip import AipNlp
from uuid import uuid4
import setting
import os

APP_ID = "11562884"
API_KEY = "9iOLKP9VCo4nsEf3N8dcOUmT"
SECRET_KEY = "aW0kwOHFbHrQely6bcmGTzU49t2jOYdL"

baidu_client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
baidu_nlp = AipNlp(APP_ID, API_KEY, SECRET_KEY)

#.1 文本轉語音.
def text2audio(text):

    file_name = f"{uuid4()}.mp3"
    print("filename:%s"%file_name)
    file_path = os.path.join(setting.AUDIO_FILE_PATH, file_name)
    print("file_path:%s" %(file_path))
    res = baidu_client.synthesis(text, "zh", 1, {
        "vol": 5,
        "pit": 7,
        "spd": 4,
        "per": 4
    })

    if type(res) == dict:
        return res

    with open(file_path, "wb") as f:
        f.write(res)

    return file_name

print(text2audio("強哥威武"))

打印結果:

filename:86375f56-f42d-447a-9bcc-829d370ff805.mp3
file_path:audio\86375f56-f42d-447a-9bcc-829d370ff805.mp3
86375f56-f42d-447a-9bcc-829d370ff805.mp3html

 

案例2.語音轉文本.

def audio2text(file_name):
    file_pcm_path =os.path.join(setting.AUDIO_FILE_PATH,file_name)
    cmd_str =f"ffmpeg -y -i {file_pcm_path} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {file_pcm_path}.pcm"
    os.system(cmd_str)

    with open(f"{file_pcm_path}","rb") as f :
        audio_context =f.read()

    res =baidu_client.asr(audio_context,"pcm",16000,{"dev_pid": 1537})

    if res.get('err_no'):
        return res

    return res.get("result")[0]

print(audio2text("qiang.wma"))

 

案例3. 語音識別 NLP 

#.3 語音識別
def my_nlp(text):
    if baidu_nlp.simnet("你今年幾歲了 ",text).get("score")>=0.72:
        print(baidu_nlp.simnet("你今年幾歲了 ",text).get("score"))
        return "我今年73歲了,否則84歲也行"

    if baidu_nlp.simnet("你叫什麼名字",text).get("score")>=0.72:
        return "個人名字叫作小嘿嘿"

    if baidu_nlp.simnet("你在哪裏學習",text).get("score")>=0.72:
        return "我在學習python"


print(my_nlp('你多大了'))  #結果:我今年73歲了,否則84歲也行

print(my_nlp("你的名字是")) #結果:個人名字叫作小嘿嘿

  

代碼總結 :

from aip import AipSpeech
from aip import AipNlp
from uuid import uuid4
import setting
import os

APP_ID = "11562884"
API_KEY = "9iOLKP9VCo4nsEf3N8dcOUmT"
SECRET_KEY = "aW0kwOHFbHrQely6bcmGTzU49t2jOYdL"

baidu_client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
baidu_nlp = AipNlp(APP_ID, API_KEY, SECRET_KEY)

#.1 文本轉語音.
def text2audio(text):

    file_name = f"{uuid4()}.mp3"
    print("filename:%s"%file_name)
    file_path = os.path.join(setting.AUDIO_FILE_PATH, file_name)
    print("file_path:%s" %(file_path))
    res = baidu_client.synthesis(text, "zh", 1, {
        "vol": 5,
        "pit": 7,
        "spd": 4,
        "per": 4
    })

    if type(res) == dict:
        return res

    with open(file_path, "wb") as f:
        f.write(res)

    return file_name
#
# print(text2audio("強哥威武"))



#.2 語音轉文本.
def audio2text(file_name):
    file_pcm_path =os.path.join(setting.AUDIO_FILE_PATH,file_name)
    cmd_str =f"ffmpeg -y -i {file_pcm_path} -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {file_pcm_path}.pcm"
    os.system(cmd_str)

    with open(f"{file_pcm_path}","rb") as f :
        audio_context =f.read()

    res =baidu_client.asr(audio_context,"pcm",16000,{"dev_pid": 1537})

    if res.get('err_no'):
        return res

    return res.get("result")[0]
#
print(audio2text("qiang.wma"))


#.3 語音識別
def my_nlp(text):
    if baidu_nlp.simnet("你今年幾歲了 ",text).get("score")>=0.72:
        print(baidu_nlp.simnet("你今年幾歲了 ",text).get("score"))
        return "我今年73歲了,否則84歲也行"

    if baidu_nlp.simnet("你叫什麼名字",text).get("score")>=0.72:
        return "個人名字叫作小嘿嘿"

    if baidu_nlp.simnet("你在哪裏學習",text).get("score")>=0.72:
        return "我在學習python"


print(my_nlp('你多大了'))  #結果:我今年73歲了,否則84歲也行

print(my_nlp("你的名字是")) #結果:個人名字叫作小嘿嘿
View Code

 

 

send_file 與audio標籤.

py文件python

from  flask  import Flask,request,send_file
import setting
import os

app =Flask(__name__)



@app.route("/getfile/<file_name>")
def get_file(file_name):

    audio_file =os.path.join(setting.AUDIO_FILE_PATH,file_name)

    return send_file(audio_file)


if __name__ =="__main__":
    app.run()

 

html文件flask

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<audio autoplay="autoplay" id="play_mp3" src="http://127.0.0.1:1111/getfile/7b86f60d-6f43-4342-9948-38657de4843e.mp3"></audio>
</body>
</html>

 

相關文章
相關標籤/搜索