用IBM Bluemix-Watson API 实现语音人机交互

”’
!/usr/bin/env python 这里写代码片

# -*- coding: utf-8 -*- 
# @File : WatsonS2T.py 
# @Author: LeonYan 
# @Contact : http://blog.csdn.net/yl_1314 # @Date : 2016/9/7
 # @Desc :

from os.path import join, dirname
import os
import pyaudio
import wave
import json
import requests
from watson_developer_cloud import SpeechToTextV1
from watson_developer_cloud import TextToSpeechV1

Record the voice to output.wav file use pyaudio

def RecordVoice ():

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "./Resources/input.wav"

p = pyaudio.PyAudio()

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK)

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
    data = stream.read(CHUNK)
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

Api for Speech to text service of watson api on bluemix

def Speech2Text ():
speech_to_text = SpeechToTextV1(
username=’e008b1d6-6776-48aa-a107-e357e5396653’,
password=’SHHXDf3fkznV’,
x_watson_learning_opt_out=False
)

# print(json.dumps(speech_to_text.models(), indent=2))

# print(json.dumps(speech_to_text.get_model('en-US_BroadbandModel'), indent=2))

with open(join(dirname(__file__), './Resources/input.wav'),
          'rb') as audio_file:
   # print("speech to text result is :\n"),

    d1 = json.dumps(speech_to_text.recognize(
        audio_file, content_type='audio/wav', timestamps=True,
        word_confidence=True),
        indent=2)
    t1 = json.loads(d1)
    print t1['results'][0]['alternatives'][0]['transcript']
    return t1['results'][0]['alternatives'][0]['transcript']

Search result from Cloudant db on bluemix

def SearchInBluemix(StrRes):
StrType=””
resu=”Sorry,i can not find any result!”
if “money” in StrRes:
StrType=”money”
elif “books” in StrRes:
StrType=”books”
else:

    return resu

myurl = "https://e34abc18-91fd-438d-9501-a091cf6aa181-bluemix.cloudant.com/leondb/75375ea45fbd9dcaa5a13dd9120a7a85"
Resut = json.dumps(requests.get(myurl).json(), indent=2)
t1 = json.loads(Resut)
StrName = StrRes.split()[-1].lower()
try:
    resu=t1[StrType][StrName]
    return resu
except:
    return resu
else:
    return resu

def text_2_speech(str):
text_to_speech = TextToSpeechV1(
username=’944db649-faf1-436d-9f92-57ba85fff037’,
password=’06YVRGBVGRsH’,
x_watson_learning_opt_out=True) # Optional flag

with open(join(dirname(__file__), './Resources/output.wav'),
            'wb') as audio_file:
    audio_file.write(
        text_to_speech.synthesize(str, accept='audio/wav',
                                      voice="en-US_AllisonVoice"))

CHUNK = 1024
wf = wave.open(r'./Resources/output.wav', 'rb')
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
                    channels=wf.getnchannels(),
                    rate=wf.getframerate(),
                    output=True)

data = wf.readframes(CHUNK)

while data != '':
    stream.write(data)
    data = wf.readframes(CHUNK)
stream.stop_stream()
stream.close()
p.terminate()

Main function

def StartMain():
WelcomeStr=”Hi,What can i do for you?”
print (WelcomeStr)
text_2_speech(WelcomeStr)##Welcom words
RecordVoice()## record question content
text_2_speech(SearchInBluemix(Speech2Text()))##search coundat and fedback it by voice

str1=raw_input("Continue try again?(Y/N)").upper()#try it again?

if str1=="Y":
  # RecordVoice()
  # print SearchInBluemix(Speech2Text())
   StartMain()
elif str1 == "N":
   os._exit(0)
else:
   print ("input error,exit...")
   os._exit(0)

if name == “main“:

StartMain()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值