コード例 #1
0
ファイル: chatrobot.py プロジェクト: yuch1a/robotQA
def task(quit_event):
    mic = Microphone(quit_event=quit_event)
    bing = BingSpeechAPI(key=BING_KEY)
    while not quit_event.is_set():
        if mic.wakeup('watson'):
            print('Wake up')
            os.system('rm temp.mp3')
            data = mic.listen()
            try:
                text = bing.recognize(data)
                if text:
                    print('\n> %s' % text)

                    if 'play music' in text:
                        tts = gTTS(text='I will play music!', lang='en-us')
                        tts.save("temp.mp3")
                        os.system('madplay temp.mp3')
                        os.system('madplay ~/Tchaikovsky_Concerto_No.1p.mp3')
                    else:
                        output = chat(text)
                        print('\n>> %s' % output)
                        tts = gTTS(text=output, lang='en-us')
                        tts.save("temp.mp3")
                        os.system('madplay temp.mp3')

            except Exception as e:
                print(e.message)
コード例 #2
0
    def __init__(self, key):
        super(Bing, self).__init__()

        self.key = key

        self.queue = queue.Queue()
        self.listening = False
        self.done = False
        self.event = threading.Event()

        self.bing = BingSpeechAPI(BING_KEY)
コード例 #3
0
def bing_init(self):
    text = ""
    bing = BingSpeechAPI(key=BING_KEY)
    try:
        fd = open(self.path)
        conect = fd.read(-1)
        text = bing.recognize(conect)
        fd.close()
        return text
    except Exception as e:
        print(e.message)
        return text
コード例 #4
0
def task(quit_event):
    mic = Microphone(quit_event=quit_event)
    bing = BingSpeechAPI(key=BING_KEY)

    while not quit_event.is_set():
        if mic.wakeup('respeaker'):
            print('Wake up')
            data = mic.listen()
            try:
                text = bing.recognize(data)
                if text:
                    print(('Recognized %s' % text))
            except Exception as e:
                print((e.message))
コード例 #5
0
def task(quit_event):
    mic = Microphone(quit_event=quit_event)
    bing = BingSpeechAPI(key=BING_KEY)

    while not quit_event.is_set():
        if mic.wakeup('respeaker'):
            print('Wake up')
            data = mic.listen()
            try:
                text = bing.recognize(data)
                if text:
                    print('Recognized %s' % text)
                    if 'play music' in text:
                        print('I will play music!')
                        os.system('madplay Tchaikovsky_Concerto_No.1p.mp3')
            except Exception as e:
                print(e.message)
コード例 #6
0
	def bing__parse_speech(self):
		bing = BingSpeechAPI(key=BING_KEY)
		try:                      
			fd = open(self.path)
			conect = fd.read(-1)
			text = bing.recognize(conect)
			fd.close()
			if text:           
				print('Recognized %s' % text)
				#f = open("out.txt", "w") 
				#print >> f, "%s " % (text)
				#f.close()
				#print(strlen)
				#print(str_doc[0:strlen])
				convert_lis = convert_to_list()
				#print("******12*********")
				find_name = fuzzyfinder(text,convert_lis)
				#print("***************")
				#print(find_name)
				#print("******1*********")
				ret_phonenumber = search_user_en(''.join(find_name).lower())
				#print("************************")
				#print(ret_phonenumber)
				if ret_phonenumber:
					global is_callflags
					is_callflags = True
					c_serial.serial_send(gfd,11,ret_phonenumber);
					print("************************")
				else:
					play_call_error_audio()
					
				return 0
				
			else:
				print('Recognized error %s' % text)
				return -1
				
		except Exception as e:               
			print(e.message)  
		
		'''
コード例 #7
0
def convert(audio_data):
    if isinstance(audio_data, types.GeneratorType):

        def generate(audio):
            yield BingSpeechAPI.get_wav_header()
            for a in audio:
                yield a

        data = generate(audio_data)
    else:
        data = BingSpeechAPI.to_wav(audio_data)
    audio = sr.AudioData(''.join(data), 16000, 2)
    return audio
コード例 #8
0
 def generate(audio):
     yield BingSpeechAPI.get_wav_header()
     for a in audio:
         yield a
コード例 #9
0
class Bing(Element):
    def __init__(self, key):
        super(Bing, self).__init__()

        self.key = key

        self.queue = queue.Queue()
        self.listening = False
        self.done = False
        self.event = threading.Event()

        self.bing = BingSpeechAPI(BING_KEY)

    def put(self, data):
        if self.listening:
            self.queue.put(data)

    def start(self):
        self.done = False
        thread = threading.Thread(target=self.run)
        thread.daemon = True
        thread.start()

    def stop(self):
        self.done = True

    def listen(self):
        self.listening = True
        self.event.set()

    def run(self):
        while not self.done:
            self.event.wait()

            def gen():
                count = 0
                while count < 16000 * 6:
                    data = self.queue.get()
                    if not data:
                        break

                    yield data
                    count += len(data) / 2

            #recognize speech using Microsoft Bing Voice Recognition
            try:
                # text = bing.recognize(gen(), language='zh-CN')
                text = self.bing.recognize(gen())
                global stringcopy
                print('Bing:{}'.format(text).encode('utf-8'))
                stringcopy = format(text).encode('utf-8')

            except ValueError:
                print('Not recognized')
                global is_music
                is_music = False

                time.sleep(1)
                #play_music('mpg123 /home/respeaker/Voice_Reception_System/notfind.mp3')
                play_music(
                    'mpg123 /home/respeaker/Voice_Reception_System/not_clear.mp3'
                )
            except RequestError as e:
                print('Network error {}'.format(e))
                time.sleep(1)
                play_music(
                    'mpg123 /home/respeaker/Voice_Reception_System/net_error.mp3'
                )

            self.listening = False
            self.event.clear()
            self.queue.queue.clear()
            global recvok, is_record
            recvok = True
            is_record = True
コード例 #10
0
from respeaker.bing_speech_api import BingSpeechAPI

from respeaker import pixel_ring

from worker import Worker
import time

try:
    from creds import BING_KEY
except ImportError:
    print(
        'Get a key from https://www.microsoft.com/cognitive-services/en-us/speech-api and create creds.py with the key'
    )
    sys.exit(-1)

bing = BingSpeechAPI(key=BING_KEY)

mic = Microphone()
player = Player(mic.pyaudio_instance)

myworker = Worker()
myworker.set_tts(bing)
myworker.set_player(player)

mission_completed = False
awake = False


def handle_int(sig, frame):
    global mission_completed
コード例 #11
0
def task(quit_event):
    mic = Microphone(quit_event=quit_event)
    bing = BingSpeechAPI(key=BING_KEY)

    while not quit_event.is_set():
        if mic.wakeup('lithnet'):
            print('Wake up')
            os.system('madplay yes-female.mp3')
            data = mic.listen()
            try:
                text = bing.recognize(data)
                if text:
                    print('Converted query from speech: %s' % text)                    
                    os.system('madplay LetmehavealookFemale.mp3')

                    try:
                        # -----------------------------
                        # Search MIM for Object 
                        # -----------------------------
                        params = {}
                        params['query'] = str(text)
                        params['staging'] = "true"
                        headers = {"Content-Type" : "application/x-www-form-urlencoded"}

                        # Connect to server to get the Access Token
                        print ("Connect to Azure Function to get Object from MIM")
                        textresp = httplib.HTTPSConnection("yourAzureFunctionApp.azurewebsites.net")
                        code = {"code" : "yourAzureFunctionKey=="}
                        functioncode = urllib.urlencode(code)
                        textresp.request("POST", "/api/yourAzureFunction?" + functioncode, json.dumps(params), headers)
                        
                        response = textresp.getresponse()
                        data = response.read()
                        print(response.status, response.reason)
                        
                        # Remove CRLF and leading "
                        data = data[:-5]      
                        data = data[1:]                  
                        print('MIM Returned: ' + data)
                        returnedResults = json.loads(data)

                        # -----------------------------
                        # Convert Repsonse to Speech
                        # -----------------------------
                        if data:
                            mimResponse = returnedResults['MIMResponse']
                            tokenurl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
                            ttskey = 'yourAzureTextToSpeechAPIKey'
                            params = ""
                            ttstokenheaders = {"Ocp-Apim-Subscription-Key": ttskey}

                            print("Getting AccessToken from westus.tts.speech.microsoft.com for Text to Speech Conversion")

                            resp = requests.post(tokenurl, params=params, headers=ttstokenheaders)
                            token = resp.text
                            accesstoken = token.decode("UTF-8")

                            ttsurl = 'https://westus.tts.speech.microsoft.com/cognitiveservices/v1'

                            ttsheaders = {}
                            ttsheaders['Ocp-Apim-Subscription-Key'] = ttskey
                            ttsheaders['Content-Type'] = "application/ssml+xml"
                            ttsheaders['X-Microsoft-OutputFormat'] = "audio-16khz-32kbitrate-mono-mp3"
                            ttsheaders['User-Agent'] = "MIMText2Speech"
                            ttsheaders['Authorization'] = "Bearer " + accesstoken

                            #<speak version='1.0' xmlns="http://www.w3.org/2001/10/synthesis" xml:lang='en-US'><voice  name='Microsoft Server Speech Text to Speech Voice (en-AU, HayleyRUS)'><prosody volume="+20.00%">Welcome to use Microsoft Cognitive Services Text-to-Speech API.</prosody></voice> </speak>
                            body = ElementTree.Element('speak', version='1.0')
                            body.set('{http://www.w3.org/XML/1998/namespace}lang', 'en-us')
                            voice = ElementTree.SubElement(body, 'voice')
                            voice.set('{http://www.w3.org/XML/1998/namespace}lang', 'en-AU')
                            voice.set('{http://www.w3.org/XML/1998/namespace}gender', 'Female')
                            voice.set('name', 'Microsoft Server Speech Text to Speech Voice (en-AU, HayleyRUS)')
                            prosody = ElementTree.SubElement(body, 'prosody')
                            prosody.set('volume', '-50.00%')
                            prosody.set('rate', '-50.00%')
                            voice.text = data

                            print("Calling westus.tts.speech.microsoft.com to convert response to audio")
                            audioresp = httplib.HTTPSConnection("westus.tts.speech.microsoft.com")
                            audioresp.request("POST", "/cognitiveservices/v1", ElementTree.tostring(body), ttsheaders)
                        
                            response = audioresp.getresponse()
                            data = response.read()
                            print(response.status, response.reason)

                            file = open("audioout.mp3", "wb")
                            file.write(data)
                            file.close()

                            # Play Response
                            os.system('madplay audioout.mp3')
                                                       
                            # -----------------------------
							# Reporting and Auditing  
							# -----------------------------                            
                            datetimenow = strftime("%m-%d-%Y %H:%M:%S", gmtime())
                            logparams = {}
                            logparams['deviceId'] = "MIMVoice"
                            logparams['messageId'] = str(datetimenow)
                            logparams['messageString'] = "MIMVoice-to-Cloud-" +str(datetimenow)
                            logparams['MIMQuery'] = str(text)
                            logparams['MIMResponse'] = mimResponse
                            logparams['entity'] = returnedResults['fullname']
                            logparams['entitlement'] = returnedResults['entitlement']
                            logparams['date'] = strftime("%m-%d-%Y", gmtime())

                            logheaders = {}
                            logheaders['Authorization'] = SASToken
                            logheaders['Content-Type'] = "application/json"

                            # Send Event to IoT Hub 
                            print ("Sending Event Summary to IoT Hub - " + IoTHubName + ".azure-devices.net from deviceID " + deviceID)
                            logresp = httplib.HTTPSConnection(IoTHubName + ".azure-devices.net")
                            logresp.request("POST", "/devices/" + deviceID + "/messages/events?api-version=" + iotHubAPIVer, json.dumps(logparams), logheaders)
                            logresponse = logresp.getresponse()
                            logdata = logresponse.read()
                            
                            if logdata:
                                print(logresponse.status, logresponse.reason)
                                logdata = logdata[:-5]
                                print("DEBUG:Event Summary send to IoT Hub failed: " + logdata)
                            
                    except Exception as e:
                        print(e.message)
                        
            except Exception as e:
                print(e.message)
コード例 #12
0
def task(quit_event):
    global ansNum
    global isStart
    global isChat
    global ans
    global vocabulary
    global score
    mic = Microphone(quit_event=quit_event)
    bing = BingSpeechAPI(key=BING_KEY)
    while not quit_event.is_set():
        if mic.wakeup('teresa'):
            print('Wake up')
            os.system('rm temp.mp3')
            data = mic.listen()
            try:
                text = bing.recognize(data)
                if text:
                    print('\n> %s' % text)

                    if (isStart == False) and ('test start' in text):
                        response = requests.get(END_POINT + '/1/vocabulary')
                        qaObj = response.json()
                        vocabulary = qaObj["question"]
                        ans = qaObj["answer"]
                        score = [4] * len(vocabulary)

                        isStart = True
                        vocNo = 0
                        tts = gTTS(text="How to spell " + vocabulary[vocNo],
                                   lang='en-us')
                        tts.save("temp.mp3")
                        os.system('madplay temp.mp3')
                    elif (isStart == False) and ('play music' in text):
                        tts = gTTS(text='I will play music!', lang='en-us')
                        tts.save("temp.mp3")
                        os.system('madplay temp.mp3')
                        os.system('madplay ~/Tchaikovsky_Concerto_No.1p.mp3')
                    elif (isStart
                          == False) and (isChat == False) and ('chat' in text):
                        isChat = True
                        tts = gTTS(text='OK! Let’s Chat !', lang='en-us')
                        tts.save("temp.mp3")
                        os.system('madplay temp.mp3')
                    elif (isStart == False) and (isChat
                                                 == True) and ('Stop Chat'
                                                               in text):
                        isChat = False
                        tts = gTTS(text='So, let’s talk again soon!',
                                   lang='en-us')
                        tts.save("temp.mp3")
                        os.system('madplay temp.mp3')
                    elif (isStart == False) and (isChat == True):
                        output = chat(text)
                        print('\n>> %s' % output)
                        tts = gTTS(text=output, lang='en-us')
                        tts.save("temp.mp3")
                        os.system('madplay temp.mp3')
                    elif isStart == False:
                        tts = gTTS(
                            text="Please say test start to start the test.",
                            lang='en-us')
                        tts.save("temp.mp3")
                        os.system('madplay temp.mp3')
                    else:
                        output = vocTest(text)
                        print('\n>> %s' % output)
                        tts = gTTS(text=output, lang='en-us')
                        tts.save("temp.mp3")
                        os.system('madplay temp.mp3')

            except Exception as e:
                print(e.message)