Example #1
0
    def _apiai_stt(self):
        from math import log
        import audioop
        import pyaudio
        import time
        resampler = apiai.Resampler(source_samplerate=settings['RATE'])
        request = self.ai.voice_request()
        vad = apiai.VAD()

        def callback(in_data, frame_count):
            frames, data = resampler.resample(in_data, frame_count)
            if settings.show_decibels:
                decibel = 20 * log(audioop.rms(data, 2) + 1, 10)
                click.echo(decibel)
            state = vad.processFrame(frames)
            request.send(data)
            state_signal = pyaudio.paContinue if state == 1 else pyaudio.paComplete
            return in_data, state_signal

        p = pyaudio.PyAudio()
        stream = p.open(format=pyaudio.paInt32, input=True, output=False, stream_callback=callback,
                        channels=settings['CHANNELS'], rate=settings['RATE'], frames_per_buffer=settings['CHUNK'])
        stream.start_stream()
        click.echo("Speak!")
        while stream.is_active():
            time.sleep(0.1)
        stream.stop_stream()
        stream.close()
        p.terminate()
Example #2
0
def main():
    global isTalking

    #Parses information from files for information
    setupCampusEvents()
    parseCampusFoodInfo()

    
    
    
    try:
        thread.start_new_thread(faceManager, ("Thread-1", 2, ) )
        #thread.start_new_thread(faceTracker,  )
        #faceTracker()
        
    except:
        print "Could not start Thread"

    while True:
       
        resampler = apiai.Resampler(source_samplerate = RATE)
        vad = apiai.VAD()
        ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN, SUBSCRIPTION_KEY)

        r = sr.Recognizer()
        speechText = ""

        #use the default microphone as the audio source
        with sr.Microphone() as source:
            #filter ambient noise               
            r.adjust_for_ambient_noise(source)        
            print "Ready!"

            #Listens for user input
            try:
                audio = r.listen(source)
            except:
                print "Try again"

        #Google Speech to Text
        try:
            speechText = r.recognize_google(audio)
            print("You said " + speechText)    
        except LookupError:                          
            print("Could not understand audio")
            continue
        except sr.UnknownValueError:
            print("Unintelligible speech.")
            continue

        #Sends text request to API.ai
        request = ai.text_request()
        request.lang = 'en'
        request.query = speechText

        #Waits for response from server and processes
        print("Wait for response...")
        response = request.getresponse()
        jsonString = (response.read()).decode('utf-8')
        processResponse(jsonString)
Example #3
0
def s2t():
    resampler = apiai.Resampler(source_samplerate=RATE)

    vad = apiai.VAD()

    ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN, SUBSCRIPTION_KEY)

    request = ai.voice_request()

    request.lang = 'en'  # optional, default value equal 'en'

    def callback(in_data, frame_count, time_info, status):
        frames, data = resampler.resample(in_data, frame_count)
        state = vad.processFrame(frames)
        request.send(data)

        if (state == 1):
            return in_data, pyaudio.paContinue
        else:
            return in_data, pyaudio.paComplete

    p = pyaudio.PyAudio()

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

    stream.start_stream()

    print("Say!")

    try:
        while stream.is_active():
            time.sleep(0.1)
    except Exception:
        raise e
    except KeyboardInterrupt:
        pass

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

    print("Wait for response...")
    response = request.getresponse()

    res = response.read()
    resDict = ast.literal_eval(res)
    print resDict["result"]['resolvedQuery']
    #print (response.read())

    return resDict["result"]['resolvedQuery']
Example #4
0
def main():
    resampler = apiai.Resampler(source_samplerate=RATE)

    vad = apiai.VAD()

    ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN)

    request = ai.voice_request()

    request.lang = 'en'  # optional, default value equal 'en'

    def callback(in_data, frame_count, time_info, status):
        frames, data = resampler.resample(in_data, frame_count)
        state = vad.processFrame(frames)
        request.send(data)

        if (state == 1):
            return in_data, pyaudio.paContinue
        else:
            return in_data, pyaudio.paComplete

    p = pyaudio.PyAudio()

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

    stream.start_stream()

    print("Say! Press enter for stop audio recording.")

    try:
        L = []
        thread.start_new_thread(input_thread, (L, ))

        while stream.is_active() and len(L) == 0:
            time.sleep(0.1)

    except Exception:
        raise
    except KeyboardInterrupt:
        pass

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

    print("Wait for response...")
    response = request.getresponse()

    print(response.read())
Example #5
0
def main():
    resampler = apiai.Resampler(source_samplerate=RATE)

    vad = apiai.VAD()

    ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN, SUBSCRIBTION_KEY)

    request = ai.voice_request()

    def callback(in_data, frame_count, time_info, status):
        frames, data = resampler.resample(in_data, frame_count)
        state = vad.processFrame(frames)
        request.send(data)

        if (state == 1):
            return in_data, pyaudio.paContinue
        else:
            return in_data, pyaudio.paComplete

    #PLACEHOLDERS FOR SPOTIFY FUNCTIONS
    def get_song(spotify_key, song_name):
        pass

    def get_playlist(spotify_key, user_ID, playlist_ID):
        pass

    def say(response):
        os.system(("echo %s |espeak" % response))

    p = pyaudio.PyAudio()

    stream = p.open(format=FORMAT,
                    channels=CHANNELS,
                    rate=RATE,
                    input=True,
                    output=False,
                    frames_per_buffer=CHUNK,
                    stream_callback=callback)
    exit is False

    print("-----------------------------------------------------------------")
    print("J.A.C.O.B - Digital Assistant Series - Mark II")
    print()
    print("Added features from MK I - Speech to Text, Spotify integration")
    print("-----------------------------------------------------------------")
    while exit is False:
        begin_speech = input("Press enter to speak. ")

        need_for_speech = False
        stream.start_stream()

        print("---------------------------!Speak!----------------------------")

        try:
            while stream.is_active():
                time.sleep(0.1)
        except Exception:
            raise e
        except KeyboardInterrupt:
            pass

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

        print("Wait for response...")
        response = request.getresponse()

        string = response.read().decode('utf-8')
        json_obj = json.loads(string)
        print(json_obj["result"]["resolvedQuery"])
        print(json_obj["result"]["fulfillment"]["speech"])
        jacob_response = json_obj["result"]["fulfillment"]["speech"]
        ###ADD SEARCH VARIABLE SO JACOB KNOWS WHEN TO SPEAK OR NAH

        ##UNTIL THEN:
        need_for_speech is True

        if need_for_speech is True:
            say(jacob_response)
Example #6
0
def main():
    resampler = apiai.Resampler(source_samplerate=RATE)
    vad = apiai.VAD()
    ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN, SUBSCRIPTION_KEY)
    request = ai.voice_request()
    request.lang = 'en'

    def stream_callback(in_data, frame_count, time_info, status):
        frames, data = resampler.resample(in_data, frame_count)
        state = vad.processFrame(frames)
        request.send(data)

        if (state == 1):
            return in_data, pyaudio.paContinue
        else:
            return in_data, pyaudio.paComplete

    p = pyaudio.PyAudio()

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

    print("Hello bro, what would you like to do ?!")
    stream.start_stream()

    try:
        while stream.is_active():
            time.sleep(0.1)
    except Exception:
        raise e
    except KeyboardInterrupt:
        pass

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

    print(
        "Give me a second bro,... Will be back with what you want right away!")
    response = request.getresponse()

    response_string = response.read().decode('utf-8')
    response_json = json.loads(response_string)

    if (response_json['result']['source'] == 'domains'
            and response_json['result']['action'] == 'web.search'):
        web_search(response_json)
    elif (response_json['result']['source'] == 'agent'
          and response_json['result']['action'] == 'browse.open'):
        webpage_open(response_json)
    elif (response_json['result']['source'] == 'agent'
          and response_json['result']['action'] == 'app.open'):
        application_open(response_json)
    elif (response_json['result']['source'] == 'agent'
          and response_json['result']['action'] == 'increase.brightness'):
        increase_screenbright(response_json)
    elif (response_json['result']['source'] == 'agent'
          and response_json['result']['action'] == 'decrease.brightness'):
        decrease_screenbright(response_json)
    elif (response_json['result']['source'] == 'agent'
          and response_json['result']['action'] == 'capture.screen'):
        screenshot(response_json)
    elif (response_json['result']['source'] == 'agent'
          and response_json['result']['action'] == 'lms.notif'):
        LMSCheck(response_json)
    elif (response_json['result']['source'] == 'agent'
          and response_json['result']['action'] == 'set.volume'):
        set_volume(response_json)
    elif (response_json['result']['source'] == 'agent'
          and response_json['result']['action'] == 'mute.volume'):
        mute(response_json)
    elif (response_json['result']['source'] == 'agent'
          and response_json['result']['action'] == 'unmute.volume'):
        unmute(response_json)
    elif (response_json['result']['source'] == 'agent'
          and response_json['result']['action'] == 'increase.volume'):
        increase_volume(response_json)
    elif (response_json['result']['source'] == 'agent'
          and response_json['result']['action'] == 'decrease.volume'):
        decrease_volume(response_json)
Example #7
0
def runSpeechQuery():
    try:
        import apiai
    except ImportError:
        sys.path.append(
            os.path.join(os.path.dirname(os.path.realpath(__file__)),
                         os.pardir))
        import apiai

    import pyaudio
    import time
    import json
    import os

    CHUNK = 512
    FORMAT = pyaudio.paInt16
    CHANNELS = 1
    RATE = 44100
    RECORD_SECONDS = 2

    CLIENT_ACCESS_TOKEN = '03848b1f5f2f48ef8a8fdc062674afa2'
    SUBSCRIBTION_KEY = '3cd524f0-efff-4ebb-9653-b9eb7bbda1cb'

    resampler = apiai.Resampler(source_samplerate=RATE)

    vad = apiai.VAD()

    ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN, SUBSCRIBTION_KEY)

    request = ai.voice_request()

    request.lang = 'en'  # optional, default value equal 'en'

    def callback(in_data, frame_count, time_info, status):
        frames, data = resampler.resample(in_data, frame_count)
        state = vad.processFrame(frames)
        request.send(data)

        if (state == 1):
            return in_data, pyaudio.paContinue
        else:
            return in_data, pyaudio.paComplete

    def say(response):
        os.system(("echo %s |espeak" % response))

    p = pyaudio.PyAudio()

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

    stream.start_stream()

    print("Say!")

    try:
        while stream.is_active():
            time.sleep(0.1)
    except Exception:
        raise e
    except KeyboardInterrupt:
        pass

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

    print("Wait for response...")
    response = request.getresponse()

    string = response.read().decode('utf-8')
    json_obj = json.loads(string)
    print(json_obj["result"]["resolvedQuery"])
    print(json_obj["result"]["fulfillment"]["speech"])
    jacob_response = json_obj["result"]["fulfillment"]["speech"]

    say(jacob_response)
Example #8
0
def main():
    def callback(in_data, frame_count, time_info, status):
        frames, data = resampler.resample(in_data, frame_count)
        state = vad.processFrame(frames)
        request.send(data)

        if (state == 1):
            return in_data, pyaudio.paContinue
        else:
            return in_data, pyaudio.paComplete

    ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN, SUBSCRIPTION_KEY)
    os.system("say -v Karen \"Hi, I'm Karen. How can I help? \"")

    while True:

        if DEBUG:  # text input
            request = ai.text_request()
            request.query = raw_input("How can I help? ")

        else:  # voice input
            resampler = apiai.Resampler(source_samplerate=RATE)
            vad = apiai.VAD()

            request = ai.voice_request()
            p = pyaudio.PyAudio()
            stream = p.open(format=FORMAT,
                            channels=CHANNELS,
                            rate=RATE,
                            input=True,
                            output=False,
                            frames_per_buffer=CHUNK,
                            stream_callback=callback)
            stream.start_stream()

            print("Say!")

            try:
                while stream.is_active():
                    time.sleep(0.1)
            except Exception:
                os.system("say Sorry can you repeat that?")
                raise e
            except KeyboardInterrupt:
                pass

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

        print("Wait for response...")
        response = request.getresponse()

        s = response.read()
        print s
        json_obj = json.loads(s)

        message = ""

        try:
            intent = json_obj['result']['metadata']['intentName']
            params = json_obj['result']['parameters']

            if intent == 'customer-lookup':
                first_name = params['customer-name']['first-name']
                last_name = params['customer-name']['last-name']
                customer_info = params['customer-info']
                r = requests.get("%s/customers/search.json?query=%s+%s" %
                                 (BASE_URL, first_name, last_name))
                customer_data = json.loads(r.content)['customers'][0]

                if customer_info == 'address':
                    answer = customer_data['default_address']['address1']
                elif customer_info == 'city':
                    answer = customer_data['default_address']['city']
                elif customer_info == 'phone-number':
                    answer = str(customer_data['default_address']['phone'])
                elif customer_info == 'country':
                    answer = customer_data['default_address']['country']
                elif customer_info == 'order-total':
                    answer = str(customer_data['total_spent']) + 'dollars'
                elif customer_info == 'email':
                    answer = customer_data['email']

                message = "The %s for %s %s is %s" % (
                    customer_info, first_name, last_name, answer)

            elif intent == 'order-information':
                number = params['order-number']

                if number is not None:
                    r = requests.get("%s/orders/%d.json" % (BASE_URL, number))

                # WIP

            elif intent == 'modify-order':
                # TBD: Allow user to provide order 'name' instead of number
                number = int(params['order-number'])
                action = params['order-action']

                if number is not None:
                    prefix = "%s/orders/%d" % (BASE_URL, number)

                    if action == 'cancel' or action == 'close':
                        r = requests.post('%s/%s.json' % (prefix, action),
                                          data={})

                        if r.status_code == 200:
                            if action == 'cancel':
                                message = "Cancelled order number %d" % number
                            elif action == 'close':
                                message = "Closed order number %d" % number
                        else:
                            message = "Couldn't perform that action on order %d" % number

                    elif action == 'delete':
                        r = requests.delete('%s.json' % prefix)

                        if r.status_code == 200:
                            message = "Deleted order %d" % number
                        else:
                            message = "Couldn't delete order %d" % number

                    elif action == 'note':
                        note = "This note is a test!"
                        payload = {"order": {"note": note}}
                        r = requests.put('%s.json' % prefix, json=payload)

                        if r.status_code == 200:
                            message = "Added note %s to order %s" % (note,
                                                                     number)
                        else:
                            message = "I wasn't able to find that order."

                    # elif action == 'fulfill':
                    # WIP

                    # elif action == 'create':
                    # WIP

            elif intent == 'fulfillment-count':
                state = params['fulfillment-state']

                if state is not None:
                    r = requests.get(
                        "%s/orders/count.json?fulfillment_status=%s" %
                        (BASE_URL, state))
                    count = json.loads(r.content)['count']

                    if state == 'shipped':
                        message = "%d orders have been shipped" % count
                    elif state == 'unshipped':
                        message = "%d orders have not been shipped" % count
                    else:
                        message = "%d orders have %s status" % (count, action)

            elif intent == 'order-history-period':
                period = params['date-period']

                start_date = period.split('/')[0].replace(
                    '/', '-') + "T00:00:00-05:00"
                end_date = period.split('/')[1].replace(
                    '/', '-') + "T23:59:59-05:00"

                r = requests.get(
                    "%s/orders/count.json?created_at_min=%s&created_at_max=%s"
                    % (BASE_URL, start_date, end_date))
                count = json.loads(r.content)['count']

                message = "There were %d orders during that period." % count

            elif intent == 'order-history-date':
                date = params['date']
                request_date_min = date + "T00:00:00-05:00"
                request_date_max = date + "T23:59:59-05:00"

                r = requests.get(
                    "%s/orders/count.json?created_at_min=%s&created_at_max=%s"
                    % (BASE_URL, request_date_min, request_date_max))
                count = json.loads(r.content)['count']

                message = "There were %d orders on that day." % count

            elif intent == 'product-information':
                product = params['product']
                r = requests.get("%s/products.json?title=%s" %
                                 (BASE_URL, product))
                try:
                    prod_inventory = json.loads(
                        r.content
                    )['products'][0]['variants'][0]['inventory_quantity']
                    message = "You have %s %s remaining in inventory" % (
                        prod_inventory, product)
                except:
                    message = "Could not find information for %s" % product

            else:
                print "Intent not matched."

        except KeyError:
            message = "I don\'t know. Ask David Lennie."

        print message
        os.system("say -v karen \"%s. Is there anything else?\"" % message)

        time.sleep(0.5)
CLIENT_ACCESS_TOKEN = 'c4c14424cdd5488f9183de8856cd83ea'
SUBSCRIPTION_KEY = 'c2a6c4eb-2961-4658-be52-fc75822d2fa7'


def callback(in_data, frame_count, time_info, status):
    frames, data = resampler.resample(in_data, frame_count)
    state = vad.processFrame(frames)
    request.send(data)
    if (state == 1):
        return in_data, pyaudio.paContinue
    else:
        return in_data, pyaudio.paComplete


ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN, SUBSCRIPTION_KEY)
resampler = apiai.Resampler(source_samplerate=RATE)
vad = apiai.VAD()
request = ai.voice_request()
request.lang = 'es'

p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=False,
                frames_per_buffer=CHUNK,
                stream_callback=callback)

print "Listening..."
stream.start_stream()
Example #10
0
def main():
    resampler = apiai.Resampler(source_samplerate=RATE)

    vad = apiai.VAD()

    ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN)

    request = ai.voice_request()

    request.lang = 'en'  # optional, default value equal 'en'

    def callback(in_data, frame_count, time_info, status):
        frames, data = resampler.resample(in_data, frame_count)
        state = vad.processFrame(frames)
        request.send(data)

        if (state == 1):
            return in_data, pyaudio.paContinue
        else:
            return in_data, pyaudio.paComplete

    p = pyaudio.PyAudio()

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

    stream.start_stream()

    print("Say! Press enter for stop audio recording.")

    try:
        L = []
        _thread.start_new_thread(input_thread, (L, ))

        while stream.is_active() and len(L) == 0:
            time.sleep(0.1)

    except Exception:
        raise
    except KeyboardInterrupt:
        pass

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

    print("Wait for response...")
    response = request.getresponse()
    jsonAll = response.read().decode("utf-8")
    print(jsonAll)
    if (jsonAll.count("action") >= 1):
        actionStart = jsonAll.index("action") + 10
        actionEnd = jsonAll.index("actionIn") - 8
        action = jsonAll[actionStart:actionEnd]
    else:
        action = " "
    print(action)
    if (jsonAll.count("displayText") >= 1):
        speechStart = jsonAll.index(
            "displayText") + 13  #fix to say all of sppech and not range
        speechEnd = jsonAll.index('"score"') - 9
        speech = jsonAll[speechStart:speechEnd]
    else:
        speechStart = jsonAll.index(
            "speech") + 10  #fix to say all of sppech and not range
        speechEnd = jsonAll.index('"score"') - 9
        speech = jsonAll[speechStart:speechEnd]
    print(speech)
    if (action == "give_joke"):
        tellTheJoke()
    elif (action == "yahooWeatherForecast"):
        tellTheWeather(speech)
    else:
        print("excuse me")