Python Forum
Problems feeding live input from my microphone into a keras model (SegFault: 11)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problems feeding live input from my microphone into a keras model (SegFault: 11)
#1
I'm trying to write a voice-activated goal horn in Python.

I'm using my own audio recordings to train the model, but I need my model to make predictions on live input. I believe I've figured out how to store my most recent audio data, but I run into problems when I try to extract the MFCC's.

import pyaudio
import librosa
import numpy as np

class Counter:
    def __init__(self):
        self.value = 0
    
    def add(self):
        self.value += 1
    
    def get(self):
        return self.value

class RingBuffer:
    """ class that implements a not-yet-full buffer """
    def __init__(self,size_max):
        self.max = size_max
        self.data = []

    class __Full:
        """ class that implements a full buffer """
        def append(self, x):
            """ Append an element overwriting the oldest one. """
            self.data[self.cur] = x
            self.cur = (self.cur+1) % self.max
        def get(self):
            """ return list of elements in correct order """
            return self.data[self.cur:]+self.data[:self.cur]

        def clear(self):
            self.data = []
            self.__class__ = RingBuffer


    def append(self,x):
        """append an element at the end of the buffer"""
        self.data.append(x)
        if len(self.data) == self.max:
            self.cur = 0
            # Permanently change self's class from non-full to full
            self.__class__ = self.__Full

    def get(self):
        return self.data

j = Counter()
ringBuffer = RingBuffer(1*22050)

def callback(in_data, frame_count, time_info, flag):
        
    audio_data = np.frombuffer(in_data, dtype=np.float32)
    audio_data = librosa.resample(audio_data, 44100, 22050)

    for i in audio_data:
        ringBuffer.append(i)

    signal = ringBuffer.get()
    signal = np.array(signal)

    if signal.shape[0] == 22050:
        print("\ncheckpoint 1")
        X_new = librosa.feature.mfcc(signal, sr=22050, n_mfcc=44, hop_length=512, n_fft=2048) # causes segfault: 11... WHY?
        print("checkpoint 2")
        print(j.get())

        j.add()
        
    return (in_data, pyaudio.paContinue)

pa = pyaudio.PyAudio()
stream = pa.open(format = pyaudio.paFloat32,
                 channels = 1,
                 rate = 44100,
                 output = False,
                 input = True,
                 stream_callback=callback)

stream.start_stream()

while stream.is_active():
    1
            
stream.close()
pa.terminate()

print("Program terminated. \n")
Output:
... checkpoint 1 checkpoint 2 136 checkpoint 1 checkpoint 2 137 checkpoint 1 checkpoint 2 138 checkpoint 1 Segmentation fault: 11
For some reason, when I call librosa.features.mfcc enough times (the exact number is inconsistent but always appears to be somewhere between 110 and 150), the program crashes. I've pinpointed the line that's producing the segfault, but I don't know why.

How can I resolve this issue?

Thank you.
Reply
#2
It turns out because I was calling librosa.feature.mfcc inside my callback function, that was causing the problem. I still don’t know exactly why that was an issue, but I solved the problem.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Supervised learning, tree based model - problems splitting data Pixel 0 644 May-16-2023, 05:25 PM
Last Post: Pixel
  1st layer tf.keras output shape set at multiple - need help! Afrodizzyjack 0 1,782 Jun-07-2022, 04:53 PM
Last Post: Afrodizzyjack
  issue displaying summary of whole keras CNN model on TensorFlow with python Afrodizzyjack 0 1,619 Oct-27-2021, 04:07 PM
Last Post: Afrodizzyjack
  Understanding Keras and TensorFlow and how to use them bytecrunch 1 2,052 Mar-11-2021, 02:40 PM
Last Post: jefsummers
  Keras.Predict into Dataframe Finpyth 13 9,563 Aug-31-2020, 07:22 AM
Last Post: hussainmujtaba
  Making a Basic Keras Model - Input Shape and Parameters MattKahn13 0 2,093 Aug-16-2020, 04:36 PM
Last Post: MattKahn13
  Error when import Keras Azadfalah 1 2,753 Apr-29-2020, 04:45 AM
Last Post: buran
  Keras + Matplotlib causing crash spearced 3 4,435 Feb-06-2020, 04:54 PM
Last Post: zljt3216
  Feeding new data to a model RawlinsCross 0 1,766 Feb-01-2020, 10:47 PM
Last Post: RawlinsCross
  Keras Dense layer with wrong input d1r4c 0 1,737 Jan-02-2020, 02:35 PM
Last Post: d1r4c

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020