ding, logger

This commit is contained in:
wassname
2019-01-12 11:12:11 +08:00
parent 21b06db316
commit a67bc9531d
5 changed files with 88 additions and 73 deletions
+9 -7
View File
@@ -1,17 +1,20 @@
#### RUNNING THE MODEL #### RUNNING THE MODEL
# Michels-MacBook-Pro:~ ShebMichel$ cd documents/pmlg/wake/decoder # cd documents/pmlg/wake/decoder
# Michels-MacBook-Pro:decoder ShebMichel$ python demo.py resources/HiBitLit.pmdl # python demo.py resources/HiBitLit.pmdl
from __future__ import print_function
import os import os
import sys import sys
import snowboydecoder import snowboydecoder
from snowboydecoder import play_ding, play_dong
import signal import signal
import numpy as np
import time import time
import numpy as np
import BitLit_main import BitLit_main
t0 = time.time() ## Time counter t0 = time.time() ## Time counter
interrupted = False interrupted = False
@@ -36,10 +39,9 @@ model = sys.argv[1]
# capture SIGINT signal, e.g., Ctrl+C # capture SIGINT signal, e.g., Ctrl+C
signal.signal(signal.SIGINT, signal_handler) signal.signal(signal.SIGINT, signal_handler)
play_ding()
detector = snowboydecoder.HotwordDetector(model, sensitivity=0.75) detector = snowboydecoder.HotwordDetector(model, sensitivity=0.5)
print("Listening for hotword (Hi BitLit)... Press Ctrl+C to exit") print("Listening... Press Ctrl+C to exit")
snowboydecoder.play_audio_file()
detector.start( detector.start(
+35 -23
View File
@@ -23,7 +23,7 @@ import hashlib
import tempfile import tempfile
from logger import logger from logger import logger
from snowboydecoder import play_audio_file from snowboydecoder import play_ding, play_dong
import speech_recognition as sr ## Packages for voice recognizer import speech_recognition as sr ## Packages for voice recognizer
for index, name in enumerate(sr.Microphone.list_microphone_names()): for index, name in enumerate(sr.Microphone.list_microphone_names()):
@@ -42,11 +42,11 @@ except:
def play_mp3(mp3_file): def play_mp3(mp3_file):
"""Play mp3 file with pyglet.""" """Play mp3 file with pyglet."""
# FIXME (wassname) It currently plays in background but we want to wait untill it's finished
source = pyglet.media.load(filename=mp3_file, streaming=False) source = pyglet.media.load(filename=mp3_file, streaming=False)
source.play() source.play()
time.sleep(source.duration*2 + 2) # TODO must be a better way to wait untill the media has played print(mp3_file, source.duration)
time.sleep(source.duration + 4) # must be a better way to wait untill the media has played
print(mp3_file, source.duration)
def cache_gtts(text, lang="en-nz", cache_file=None): def cache_gtts(text, lang="en-nz", cache_file=None):
""" """
@@ -71,7 +71,7 @@ def cache_gtts(text, lang="en-nz", cache_file=None):
en: English en: English
""" """
print('say:', text) logger.debug('say: %s', text)
if not cache_file: if not cache_file:
hash_filename = hashlib.md5(text.encode()).hexdigest() + '.mp3' hash_filename = hashlib.md5(text.encode()).hexdigest() + '.mp3'
cache_file = os.path.join(tempfile.gettempdir(), hash_filename) cache_file = os.path.join(tempfile.gettempdir(), hash_filename)
@@ -85,49 +85,61 @@ def generate_poem():
############ AUDIO CONVERSION TO TEST ############ AUDIO CONVERSION TO TEST
play_audio_file() play_dong()
t0 = time.time() t0 = time.time()
r = sr.Recognizer() r = sr.Recognizer()
with sr.Microphone() as source: with sr.Microphone() as source:
outfile1 = cache_gtts(text="Hi! My Name is BIT-LIT. PLEASE SPEAK SOME IDEAS FOR A POEM between the beeps.")
# print(r.energy_threshold)
# r.adjust_for_ambient_noise(source)
# print('energy_threshold', r.energy_threshold)
r.energy_threshold=50
print('mic', source)
outfile1 = cache_gtts(text="Hi! My Name is BIT-LIT. PLEASE SPEAK SOME IDEAS FOR A POEM AFTER THE BEEP.")
play_mp3(outfile1) play_mp3(outfile1)
play_ding()
play_audio_file() print('speak now', time.time())
audio = r.listen(source)
logger.debug('done recording %s', time.time())
logger.info('recorded %s s', len(audio.frame_data)/audio.sample_rate)
audio = r.listen(source, phrase_time_limit=20) play_dong()
play_audio_file() outfile2 = cache_gtts(text="BEEP. THANK YOU! GIVE ME A MINUTE TO GENERATE AND READ YOUR POEM")
outfile2 = cache_gtts(text="BEEP. THANK YOU! GIVE ME A MINUTE TO GENERATE AND READ YOUR POEM. BEEP")
play_mp3(outfile2) play_mp3(outfile2)
t1 = time.time() t1 = time.time()
print('listen took', t1 - t0) logger.debug('listen took %s', t1 - t0)
try: try:
print("using google speech to text...") logger.debug("using google speech to text...")
USER_INPUT = r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS) USER_INPUT = r.recognize_google_cloud(audio, credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS)
print("Google thinks you said: " + USER_INPUT) logger.info("Google thinks you said: " + USER_INPUT)
except sr.UnknownValueError as e: except sr.UnknownValueError as e:
print("Could not understand audio. {}".format(e)) logger.error("Could not understand audio. {}".format(e))
return return
except sr.RequestError as e: except sr.RequestError as e:
print("Could not request results; {0}".format(e)) logger.error("Could not request results; {0}".format(e))
return return
t1b = time.time() t1b = time.time()
print('transcribe took', t1b - t1) logger.debug('transcribe took %s', t1b - t1)
return
# Generate poem from user seed # Generate poem from user seed
text_generated = poem(USER_INPUT) text_generated = poem(USER_INPUT)
t2 = time.time() t2 = time.time()
logger.info("ML POEM is: %s", text_generated) logger.info("ML POEM is: %s", text_generated)
logger.info('poem and rhyme generation took %s', t2 - t1) logger.debug('poem and rhyme generation took %s', t2 - t1)
# TEXT CONVERSION IN AUDIO # TEXT CONVERSION IN AUDIO
# FEED POEM TO TRANSCRIBER # FEED POEM TO TRANSCRIBER
tts = gTTS(text=text_generated) tts = gTTS(text=text_generated)
# ts = datetime.datetime.utcnow().strftime('%Y%m%d_%H-%M-%S') ts = datetime.datetime.utcnow().strftime('%Y%m%d_%H-%M-%S')
poem_mp3 = "outputs/BitLit_poem.mp3"#.format(ts) poem_mp3 = "outputs/BitLit_{}.mp3".format(ts)
tts.save(poem_mp3) tts.save(poem_mp3)
play_mp3(poem_mp3) play_mp3(poem_mp3)
@@ -136,8 +148,8 @@ def generate_poem():
###### ######
t3 = time.time() t3 = time.time()
logger.info('Poem to speech took %s', t3 - t2) logger.debug('Poem to speech took %s', t3 - t2)
logger.info("Time spent is about: %s seconds") logger.debug("Total time spent is about: %s seconds", np.round(t3 - t0))
if __name__ == "__main__": if __name__ == "__main__":
+33 -41
View File
@@ -1,5 +1,5 @@
Forked from https://github.com/ShebMichel/BitLit_test1
# Run # Run
To run the program once installed To run the program once installed
@@ -42,43 +42,35 @@ API Keys:
When setup the layout should look something like When setup the layout should look something like
``` ├── BitLit_decoder.py
├── BitLit_decoder.py ├── BitLit_main.py
├── BitLit_main.py ├── BitLit_main.pyc
├── BitLit_main.pyc ├── BitLit_model_param.py
├── BitLit_model_param.py ├── BitLit_model_param.pyc
├── BitLit_model_param.pyc ├── HiBitLit.pmdl
├── HiBitLit.pmdl ├── README.md
├── README.md ├── logger.py
├── logger.py ├── logger.pyc
├── logger.pyc ├── outputs
├── outputs ├── poem_generator.py
├── poem_generator.py ├── poem_generator.pyc
├── poem_generator.pyc ├── requirements.txt
├── requirements.txt ├── secrets
├── secrets ├── google_cloud_credentials.json
── google_cloud_credentials.json ── google_cloud_credentials.template.json
│ └── google_cloud_credentials.template.json ├── snowboy
├── snowboy ├── _snowboydetect.so
│ ├── _snowboydetect.so │ ├── resources
│ ├── resources │ ├── alexa.umdl
│ │ ├── alexa.umdl │ │ ├── alexa_02092017.umdl
│ │ ├── alexa_02092017.umdl │ │ ├── common.res
│ │ ├── common.res │ │ ├── ding.wav
│ │ ├── ding.wav │ │ ├── dong.wav
│ │ ── dong.wav │ │ ── snowboy.umdl
│ └── snowboy.umdl ── snowboydetect.py
── snowboydetect.py ── snowboydetect.pyc
│ └── snowboydetect.pyc ── snowboydecoder.py
├── snowboydecoder.py ├── snowboydecoder.pyc
├── snowboydecoder.pyc └── weights
── weights ── model_poems.npy
── model_poems.npy ── model_rhymes.npy
└── model_rhymes.npy
```
# TODO
- [ ] make sure text logging works
- [ ] improve before and after text
- [ ] it start recording while prompt is playing?
+4 -2
View File
@@ -2,9 +2,11 @@
Voice to text to poem to speech Voice to text to poem to speech
Credits: Michel, Lauren, Thomas Credits: Michel, Lauren, Thomas
""" """
from __future__ import print_function
import os import os
import numpy as np import numpy as np
from logger import logger
import re import re
from textblob import TextBlob from textblob import TextBlob
import random import random
@@ -120,7 +122,7 @@ def poem(USER_INPUT):
start_string = [first_rhyme] start_string = [first_rhyme]
else: else:
start_string = [random.choice(list(word2idx_rhymes.keys()))] start_string = [random.choice(list(word2idx_rhymes.keys()))]
print("The word {} is not in our corpus of rhymes yet.".format(first_rhyme)) logger.error("The word {} is not in our corpus of rhymes yet.".format(first_rhyme))
input_eval = [ input_eval = [
word2idx_rhymes[s] for s in start_string word2idx_rhymes[s] for s in start_string
] # converts start_string to numbers the model understands ] # converts start_string to numbers the model understands
@@ -143,7 +145,7 @@ def poem(USER_INPUT):
input_eval = tf.expand_dims([predicted_id], 0) input_eval = tf.expand_dims([predicted_id], 0)
rhymes += [idx2word_rhymes[predicted_id]] rhymes += [idx2word_rhymes[predicted_id]]
print("rhymes:", rhymes) logger.info("rhymes:", rhymes)
#################### ####################
# POEM GENERATION # # POEM GENERATION #
+7
View File
@@ -60,6 +60,13 @@ def play_audio_file(fname=DETECT_DING):
audio.terminate() audio.terminate()
def play_ding():
play_audio_file(fname=DETECT_DING)
def play_dong():
play_audio_file(fname=DETECT_DONG)
class HotwordDetector(object): class HotwordDetector(object):
""" """
Snowboy decoder to detect whether a keyword specified by `decoder_model` Snowboy decoder to detect whether a keyword specified by `decoder_model`