Skip to content

Commit

Permalink
Merge remote-tracking branch 'cclauss/Fix-Python-syntax-errors' into …
Browse files Browse the repository at this point in the history
…python3_fix
  • Loading branch information
NicoHood committed May 10, 2018
2 parents a5a54ef + 7d82c53 commit e1fa55b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 17 deletions.
Binary file added examples/Python/Hey_Watson_PI.pmdl
Binary file not shown.
12 changes: 6 additions & 6 deletions examples/Python/demo4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand All @@ -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]
Expand All @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions examples/Python/demo_threaded.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
27 changes: 24 additions & 3 deletions examples/Python/snowboydecoder_arecord.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
import wave
import os
import sys
import logging
import subprocess
import threading
Expand Down Expand Up @@ -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)
Expand All @@ -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 = "<redacted>"
whisk_action = "<redacted>"
whisk_api = "https://openwhisk.ng.bluemix.net/api/v1/namespaces/" + whisk_namespace + "/actions/" + whisk_action + "?blocking=true&result=true"
whisk_auth = ("<redacted>", "<redacted>")
stt_username = "<redacted>"
stt_password = "<redacted>"
#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):
Expand Down Expand Up @@ -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):
Expand All @@ -178,4 +200,3 @@ def terminate(self):
"""
self.recording = False
self.record_thread.join()

8 changes: 4 additions & 4 deletions examples/REST_API/training_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
21 changes: 19 additions & 2 deletions swig/Python/snowboydecoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit e1fa55b

Please sign in to comment.