diff --git a/examples/Python/Hey_Watson_PI.pmdl b/examples/Python/Hey_Watson_PI.pmdl new file mode 100644 index 00000000..bfeb168d Binary files /dev/null and b/examples/Python/Hey_Watson_PI.pmdl differ diff --git a/examples/Python/demo4.py b/examples/Python/demo4.py index ac946e0a..89ea0ce4 100644 --- a/examples/Python/demo4.py +++ b/examples/Python/demo4.py @@ -19,7 +19,7 @@ def audioRecorderCallback(fname): - print "converting audio to text" + print("converting audio to text") r = sr.Recognizer() with sr.AudioFile(fname) as source: audio = r.record(source) # read the entire audio file @@ -30,9 +30,9 @@ def audioRecorderCallback(fname): # instead of `r.recognize_google(audio)` print(r.recognize_google(audio)) except sr.UnknownValueError: - print "Google Speech Recognition could not understand audio" + print("Google Speech Recognition could not understand audio") except sr.RequestError as e: - print "Could not request results from Google Speech Recognition service; {0}".format(e) + print("Could not request results from Google Speech Recognition service; {0}".format(e)) os.remove(fname) @@ -52,8 +52,8 @@ def interrupt_callback(): return interrupted if len(sys.argv) == 1: - print "Error: need to specify model name" - print "Usage: python demo.py your.model" + print("Error: need to specify model name") + print("Usage: python demo.py your.model") sys.exit(-1) model = sys.argv[1] @@ -62,7 +62,7 @@ def interrupt_callback(): signal.signal(signal.SIGINT, signal_handler) detector = snowboydecoder.HotwordDetector(model, sensitivity=0.38) -print "Listening... Press Ctrl+C to exit" +print("Listening... Press Ctrl+C to exit") # main loop detector.start(detected_callback=detectedCallback, diff --git a/examples/Python/demo_threaded.py b/examples/Python/demo_threaded.py index ded146c7..2e202922 100644 --- a/examples/Python/demo_threaded.py +++ b/examples/Python/demo_threaded.py @@ -3,6 +3,11 @@ import signal import time +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + stop_program = False # This a demo that shows running Snowboy in another thread @@ -40,8 +45,8 @@ def signal_handler(signal, frame): try: num1 = int(raw_input("Enter the first number to add: ")) num2 = int(raw_input("Enter the second number to add: ")) - print "Sum of number: {}".format(num1 + num2) + print("Sum of number: {}".format(num1 + num2)) except ValueError: - print "You did not enter a number." + print("You did not enter a number.") threaded_detector.terminate() diff --git a/examples/Python/snowboydecoder_arecord.py b/examples/Python/snowboydecoder_arecord.py index efa3be46..992c9d80 100644 --- a/examples/Python/snowboydecoder_arecord.py +++ b/examples/Python/snowboydecoder_arecord.py @@ -5,6 +5,7 @@ import time import wave import os +import sys import logging import subprocess import threading @@ -92,7 +93,7 @@ def __init__(self, decoder_model, def record_proc(self): CHUNK = 2048 RECORD_RATE = 16000 - cmd = 'arecord -q -r %d -f S16_LE' % RECORD_RATE + cmd = 'arecord -D mic_channel8 -q -r %d -f S16_LE' % RECORD_RATE process = subprocess.Popen(cmd.split(' '), stdout = subprocess.PIPE, stderr = subprocess.PIPE) @@ -110,6 +111,18 @@ def init_recording(self): self.record_thread = threading.Thread(target = self.record_proc) self.record_thread.start() + def wsk_transcribe_audio(audio): + sys.exit(0) + encoded_audio = open(audio, 'r').read().encode("base64") + # get the wsk values by running "wsk property get" + whisk_namespace = "" + whisk_action = "" + whisk_api = "https://openwhisk.ng.bluemix.net/api/v1/namespaces/" + whisk_namespace + "/actions/" + whisk_action + "?blocking=true&result=true" + whisk_auth = ("", "") + stt_username = "" + stt_password = "" + #r = requests.post(whisk_api, json={"content_type":"audio/flac","encoding":"base64","payload": encoded_audio ,"username":stt_username,"password":stt_password}, auth=whisk_auth) + def start(self, detected_callback=play_audio_file, interrupt_check=lambda: False, sleep_time=0.03): @@ -168,7 +181,16 @@ def start(self, detected_callback=play_audio_file, callback = detected_callback[ans-1] if callback is not None: callback() - + matrix_demos_dir = "/home/pi/matrix-creator-hal/build/demos/" + beamforming_result_path = "/tmp/beamforming_result.flac" + cwd = os.getcwd() + print("Running from " + cwd) + os.system(matrix_demos_dir + "micarray_recorder") + print("Voice recording complete") + os.system("sox -r 16000 -c 1 -e signed -b 16 " + cwd + "/mic_16000_s16le_channel_8.raw /tmp/channel_8.wav") + os.system("flac -f -s /tmp/channel_8.wav -o /tmp/beamforming_result.flac") + print("Conversion from raw to flac complete") + #wsk_transcribe_audio("/tmp/beamforming_result.flac") logger.debug("finished.") def terminate(self): @@ -178,4 +200,3 @@ def terminate(self): """ self.recording = False self.record_thread.join() - diff --git a/examples/REST_API/training_service.py b/examples/REST_API/training_service.py index 0e842fff..314d0009 100644 --- a/examples/REST_API/training_service.py +++ b/examples/REST_API/training_service.py @@ -25,7 +25,7 @@ def get_wave(fname): try: [_, wav1, wav2, wav3, out] = sys.argv except ValueError: - print "Usage: %s wave_file1 wave_file2 wave_file3 out_model_name" % sys.argv[0] + print("Usage: %s wave_file1 wave_file2 wave_file3 out_model_name" % sys.argv[0]) sys.exit() data = { @@ -46,7 +46,7 @@ def get_wave(fname): if response.ok: with open(out, "w") as outfile: outfile.write(response.content) - print "Saved model to '%s'." % out + print("Saved model to '%s'." % out) else: - print "Request failed." - print response.text + print("Request failed.") + print(response.text) diff --git a/swig/Python/snowboydecoder.py b/swig/Python/snowboydecoder.py index 7718999c..8956a145 100644 --- a/swig/Python/snowboydecoder.py +++ b/swig/Python/snowboydecoder.py @@ -76,6 +76,25 @@ def play_audio_file(fname=DETECT_DING): audio.terminate() +def wsk_transcribe_audio(): + rec_seconds = 3 + audio_path = "/tmp/input.flac" + os.system("rec /tmp/input.wav trim 0 " + str(rec_seconds)) + os.system("flac -f -s /tmp/input.wav -o " + audio_path ) + encoded_audio = open(audio_path, 'r').read().encode("base64") + # get the following wsk values by running "wsk property get" + whisk_namespace = "kkbankol%40us.ibm.com_dev" + whisk_action = "homeSequence" + whisk_api = "https://openwhisk.ng.bluemix.net/api/v1/namespaces/" + whisk_namespace + "/actions/" + whisk_action + "?blocking=true&result=true" + # running "wsk property get" should deliver a string like 'd16f007f-d412-4511-87d9-ca34d40c6cce:OWosfg0EHHgTw4FPRxFaPDWS1IqNgihX8OazAZ9XO75VtbNQH1k5yfx7CXDdjr5a' + # replace the "auth" value below with the string printed by the wsk cli + auth = 'd16f007f-d412-4511-87d9-ca34d40c6cce:OWosfg0EHHgTw4FPRxFaPDWS1IqNgihX8OazAZ9XO75VtbNQH1k5yfx7CXDdjr5a' + whisk_auth = tuple(auth.split(":")) + r = requests.post(whisk_api, json={"content_type":"audio/flac","encoding":"base64","payload": encoded_audio}, auth=whisk_auth) + print("Listening... Press Ctrl+C to exit") + return + + class HotwordDetector(object): """ Snowboy decoder to detect whether a keyword specified by `decoder_model` @@ -138,7 +157,6 @@ def audio_callback(in_data, frame_count, time_info, status): frames_per_buffer=2048, stream_callback=audio_callback) - def start(self, detected_callback=play_audio_file, interrupt_check=lambda: False, sleep_time=0.03, @@ -240,7 +258,6 @@ def start(self, detected_callback=play_audio_file, recordingCount = recordingCount + 1 self.recordedData.append(data) - logger.debug("finished.") def saveMessage(self):