用IBM Bluemix-Watson API 實現語音人機互動_api

來源:互聯網
上載者:User

”’
!/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 = 1024FORMAT = pyaudio.paInt16CHANNELS = 2RATE = 44100RECORD_SECONDS = 5WAVE_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 resumyurl = "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 resuexcept:    return resuelse:    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 = 1024wf = 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()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.