Example #1
0
def main():
    import signal

    thread = None
    #if not get_refresh_token():
    #    thread = Thread(target=login)
    #    thread.daemon = True
    #    thread.start()

    if not get_refresh_token():
        thread = Thread(target=get_token_loop)
        thread.daemon = True
        thread.start()

    quit_event = Event()

    def handler(signum, frame):
        quit_event.set()
        if thread:
            tornado.ioloop.IOLoop.instance().stop()

    signal.signal(signal.SIGINT, handler)

    get_refresh_token()

    #mosq_command='/usr/bin/mosquitto_sub -h 120.27.138.117 -t umomoSofa -v &'
    mosq_command = '/usr/bin/mosquitto_sub'
    subprocess.Popen(mosq_command, shell=True)
    mic_command = '/usr/bin/mic_wake'
    subprocess.Popen(mic_command, shell=True)

    mic = Microphone(quit_event=quit_event)
    alexa = Alexa()
    print("========================alexa start========================")
    while not quit_event.is_set():
        with open('/etc/config/mic_wake') as f:
            contents = f.read()
        #if mic.wakeup(keyword='alexa'):
        if "1" in contents:
            logging.debug('wakeup')
            if not get_refresh_token():
                if platform.machine() == 'mips':
                    command = 'madplay -o wave:- {} | aplay -M'.format(
                        hint_file)
                else:
                    command = 'ffplay -autoexit -nodisp {}'.format(hint_file)

                subprocess.Popen(command, shell=True).wait()
                continue

            data = mic.listen()
            print(
                "========================alexa listen========================")
            try:
                alexa.recognize(data)
            except Exception as e:
                logging.warn(e.message)

    mic.close()
    logging.debug('Mission completed')
Example #2
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('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)
Example #3
0
def task(quit_event):
    mic = Microphone(quit_event=quit_event)
    player = Player(mic.pyaudio_instance)

    pixel_ring.set_color(rgb=0x505000)
    time.sleep(3)

    speech = Speech()
    myBot = Bot()

    while not quit_event.is_set():
        if mic.wakeup(keyword='olaf'):
            pixel_ring.listen()
            data = mic.listen()
            pixel_ring.wait()
            text = speech.recognize(data)
            if text:
                logger.debug('Recognized : %s', text)
                result = myBot.request(text)
                pixel_ring.speak(4, 0)
                audio = speech.synthetize(result)

                if (audio != None):
                    player.play_raw(audio)

            pixel_ring.off()

    mic.close()
    pixel_ring.off()
Example #4
0
def task(quit_event):
    mic = Microphone(quit_event=quit_event)

    while not quit_event.is_set():
        if mic.wakeup('respeaker'):
            print('Wake up')
            data = mic.listen()
            text = mic.recognize(data)
            if text:
                print(('Recognized %s' % text))
Example #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))
            except Exception as e:
                print((e.message))
def task(quit_event):
    mic = Microphone(quit_event=quit_event)
    r = sr.Recognizer()

    while not quit_event.is_set():
        if mic.wakeup('respeaker'):
            print('Wake up')
            data = mic.listen()
            try:
                text = r.recognize_google(convert(data), language='en-US')
                if text:
                    print('Recognized %s' % text)
            except Exception as e:
                print(e.message)
Example #7
0
def task(quit_event):
    mic = Microphone(quit_event=quit_event)
    bing = BingSpeechAPI(key=BING_KEY)

    while not quit_event.is_set():
        print("\nListening ... ")
        data = mic.listen()
        try:
            print("\nSending to buzzword AI ML MODEL")
            text = bing.recognize(data)
            if text:
                print("\nWriting {}".format(text))
                print_to_display(text)
        except Exception as e:
            print(e.message)
def task(quit_event):
    mic = Microphone(quit_event=quit_event)
    baidu = BaiduVoiceApi(appkey=APP_KEY, secretkey=SECRET_KEY)

    while not quit_event.is_set():
        if mic.wakeup('respeaker'):
            print('Wake up')
            data = mic.listen()
            try:
                text = baidu.server_api(data)
                if text:
                    text = json.loads(text)
                    print('Recognized %s' % text['result'][0])
            except Exception as e:
                print(e.message)
Example #9
0
def main():
    global mic, quit_event

    pa = pyaudio.PyAudio()
    mic = Microphone(pa)

    while not quit_event.is_set():
        if mic.detect(keyword='alexa'):
            print('wakeup')
            data = mic.listen()
            data = b''.join(data)
            if data:
                alexa(data)

    mic.close()
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)
Example #11
0
def task(quit_event):
    mic = Microphone(quit_event=quit_event)
    bing = BingSpeechAPI(BING_KEY)

    while not quit_event.is_set():
        if mic.wakeup('respeaker'):
            print('Wake up')
            pixel_ring.listen()
            robot("WakeUp")

            time.sleep(.1)
            data = mic.listen()

            try:
                pixel_ring.wait()
                text = bing.recognize(data, language='en-US')
                # spi.write('answer\n')
                print('\nBing:' + text.encode('utf-8'))
                if re.search(r'shake', text) and re.search(r'left hand', text):
                    robot("LeftHand")
                    print("Shake Left hand")
                elif re.search(r'shake', text) and re.search(
                        r'right hand', text):
                    robot("RightHand")
                    print("Shake right hand")
                elif re.search(r'shake.*(head).*', text):
                    robot("ShakeHead")
                    print("Shake head")
                elif re.search(r'head', text) or re.search(r'had', text):
                    robot("NodHead")
                    print("Nod head")
                elif re.search(r'hand', text):
                    robot("ShakeHand")
                    print("Shake hand")
                elif re.search(r'hello', text):
                    robot("RightHand")
                    print("Hello")
                else:
                    print("Other")
            except Exception as e:
                print(
                    "\nCould not request results from Microsoft Bing Voice Recognition service; {0}"
                    .format(e))
            # if text:
            # print('Recognized %s' % text)
            pixel_ring.off()
Example #12
0
def main():
    import signal

    thread = None
    if not get_refresh_token():
        thread = Thread(target=login)
        thread.daemon = True
        thread.start()

    quit_event = Event()

    def handler(signum, frame):
        quit_event.set()
        if thread:
            tornado.ioloop.IOLoop.instance().stop()

    signal.signal(signal.SIGINT, handler)

    mic = Microphone(quit_event=quit_event)
    alexa = Alexa()

    while not quit_event.is_set():
        if mic.wakeup(keyword='alexa'):
            logging.debug('wakeup')
            if not get_refresh_token():
                if platform.machine() == 'mips':
                    command = 'madplay -o wave:- {} | aplay -M'.format(
                        hint_file)
                else:
                    command = 'ffplay -autoexit -nodisp {}'.format(hint_file)

                subprocess.Popen(command, shell=True).wait()
                continue

            data = mic.listen()
            try:
                alexa.recognize(data)
            except Exception as e:
                logging.warn(e.message)

    mic.close()
    logging.debug('Mission completed')
Example #13
0
def main():
    import signal

    thread = None
    if not get_refresh_token():
        thread = Thread(target=login)
        thread.daemon = True
        thread.start()

    quit_event = Event()

    def handler(signum, frame):
        quit_event.set()
        if thread:
            tornado.ioloop.IOLoop.instance().stop()

    signal.signal(signal.SIGINT, handler)

    mic = Microphone(quit_event=quit_event)
    alexa = Alexa()

    while not quit_event.is_set():
        if mic.wakeup(keyword='alexa'):
            logging.debug('wakeup')
            if not get_refresh_token():
                if platform.machine() == 'mips':
                    command = 'madplay -o wave:- {} | aplay -M'.format(hint_file)
                else:
                    command = 'ffplay -autoexit -nodisp {}'.format(hint_file)

                subprocess.Popen(command, shell=True).wait()
                continue

            data = mic.listen()
            try:
                alexa.recognize(data)
            except Exception as e:
                logging.warn(e.message)

    mic.close()
    logging.debug('Mission completed')
Example #14
0
def main():
    quit_event = Event()
    mic = Microphone(quit_event=quit_event)
    alexa = Alexa(mic)

    def on_quit(signum, frame):
        quit_event.set()

    signal.signal(signal.SIGINT, on_quit)

    while not quit_event.is_set():
        if mic.wakeup(keyword='alexa'):
            logging.debug('wakeup')
            data = mic.listen()
            try:
                alexa.recognize(data)
            except Exception as e:
                logging.warn(e.message)

    mic.close()
    logging.debug('Mission completed')
Example #15
0
    def run_listening(self):
        mic = Microphone()

        try:
            while 1:
                print("Listening\n")
                was_learning = self.learning
                data = mic.listen(LEN_AUDIO, 1)  #make recordings of one second
                data = b''.join(data)

                if was_learning:
                    self.learning_recording += data

                    if not self.learning:
                        self.end_learning_event.set()
                else:
                    _thread.start_new_thread(self.process_audio, (data, ))

                    if self.learning:
                        self.end_learning_event.clear()

        except KeyboardInterrupt:
            print("Quit")
Example #16
0
class Audio(object):
    def __init__(self):
        from respeaker import Microphone

        self.mic = Microphone()

    def wakeup(self):
        return self.mic.wakeup('alexa')

    def start(self):
        pass

    def __iter__(self):
        return self.mic.listen()

    def stop(self):
        pass

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        pass
Example #17
0
def task(quit_event):
    mic = Microphone(quit_event=quit_event)
    while not quit_event.is_set():
        pixels.off()
        print("Waiting for wakeup word!")
        if mic.wakeup(['sen ơi','senoi','maioi','mai ơi']):                        
            print('waked up')
            f_wav=random.randint(1,8)
            mic.stop()
            os.system('aplay /dev/shm/waves/' + str(f_wav)+'.wav')           
            print("Speaking something ...")
            data=mic.listen(duration=6, timeout=1.5)
            mic.stop()
            out=json.loads(send_raw(data))
            trans=out['hypotheses'][0]['utterance'].encode('utf-8')
            print('Recognized output: ' + trans)
            if len(trans)>0:
                try:
                    jmsg=json.loads(msg)
                    jmsg['texts']=trans
                    playtts(jmsg)
                except:
                    print("TTS has some problem")
def task(quit_event):
    mic = Microphone(quit_event=quit_event)
    bing = BingSpeechAPI(key=BING_KEY)
 
    while not quit_event.is_set():
            data = mic.listen()
            try:
                text = bing.recognize(data)
                if text:
                    print('\n\nRecognized : %s\n' % text)
                    qty = getQuantityFromString(text)
                    print("Quantite: " + qty)
                if 'avance' in text:
                    API_ENDPOINT = HOST+"/movement/forward/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'recule' in text:
                    API_ENDPOINT = HOST+"/movement/backward/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'rampe à gauche'.decode('utf-8') in text:
                    API_ENDPOINT = HOST+"/movement/left/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'rampe à droite'.decode('utf-8') in text:
                    API_ENDPOINT = HOST+"/movement/right/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'tourne à droite'.decode('utf-8') in text:
                    API_ENDPOINT = HOST+"/movement/turn/right/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'tourne à gauche'.decode('utf-8') in text:
                    API_ENDPOINT = HOST+"/movement/turn/left/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'éteins-toi'.decode('utf-8') in text:
                    API_ENDPOINT = HOST+"/action/standby"
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'twist' in text:
                    API_ENDPOINT = HOST+"/movement/twist/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'allume toi' in text:
                    API_ENDPOINT = HOST+"/action/wakeup"
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'calibre toi' in text:
                    API_ENDPOINT = HOST+"/action/calibrate"
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'rampe' in text:
                    API_ENDPOINT = HOST+"/movement/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'changes ta hauteur' in text:
                    API_ENDPOINT = HOST+"/height/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'bouge ton corps' in text:
                    API_ENDPOINT = HOST+"/move/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'tourne toi' in text:
                    API_ENDPOINT = HOST+"/rotate/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
                elif 'dance' in text:
                    API_ENDPOINT = HOST+"/dance/" + qty
                    res = r.get(API_ENDPOINT)
                    print(res)
            except Exception as e:
                print(e.message)
Example #19
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)
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)
Example #21
0
    pixel_ring.off()
    mission_completed = True
    mic.close()
    player.close()
    myworker.stop()


signal.signal(signal.SIGINT, handle_int)

myworker.start()

while not mission_completed:
    print
    print "*********** wake me up with \"respeaker\" ***************"
    if mic.wakeup('respeaker'):
        data = mic.listen()
        time.sleep(0.5)
        if data:
            try:
                pixel_ring.wait()
                text = bing.recognize(data, language='en-US')
                print
                print('BING recognize:', text.encode('utf-8'))
                myworker.push_cmd(text)
                myworker.wait_done()
            except Exception as e:
                print(e.message)
        pixel_ring.off()

time.sleep(2)