Successful hotword detection; but later that processing audio gets a error - pyaudio

#Global Declarations
CHUNK_SIZE = 1024
MIN_VOLUME = 500
BUF_MAX_SIZE = 1024 * 10
RECORD_TIME_SECONDS = 7
def wakeword():
agent = snowboydecoder.HotwordDetector("res/bella.pmdl",
sensitivity=0.5)
agent.start(detected_callback=listenIntent, sleep_time=0.03)
agent.terminate()
It makes a successful detection. When it gets into listenIntent function it gives me a error
def listenIntent():
print("Hotword")
stream = pyaudio.PyAudio().open(
format = pyaudio.paInt16,
channels = 1,
rate = 44100,
input = True,
frames_per_buffer = 1024)
while True:
for i in range(0,int(44100/1024*RECORD_TIME_SECONDS)):
data = array('h',stream.read(CHUNK_SIZE))
stream_rec = pyaudio.PyAudio.open(format= pyaudio.paInt16,
channels = 2,
rate = 44100,
input = True,
frames_per_buffer = 1024)
threshold = max(stream_rec)
if threshold >= MIN_VOLUME:
print("Recording....")
frames = []
data_rec = data.read(CHUNK_SIZE)
frames.append(data_rec)
stream.stop_stream()
stream.close()
pyaudio.PyAudio.terminate()
_wavefile = wave.open("temp.wav",'wb')
_wavefile.setnchannels(1)
_wavefile.setsampwidth(pyaudio.PyAudio.get_sample_size(pyaudio.paInt16))
_wavefile.setframerate(44100)
_wavefile.writeframes(b''.join(frames))
_wavefile.close()
processAction()
def processAction():
access_token = ""
client = wit(access_token = access_token)
with open("temp.wav",'rb') as f:
response = client.speech(f, None,{'Content-Type': 'audio/wav',
'encoding':'encode',
'bits':'1024',
'rate':'44100',
'endian': 'big'})
print("Response" + str(response))
if __name__ == "__main__":
wakeword()
process
- First wakeword agent triggers
- Starts audio recording for 7 seconds
- Processes the audio file it wit.ai to understand the intent
As soon as i run the program it does a successful hotword detection but later part fails. i.e recording. I get this error.
Hotword
INFO:snowboy:Keyword 1 detected at time: 2017-10-15 10:10:53
||PaMacCore (AUHAL)|| Error on line 2490: err='-10863', msg=Audio
Unit: cannot do in current context
||PaMacCore (AUHAL)|| Error on line 2490: err='-10863', msg=Audio
Unit: cannot do in current context

Related

Actor Critic model returns NaN for action probabilities

I am new to RL and walking through the Keras implementation of Actor Critic.
As a variant of it, I am trying to learn the strategy for WORDLE. However, after a few runs, my action spaces all go down to nan.
actions = [nan nan nan ... nan nan nan]
Not sure what's happening. Could someone have any insights or pointers?
Attaching my code for reference.
Thanks
import pandas as pd
import numpy as np
import random
import string
import random
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
# Configuration parameters for the whole setup
gamma = 0.9 # Discount factor for past rewards
max_runs = 10000
eps = np.finfo(np.float32).eps.item() # Smallest number such that 1.0 + eps != 1.0
my_file = open("<wordle set of words data path>", "r")
content = my_file.read()
content = list(content.split('\n'))
lower_alphabet = list(string.ascii_letters)[:26]
def get_secret_word():
return random.choice(content)
def reset_available_action_space():
return [1 for i in range(len(content))]
def reset_guessed_alphabet_state():
return [0 for i in range(len(lower_alphabet))]
# array of 26 which represents which alphabet is available in word
def reset_contains_alphabet_state():
return [0 for i in range(len(lower_alphabet))]
# Array of 26*5.
# First 26 represent which alphabet was correctly guessed at the first slot
# Second 26 represent which alphabet was correctly guessed at the second slot. And so on for the next 5 slots.
def reset_correct_alphabet_pos_state():
return [0 for i in range(len(lower_alphabet)*5)]
def select_and_update_AVAILABLE_ACTION_SPACE(actions):
action_index = 0
while AVAILABLE_ACTION_SPACE[actions[action_index]] == False:
action_index += 1
AVAILABLE_ACTION_SPACE[actions[action_index]] = 0
return actions[action_index]
def env_reset():
AVAILABLE_ACTION_SPACE = reset_available_action_space()
guessed_alphabet_state = reset_guessed_alphabet_state()
contains_alphabet_state = reset_contains_alphabet_state()
correct_alphabet_pos_state = reset_correct_alphabet_pos_state()
state = guessed_alphabet_state + contains_alphabet_state + correct_alphabet_pos_state
SECRET_WORD = get_secret_word()
return state, SECRET_WORD, AVAILABLE_ACTION_SPACE
def env_step(action, state):
guessed_word = content[action]
guessed_alphabet_state = state[:26]
contains_alphabet_state = state[26:52]
correct_alphabet_pos_state = state[52:]
done = False
reward = -10
if SECRET_WORD == guessed_word:
done = True
reward = 10
secret_word = list(SECRET_WORD)
guessed_word = list(guessed_word)
for index_, char_ in enumerate(guessed_word):
alphabet_index = lower_alphabet.index(char_)
guessed_alphabet_state[alphabet_index] = 1
if char_ in secret_word:
contains_alphabet_state[alphabet_index] = 1
if secret_word[index_] == char_:
correct_alphabet_pos_state[26*index_ + alphabet_index] = 1
state = guessed_alphabet_state + contains_alphabet_state + correct_alphabet_pos_state
return state, reward, done
num_inputs = 182
num_actions = len(content)
num_hidden_1 = 256
num_hidden_2 = 128
inputs = layers.Input(shape=(num_inputs,))
common = layers.Dense(num_hidden_1, activation="relu")(inputs)
common = layers.Dense(num_hidden_2, activation="relu")(common)
action = layers.Dense(num_actions, activation="softmax")(common)
critic = layers.Dense(1)(common)
model = keras.Model(inputs=inputs, outputs=[action, critic])
optimizer = keras.optimizers.Adam(learning_rate=0.001)
huber_loss = keras.losses.Huber()
action_probs_history = []
critic_value_history = []
rewards_history = []
running_reward = 0
episode_count = 0
for runs in range(max_runs):
max_steps_per_episode = 6
state, SECRET_WORD, AVAILABLE_ACTION_SPACE = env_reset()
episode_reward = 0
with tf.GradientTape() as tape:
for timestep in range(max_steps_per_episode):
state_tensor = tf.convert_to_tensor(state)
state_tensor = tf.expand_dims(state, 0)
action_probs, critic_value = model(state_tensor)
critic_value_history.append(critic_value[0, 0])
actions = np.random.choice(num_actions, size=max_steps_per_episode+1, replace = False, p=np.squeeze(action_probs))
action = select_and_update_AVAILABLE_ACTION_SPACE(actions)
action_probs_history.append(tf.math.log(action_probs[0, action]))
state, reward, done = env_step(action, state)
rewards_history.append(reward)
episode_reward += reward
if done:
break
# Update running reward to check condition for solving
running_reward = 0.05 * episode_reward + (1 - 0.05) * running_reward
# Calculate expected value from rewards
# - At each timestep what was the total reward received after that timestep
# - Rewards in the past are discounted by multiplying them with gamma
# - These are the labels for our critic
returns = []
discounted_sum = 0
for r in rewards_history[::-1]:
discounted_sum = r + gamma * discounted_sum
returns.insert(0, discounted_sum)
# Normalize
returns = np.array(returns)
returns = (returns - np.mean(returns)) / (np.std(returns) + eps)
returns = returns.tolist()
# Calculating loss values to update our network
history = zip(action_probs_history, critic_value_history, returns)
actor_losses = []
critic_losses = []
for log_prob, value, ret in history:
# At this point in history, the critic estimated that we would get a
# total reward = `value` in the future. We took an action with log probability
# of `log_prob` and ended up recieving a total reward = `ret`.
# The actor must be updated so that it predicts an action that leads to
# high rewards (compared to critic's estimate) with high probability.
diff = ret - value
actor_losses.append(-log_prob * diff) # actor loss
# The critic must be updated so that it predicts a better estimate of
# the future rewards.
critic_losses.append(
huber_loss(tf.expand_dims(value, 0), tf.expand_dims(ret, 0))
)
# Backpropagation
loss_value = sum(actor_losses) + sum(critic_losses)
grads = tape.gradient(loss_value, model.trainable_variables)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
# Clear the loss and reward history
action_probs_history.clear()
critic_value_history.clear()
rewards_history.clear()
# Log details
episode_count += 1
if episode_count % 10 == 0:
template = "running reward: {:.2f} at episode {}"
print(template.format(running_reward, episode_count))
wordle set of words data path => https://gist.github.com/cfreshman/a03ef2cba789d8cf00c08f767e0fad7b
My State Space is [guessed alphabets, alphabets contained in the secret word, alphabet in the correct position]
guessed alphabets => Array of 26 size (a-z)
alphabets contained in the secret word => Array of 26 size (a-z)
alphabet in the correct position => Array of 26 * 5 [(a-z), (a-z), (a-z), (a-z), (a-z)] (as each word is 5 letters)
The Available action spaces get updated after every action. The previously taken actions are no longer available for future actions.
I have tried both relu and tanh for activation
Observation: Critic Value keeps increasing to an extremely large values

PyTorch-lightning models running out of Memory after 1st epoch

I saw a Kaggle kernel on PyTorch and run it with the same img_size, batch_size, etc. and created another PyTorch-lightning kernel with exact same values but my lightning model runs out of memory after about 1.5 epochs (each epoch contains 8750 steps) on the first fold whereas the native PyTorch model runs for whole 5 folds. Is there any way to improve the code or release memory? I could have tried to delete the models or do some garbage collection but if it doesn't complete even the first fold I can't delete the models and things.
def run_fold(fold):
df_train = train[train['fold'] != fold]
df_valid = train[train['fold'] == fold]
train_dataset = G2NetDataset(df_train, get_train_aug())
valid_dataset = G2NetDataset(df_valid, get_test_aug())
train_dl = DataLoader(train_dataset,
batch_size = config.batch_size,
num_workers = config.num_workers,
shuffle = True,
drop_last = True,
pin_memory = True)
valid_dl = DataLoader(valid_dataset,
batch_size = config.batch_size,
num_workers = config.num_workers,
shuffle = False,
drop_last = False,
pin_memory = True)
model = Classifier()
logger = pl.loggers.WandbLogger(project='G2Net', name=f'fold: {fold}')
trainer = pl.Trainer(gpus = 1,
max_epochs = config.epochs,
fast_dev_run = config.debug,
logger = logger,
log_every_n_steps=10)
trainer.fit(model, train_dl, valid_dl)
result = trainer.test(test_dataloaders = valid_dl)
wandb.run.finish()
return result
def main():
if config.train:
results = []
for fold in range(config.n_fold):
result = run_fold(fold)
results.append(result)
return results
results = main()
I cannot say much without looking at your model class, but couple possible issues that I encountered were metric and loss evaluation for logging.
For example, stuff like
pl.metrics.Accuracy(compute_on_step=False)
requires and explicit call of .compute()
def training_epoch_end(self, outputs):
loss = sum([out['loss'] for out in outputs])/len(outputs)
self.log_dict({'train_loss' : loss.detach(),
'train_accuracy' : self.train_metric.compute()})
at the epoch end.

how to handle value errors in while 1 loop with scheduling it with a Timer class

i have written a code to read registers of a modbus communication protocol. i have attached the code below as well.. am able to overcome the i/o errors by exception handling method where as the value error that i get , am not able throw that error and move on.
Basically what i am doing is am reading the data in the registers and sending up to the server. but my requirement is i have to read the values every second and for 24 hours. so i need to build a robust system that will overcome these value errors and continue executing the threads i have created.
the code to read registers is given below :
import minimalmodbus
import serial
from ctypes import *
import struct
import time
minimalmodbus.BAUDRATE = 9600
minimalmodbus.PARITY = serial.PARITY_NONE
minimalmodbus.BYTESIZE = 8
minimalmodbus.TIMEOUT=5
minimalmodbus.CLOSE_PORT_AFTER_EACH_CALL = True
energy_meter = serial.Serial("/dev/ttyUSB0", baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=5)
energy_meter = minimalmodbus.Instrument('/dev/ttyUSB0', 2, mode='rtu')
#energy_meter.debug = True
def convert_in_float(value1, value2):
raw = struct.pack('>HH',value1,value2)
ans = struct.unpack('>f', raw)[0]
return ans
def sdm630():
parameter_list1 = [ 0 ] * 0x12
parameter_list2 = [ 0 ] * 3
parameter_list3 = [ 0 ] * 6
#print energy_meter
error = 0
try:
index = 0
read_values1 = energy_meter.read_registers( 0 , 0x24, 4)
for i in range ( 0, 0x24, 2):
parameter_list1[index] = convert_in_float( read_values1[i], read_values1[i+1])
#print "Parameter read from register : ", hex(index), "is : ", parameter_list1[index] ,"\n"
index = index + 1
#read parameter list 2 & 3 in a similar way
error = 0
return error, parameter_list1, parameter_list2, parameter_list3, int(time.time())
except IOError or ValueError:
print "got error"
error = 1
return error, parameter_list1, parameter_list2, parameter_list3, int(time.time())
also, i have written a separate code to dump all data to server and is shown below :
import time
from pymongo import MongoClient
client = MongoClient('mongodb://10.32.36.40:27017')
db = client.clytics
collection = db['raspberry_pi']
def pushData(error, value1, value2, value3, value4):
if error == 0 :
temp_js = {
#variable assignment
}
temp_js_id = collection.insert(temp_js)
using the above two codes i have created threads for each function. and i only execute this code and after 20 minutes of execution , i get value errors and the program doesnt execute anymore. the main program is given below :
import time
from threading import Thread
from threading import Timer
from Queue import Queue
from modbus import sdm630
from dumpInDB import pushData
from processData import process_the_data
DELAY_SEC = 1
DELAY_MIN = 60
LOOP_LIMIT = 60
def getData(q):
error, parameter_list1, parameter_list2 , parameter_list3, parameter_list4= sdm630()
print "In getData - data:", parameter_list1, parameter_list2
q.put([error, parameter_list1, parameter_list2, parameter_list3, parameter_list4])
def processData(q1,q2):
sec_data = q1.get()
min_data = process_the_data(sec_data)
print "In processData - data:", sec_data, min_data
q2.put(min_data)
print "queue:", q2.qsize()
def putData(q):
#print "In putData - data:", value[0], value[1], value[2]
for i in range(0, q.qsize()):
value = q.get()
print "In putData - data:", value[0], value[1], value[2], value[3]
pushData( value[0], value[1] , value[2], value[3] , value[4])
def thread1(threadName, q):
i = 0
while 1:
t = Timer( DELAY_SEC, getData, args = (q,))
t.start()
time.sleep(DELAY_SEC)
def thread2( threadName, q1,q2):
i = 0
print "in thread2"
while 1:
t = Timer( DELAY_SEC, processData, args = (q1,q2,))
t.start()
time.sleep(DELAY_SEC)
def thread3( threadName, q):
i = 0
print "in thread3"
while 1:
t = Timer( DELAY_MIN, putData, args = (q,))
t.start()
print "schedule time - min"
time.sleep(DELAY_MIN)
queue_second = Queue()
queue_minute = Queue()
thread1 = Thread( target=thread1, args=("Thread-1", queue_second) )
thread2 = Thread( target=thread2, args=("Thread-2", queue_second, queue_minute) )
thread3 = Thread( target=thread3, args=("Thread-3", queue_minute) )
thread1.start()
thread2.start()
thread3.start()
thread1.join()
thread2.join()
thread3.join()
am stuck with this error. shown below :
minimalmodbus.Instrument<id=0xb6b2d9b8, address=2, mode=rtu, close_port_after_each_call=True, precalculate_read_size=True, debug=False, serial=Serial<id=0xb6b482f0, open=False>(port='/dev/ttyUSB0', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=5, xonxoff=False, rtscts=False, dsrdtr=False)>
Traceback (most recent call last):
File "topScript.py", line 7, in <module>
from modbus import sdm630
File "/home/pi/scripts/modbus.py", line 60, in <module>
sdm630()
File "/home/pi/scripts/modbus.py", line 32, in sdm630
read_values1 = energy_meter.read_registers( 0 , 0x24, 4)
File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 498, in read_registers
numberOfRegisters=numberOfRegisters, payloadformat='registers')
File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 697, in _genericCommand
payloadFromSlave = self._performCommand(functioncode, payloadToSlave)
File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 798, in _performCommand
payloadFromSlave = _extractPayload(response, self.address, self.mode, functioncode)
File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 1075, in _extractPayload
raise ValueError(text)
ValueError: Checksum error in rtu mode: '\xa6\xe6' instead of '\xf7[' . The response is: '\xff\xf7HCeN\xce\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?\x80\x00\x00?\x80\x00\x00?\x80\x00\x00\xa6\xe6' (plain response: '\xff\xf7HCeN\xce\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
sometimes value error keeps popping up for sometime and finally fails and gives a message saying no more threads can be created.(reached to maximum level)
Your syntax to handle multiple exceptions is wrong. Use something like:
except (ValueError, IOError):
For more details see the Python tutorial https://docs.python.org/2/tutorial/errors.html

Constant Runtime Error -Python - Tkinter

My problem here is that I get a Runtime Error every time I run my code. Also, I am unable to bind my "Repeat" button with the "RUN" function. I do not understand why the button will not work and I am also unsure why the "self.canvas.delete()" does not work and why the new frame does not open when the puzzle has been solved (when all of the disks have been moved to a new pole) and why it opens straight away.
from Tkinter import *
import sys
import time
def Hanoi(n, src, dst, tmp, move):
if n == 0:
return n
else:
Hanoi(n-1, src, tmp, dst, move)
move(src, dst)
Hanoi(n-1, tmp, dst, src, move)
class hanoi():
master = Tk()
width = 2000
height = 400
time = 0.1
big_sep = 30
small_sep = 5
def __init__(self):
self.User_Input = input("Please enter the amount of disks")
self.canvas = Canvas(hanoi.master, height = hanoi.height, width = hanoi.width)
self.canvas.pack()
self.poles = [[], [], []]
self.Add_Objects()
def Add_Objects(self):
floor_size = 1000-2*hanoi.small_sep
self.disk_height = (400 - 4*hanoi.big_sep) / self.User_Input
self.disk_width = (floor_size - 4*hanoi.small_sep) / 3
disk_h = self.disk_height
disk_w = self.disk_width
global disk_h
global disk_w
width = hanoi.width
height = hanoi.height
small_sep = hanoi.small_sep
big_sep = hanoi.big_sep
#create poles
bar_pos = [ 4*small_sep + disk_w/2 + (disk_w+small_sep)*i for i in range(0,3) ]
for x in bar_pos:
print x
self.canvas.create_rectangle(x-small_sep, big_sep, x+small_sep, 400-big_sep-1, fill='black')
#create bases
for x in bar_pos:
print i
self.canvas.create_rectangle(x-(13*small_sep), 370, x+(13*small_sep), 410, fill = "brown")
#create disks
for i in range(self.User_Input):
print 2*small_sep + 4*small_sep*i, height-big_sep-1-disk_h*i,2*small_sep + disk_w - 4*small_sep*i, height-big_sep-1-disk_h*(i+1)
disk = self.canvas.create_rectangle(2*small_sep + 4*small_sep*i, 400-big_sep-1-disk_h*i,2*small_sep + disk_w - 4*small_sep*i, 400-big_sep-1-disk_h*(i+1),fill='blue')
self.poles[0].append(disk)
self.canvas.update()
def Move_Object(self, origin, dest):
print len(self.poles[dest]), self.User_Input-1
current_disk = self.poles[origin].pop()
self.poles[dest].append(current_disk)
x_distance = (dest - origin) * (disk_w)
y_distance = ((len(self.poles[origin]) - len(self.poles[dest])+1) * disk_h)
self.canvas.move(current_disk, x_distance, y_distance)
self.canvas.update()
time.sleep(hanoi.time)
if len(self.poles[dest]) == self.User_Input:
self.canvas.delete()
new_frame = Frame(hanoi.master, width = 300, height = 300)
new_frame.pack()
print "Yayar"
newb = Button(new_frame, text = "Replay", )
newb.place(relx = 0.5, rely = 0.5)
newb.bind("<ButtonPress-1>", self.RUN)
def RUN(self):
Hanoi(self.User_Input, 0, 2, 1, self.Move_Object)
if __name__ == "__main__":
print "hello"
hanoi().RUN()

Pygame plays mp3 too fast (regardless of what frequency i define)

I´m playing an mp3 file I get after a request to google translate. When I play it through pygame it plays at twice the speed, regardless of what frequency I set in pygame.mixer.init()
Here's the code:
import tweepy, urllib, os, pygame, pycurl, difflib, time
pygame.init()
pygame.mixer.init(frequency=22050, size=-16, channels=2, buffer=4096)
consumer_key = 'xxxxxxxxx'
consumer_secret = 'xxxxxxxx'
access_token = 'xxxxxx'
access_token_secret = 'xxxxxx'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# function to download a file from a url, used for testing
def downloadFile(url, fileName):
fp = open(fileName, "wb")
curl = pycurl.Curl()
curl.setopt(pycurl.URL, url)
curl.setopt(pycurl.WRITEDATA, fp)
curl.perform()
curl.close()
fp.close()
# returns the appropriate google speech url for a particular phrase
def getGoogleSpeechURL(phrase):
googleTranslateURL = "http://translate.google.com/translate_tts?tl=en&"
parameters = {'q': phrase}
data = urllib.urlencode(parameters)
googleTranslateURL = "%s%s" % (googleTranslateURL,data)
return googleTranslateURL
def speakSpeechFromText(phrase):
googleSpeechURL = getGoogleSpeechURL(phrase.encode('utf-8').strip())
downloadFile(googleSpeechURL,"tts.mp3")
pygame.mixer.music.load("tts.mp3")
pygame.mixer.music.play(0,0.0)
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
#os.system("mplayer tts.mp3 -af extrastereo=0 &")
def diffindex(string1, string2):
for i, (char1, char2) in enumerate(zip(string1, string2)):
if char1 != char2:
return 1
return 2
A = "a"
B = "b"
api = tweepy.API(auth)
while(true)
results = api.search(q="Hackney", count=1, result_type="recent")
for result in results:
A = result.text
if diffindex(A, B) != 2:
speakSpeechFromText(A)
B = A
else:
print ("jaha")
time.sleep(20)
Sorry if there´s a lot of irrelevant stuff there as well but I thought it might be of use.
I got it:
pygame.mixer.pre_init(16000, -16, 2, 2048) # setup mixer to avoid sound lag
pygame.mixer.init()
pygame.mixer.music.load("mymp3file.mp3")
pygame.mixer.music.play()
# I adjusted the 16000 - it was 44100, tryied 48000, 22000, 110000 and themn 16000
You should call the mixer.init() before pygame.init() because pygame.init() also initializing the mixer.
If you have to call mixer.init() after pygame.init() first call mixer.quit().
It's all in the docs :).