”’
!/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
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()
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']
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()
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()