Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Python fixes #383

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ install:
- node lib/node/index.js

before_script:
# python: check for syntax errors or undefined names
- if [[ $NODE_VERSION == "9.0.0" ]]; then pip install flake8; flake8 . --select=E901,E999,F821,F822,F823; fi;
# if publishing, do it
- if [[ $REPUBLISH_BINARY == true ]]; then node-pre-gyp package unpublish; fi;
- if [[ $PUBLISH_BINARY == true ]]; then node-pre-gyp package publish; fi;
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ To run the Java example script:

cd swig/Python
make
# Set temporary python path to load the compiled module instead of the currently installed one.
PYTHONPATH=$(pwd)

SWIG will generate a `_snowboydetect.so` file and a simple (but hard-to-read) python wrapper `snowboydetect.py`. We have provided a higher level python wrapper `snowboydecoder.py` on top of that.

Expand Down
Empty file removed examples/Python/__init__.py
Empty file.
1 change: 0 additions & 1 deletion examples/Python/_snowboydetect.so

This file was deleted.

3 changes: 2 additions & 1 deletion examples/Python/demo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import snowboydecoder
#!/usr/bin/env python2
from snowboy import snowboydecoder
import sys
import signal

Expand Down
3 changes: 2 additions & 1 deletion examples/Python/demo2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import snowboydecoder
#!/usr/bin/env python2
from snowboy import snowboydecoder
import sys
import signal

Expand Down
4 changes: 2 additions & 2 deletions examples/Python/demo3.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import snowboydecoder
#!/usr/bin/env python2
from snowboy import snowboydecoder
import sys
import wave

Expand Down Expand Up @@ -37,4 +38,3 @@
print('Hotword Detected!')
else:
print('Hotword Not Detected!')

20 changes: 9 additions & 11 deletions examples/Python/demo4.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import snowboydecoder
#!/usr/bin/env python2
from snowboy import snowboydecoder
from __future__ import print_function
import sys
import signal
import speech_recognition as sr
Expand All @@ -18,7 +20,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 @@ -29,9 +31,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 @@ -51,8 +53,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 @@ -61,7 +63,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 All @@ -70,7 +72,3 @@ def interrupt_callback():
sleep_time=0.01)

detector.terminate()




11 changes: 9 additions & 2 deletions examples/Python/demo_threaded.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
from __future__ import print_function

import snowboythreaded
import sys
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 +47,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()
1 change: 0 additions & 1 deletion examples/Python/snowboydetect.py

This file was deleted.

1 change: 0 additions & 1 deletion examples/Python3/_snowboydetect.so

This file was deleted.

44 changes: 29 additions & 15 deletions examples/Python3/demo.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import snowboydecoder
#!/usr/bin/env python3
from __future__ import print_function
from snowboy import snowboydecoder
import sys
import signal
import os

interrupted = False

Expand All @@ -14,22 +17,33 @@ def interrupt_callback():
global interrupted
return interrupted

if len(sys.argv) == 1:
print("Error: need to specify model name")
print("Usage: python demo.py your.model")
sys.exit(-1)
def main():
if len(sys.argv) < 2:
print("Using default alexa model.")
model = os.path.join(snowboydecoder.TOP_DIR, 'resources/alexa/alexa-avs-sample-app/alexa.umdl')
elif len(sys.argv) > 2:
print("Error: need to specify 1 model name")
print("Usage: python demo.py your.model")
sys.exit(-1)
else:
model = sys.argv[1]

model = sys.argv[1]
if not os.path.isfile(model):
print("Error: Not a valid model.")
sys.exit(-1)

# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)

detector = snowboydecoder.HotwordDetector(model, sensitivity=0.5)
print('Listening... Press Ctrl+C to exit')
detector = snowboydecoder.HotwordDetector(model, sensitivity=0.5, apply_frontend=True)
print('Listening... Press Ctrl+C to exit')

# main loop
detector.start(detected_callback=snowboydecoder.play_audio_file,
interrupt_check=interrupt_callback,
sleep_time=0.03)
# main loop
detector.start(detected_callback=snowboydecoder.play_audio_file,
interrupt_check=interrupt_callback,
sleep_time=0.03)

detector.terminate()
detector.terminate()

if __name__ == "__main__":
main()
44 changes: 25 additions & 19 deletions examples/Python3/demo2.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import snowboydecoder
#!/usr/bin/env python3
from __future__ import print_function
from snowboy import snowboydecoder
import sys
import signal

Expand All @@ -16,26 +18,30 @@ def interrupt_callback():
global interrupted
return interrupted

if len(sys.argv) != 3:
print("Error: need to specify 2 model names")
print("Usage: python demo.py 1st.model 2nd.model")
sys.exit(-1)
def main():
if len(sys.argv) != 3:
print("Error: need to specify 2 model names")
print("Usage: python demo.py 1st.model 2nd.model")
sys.exit(-1)

models = sys.argv[1:]
models = sys.argv[1:]

# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)
# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)

sensitivity = [0.5]*len(models)
detector = snowboydecoder.HotwordDetector(models, sensitivity=sensitivity)
callbacks = [lambda: snowboydecoder.play_audio_file(snowboydecoder.DETECT_DING),
lambda: snowboydecoder.play_audio_file(snowboydecoder.DETECT_DONG)]
print('Listening... Press Ctrl+C to exit')
sensitivity = [0.5]*len(models)
detector = snowboydecoder.HotwordDetector(models, sensitivity=sensitivity)
callbacks = [lambda: snowboydecoder.play_audio_file(snowboydecoder.DETECT_DING),
lambda: snowboydecoder.play_audio_file(snowboydecoder.DETECT_DONG)]
print('Listening... Press Ctrl+C to exit')

# main loop
# make sure you have the same numbers of callbacks and models
detector.start(detected_callback=callbacks,
interrupt_check=interrupt_callback,
sleep_time=0.03)
# main loop
# make sure you have the same numbers of callbacks and models
detector.start(detected_callback=callbacks,
interrupt_check=interrupt_callback,
sleep_time=0.03)

detector.terminate()
detector.terminate()

if __name__ == "__main__":
main()
46 changes: 25 additions & 21 deletions examples/Python3/demo3.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import snowboydecoder
#!/usr/bin/env python3
from __future__ import print_function
from snowboy import snowboydecoder
import sys
import wave

Expand All @@ -12,29 +14,31 @@
# Should print:
# Hotword Not Detected!

def main():
if len(sys.argv) != 3:
print("Error: need to specify wave file name and model name")
print("Usage: python demo3.py wave_file model_file")
sys.exit(-1)

if len(sys.argv) != 3:
print("Error: need to specify wave file name and model name")
print("Usage: python demo3.py wave_file model_file")
sys.exit(-1)
wave_file = sys.argv[1]
model_file = sys.argv[2]

wave_file = sys.argv[1]
model_file = sys.argv[2]
f = wave.open(wave_file)
assert f.getnchannels() == 1, "Error: Snowboy only supports 1 channel of audio (mono, not stereo)"
assert f.getframerate() == 16000, "Error: Snowboy only supports 16K sampling rate"
assert f.getsampwidth() == 2, "Error: Snowboy only supports 16bit per sample"
data = f.readframes(f.getnframes())
f.close()

f = wave.open(wave_file)
assert f.getnchannels() == 1, "Error: Snowboy only supports 1 channel of audio (mono, not stereo)"
assert f.getframerate() == 16000, "Error: Snowboy only supports 16K sampling rate"
assert f.getsampwidth() == 2, "Error: Snowboy only supports 16bit per sample"
data = f.readframes(f.getnframes())
f.close()
sensitivity = 0.5
detection = snowboydecoder.HotwordDetector(model_file, sensitivity=sensitivity)

sensitivity = 0.5
detection = snowboydecoder.HotwordDetector(model_file, sensitivity=sensitivity)
ans = detection.detector.RunDetection(data)

ans = detection.detector.RunDetection(data)

if ans == 1:
print('Hotword Detected!')
else:
print('Hotword Not Detected!')
if ans == 1:
print('Hotword Detected!')
else:
print('Hotword Not Detected!')

if __name__ == "__main__":
main()
40 changes: 21 additions & 19 deletions examples/Python3/demo4.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import snowboydecoder
#!/usr/bin/env python3
from __future__ import print_function
from snowboy import snowboydecoder
import sys
import signal
import speech_recognition as sr
Expand Down Expand Up @@ -49,27 +51,27 @@ def interrupt_callback():
global interrupted
return interrupted

if len(sys.argv) == 1:
print("Error: need to specify model name")
print("Usage: python demo.py your.model")
sys.exit(-1)
def main():
if len(sys.argv) != 2:
print("Error: need to specify 1 model name")
print("Usage: python demo.py your.model")
sys.exit(-1)

model = sys.argv[1]
model = sys.argv[1]

# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)

detector = snowboydecoder.HotwordDetector(model, sensitivity=0.38)
print('Listening... Press Ctrl+C to exit')

# main loop
detector.start(detected_callback=detectedCallback,
audio_recorder_callback=audioRecorderCallback,
interrupt_check=interrupt_callback,
sleep_time=0.01)

detector.terminate()
# capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler)

detector = snowboydecoder.HotwordDetector(model, sensitivity=0.38)
print('Listening... Press Ctrl+C to exit')

# main loop
detector.start(detected_callback=detectedCallback,
audio_recorder_callback=audioRecorderCallback,
interrupt_check=interrupt_callback,
sleep_time=0.01)

detector.terminate()

if __name__ == "__main__":
main()
1 change: 0 additions & 1 deletion examples/Python3/requirements.txt

This file was deleted.

1 change: 0 additions & 1 deletion examples/Python3/resources

This file was deleted.

1 change: 0 additions & 1 deletion examples/Python3/snowboydetect.py

This file was deleted.

Loading