基于Asterisk和TTS/ASR语音识别配置示例

狄易安
2023-12-01

智能客服的开发基于ASR和TTS的处理是一个非常关键的技术点。博主通过多年研究后发现通过软交换平台直接和一些大公司的ASR和TTS接口进行交互,通过媒体服务器的拨号规则的处理,可以实现智能客服,或者自动拨号功能。

以下是笔者看到的一个比较完整的示例,此示例支持了Asterisk,google ASR/TTS API接口。通过API调用返回的结果来实现呼叫的处理。现在和大家分享一下具体的处理流程:

首先,我们一下基于Google的语音识别的处理。首先需要安装依赖支持包:

然后把speech-recog.agi的AGI文件拷贝到 /var/lib/asterisk/agi-bin/

拷贝进去以后,处理执行权限,保证agi那个正常工作。此agi配置文件配置了API接口的调用机制。

使用语法:

agi(speech-recog.agi,[lang],[timeout],[intkey],[NOBEEP])
通过拨号规则的AGI接口调用语音识别和TTS数据:

imple speech recognition
exten => 1234,1,Answer()
exten => 1234,n,agi(speech-recog.agi,en-US) //
exten => 1234,n,Verbose(1,The text you just said is: ${utterance})
exten => 1234,n,Verbose(1,The probability to be right is: ${confidence})
exten => 1234,n,Hangup()

;;Speech recognition demo:
exten => 1235,1,Answer()
exten => 1235,n,agi(googletts.agi,“Say something in English, when done press the pound key.”,en)
exten => 1235,n(record),agi(speech-recog.agi,en-US)
exten => 1235,n,Verbose(1,Script returned: ${confidence} , ${utterance})

;Check the probability of a successful recognition:
exten => 1235,n(success),GotoIf( [ " [" ["{confidence}" > “0.8”]?playback:retry)

;Playback the text:
exten => 1235,n(playback),agi(googletts.agi,“The text you just said was…”,en)
exten => 1235,n,agi(googletts.agi,"${utterance}",en)
exten => 1235,n,goto(end)

;Retry in case speech recognition wasn’t successful:
exten => 1235,n(retry),agi(googletts.agi,“Can you please repeat more clearly?”,en)
exten => 1235,n,goto(record)

exten => 1235,n(fail),agi(googletts.agi,“Failed to get speech data.”,en)
exten => 1235,n(end),Hangup()

;;Voice dialing example
exten => 1236,1,Answer()
exten => 1236,n,agi(googletts.agi,“Please say the number you want to dial.”,en)
exten => 1236,n(record),agi(speech-recog.agi,en-US)
exten => 1236,n,GotoIf( [ " [" ["{confidence}" > “0.8”]?success:retry)

exten => 1236,n(success),goto(${utterance},1)

exten => 1236,n(retry),agi(googletts.agi,“Can you please repeat?”,en)
exten => 1236,n,goto(record)

以上是ASR的接口调用,用户也可以使用TTS调用方式。当然,首先需要创建一个tts.agi 文件,拷贝此文件到agi默认路径,执行权限设置,保证其可执行。

使用语法:

agi(googletts.agi,text,[language],[intkey])
TTS和asterisk的测试示例:

GoogleTTS Demo

exten => 1234,1,Answer()
;;Play mesage in English:
exten => 1234,n,agi(googletts.agi,“This is a simple google text to speech test in english.”,en)
;;Play message in Spanish:
exten => 1234,n,agi(googletts.agi,“Esta es una simple prueba en español.”,es)
;;Play message in Greek:
exten => 1234,n,agi(googletts.agi,“Αυτό είναι ένα απλό τέστ στα ελληνικά.”,el)
;;Play message in Japanese:
exten => 1234,n,agi(googletts.agi,“これは、日本の簡単なテストです。良い一日を。”,ja)
;;Play message in simplified Chinese:
exten => 1234,n,agi(googletts.agi,“这是一个简单的测试,在中国。有一个愉快的一天。”,zh-CN)
;A simple dynamic IVR using GoogleTTS

[my_ivr]
exten => s,1,Answer()
exten => s,n,Set(TIMEOUT(digit)=5)
exten => s,n,agi(googletts.agi,“Welcome to my small interactive voice response menu.”,en)
;;Wait for digit:
exten => s,n(start),agi(googletts.agi,“Please dial a digit.”,en,any)
exten => s,n,WaitExten()

    ;;PLayback the name of the digit and wait for another one:

exten => _X,1,agi(googletts.agi,“You just pressed ${EXTEN}. Try another one please.”,en,any)
exten => _X,n,WaitExten()

exten => i,1,agi(googletts.agi,“Invalid extension.”,en)
exten => i,n,goto(s,start)

exten => t,1,agi(googletts.agi,“Request timed out.”,en)
exten => t,n,goto(s,start)

exten => h,1,Hangup()

以上示例是一个国外开发人员的开源代码分享,笔者没有测试,因为访问Google还是有很多不方便的地方。开发人员也提供了语音合成的接口,支持微软的翻译工具来实现,读者可以进一步研究。读者可以根据ASR和TTS的接口给的大概思路,利用我们国内的ASR和TTS厂家(例如,百度,科大讯飞等)的API接口进行调整来实现ASR/TTS/IVR的流程处理。

 类似资料: