Ejemplo n.º 1
0
def handle_source(json_data):
    data = str(json_data['data'])
    data = data[1:-1]
    global graph
    np_wav = np.fromstring(data, dtype=np.int16, sep=',') / \
        32768.0  # Convert to [-1.0, +1.0]
    # Compute RMS and convert to dB
    print('Successfully convert to NP rep', np_wav)
    rms = np.sqrt(np.mean(np_wav**2))
    db = dbFS(rms)
    print('Db...', db)
    # Make predictions
    print('Making prediction...')
    x = waveform_to_examples(np_wav, RATE)
    predictions = []
    if x.shape[0] != 0:
        x = x.reshape(len(x), 96, 64, 1)
    print('Successfully reshape x', x.shape)
    # pred = model.predict(x)
    # predictions.append(pred)

    print('Prediction: Speech (50%)')
    socketio.emit('audio_label', {
        'label': 'Unrecognized Sound',
        'accuracy': '1.0'
    })
Ejemplo n.º 2
0
def audio_samples(in_data, frame_count, time_info, status_flags):
    global graph
    np_wav = np.fromstring(in_data, dtype=np.int16) / \
        32768.0  # Convert to [-1.0, +1.0]
    # Compute RMS and convert to dB
    rms = np.sqrt(np.mean(np_wav**2))
    db = dbFS(rms)

    # Make predictions
    x = waveform_to_examples(np_wav, RATE)
    predictions = []
    with graph.as_default():
        if x.shape[0] != 0:
            x = x.reshape(len(x), 96, 64, 1)
            print('Reshape x successful', x.shape)
            pred = model.predict(x)
            predictions.append(pred)
        print('Prediction succeeded')
        for prediction in predictions:
            context_prediction = np.take(
                prediction[0], [homesounds.labels[x] for x in active_context])
            m = np.argmax(context_prediction)
            if (context_prediction[m] > PREDICTION_THRES
                    and db > DBLEVEL_THRES):
                print("Prediction: %s (%0.2f)" %
                      (homesounds.to_human_labels[active_context[m]],
                       context_prediction[m]))

    return (in_data, pyaudio.paContinue)
Ejemplo n.º 3
0
def handle_source(json_data):
    data = str(json_data['data'])
    data = data[1:-1]
    global graph
    np_wav = np.fromstring(data, dtype=np.int16, sep=',') / \
        32768.0  # Convert to [-1.0, +1.0]
    # Compute RMS and convert to dB
    rms = np.sqrt(np.mean(np_wav**2))
    db = dbFS(rms)
    # Make predictions
    x = waveform_to_examples(np_wav, RATE)
    predictions = []

    with graph.as_default():
        if x.shape[0] != 0:
            x = x.reshape(len(x), 96, 64, 1)
            pred = model.predict(x)
            predictions.append(pred)

        for prediction in predictions:
            context_prediction = np.take(
                prediction[0], [homesounds.labels[x] for x in active_context])
            m = np.argmax(context_prediction)
            print('Max prediction', str(
                homesounds.to_human_labels[active_context[m]]), str(context_prediction[m]))
            if (context_prediction[m] > PREDICTION_THRES and db > DBLEVEL_THRES):
                socketio.emit('audio_label',
                              {
                                  'label': str(homesounds.to_human_labels[active_context[m]]),
                                  'accuracy': str(context_prediction[m]),
                                  'db': str(db)
                              },
                              room=request.sid)
                print("Prediction: %s (%0.2f)" % (
                    homesounds.to_human_labels[active_context[m]], context_prediction[m]))
Ejemplo n.º 4
0
def audio_samples(in_data, frame_count, time_info, status_flags):
    global session
    global interpolators
    global audio_rms
    global candidate
    global m
    np_wav = np.fromstring(in_data,
                           dtype=np.int16) / 32768.0  # Convert to [-1.0, +1.0]

    # Compute RMS and convert to dB
    rms = np.sqrt(np.mean(np_wav**2))
    db = dbFS(rms)
    interp = interpolators[30]
    interp.animate(interp.end, db, 1.0)

    # Make Predictions
    x = waveform_to_examples(np_wav, RATE)
    predictions = []
    with session.graph.as_default():
        set_session(session)
        if x.shape[0] != 0:
            x = x.reshape(len(x), 96, 64, 1)
            pred = model.predict(x)
            predictions.append(pred)

        for prediction in predictions:
            m = np.argmax(prediction[0])
            candidate = (ubicoustics.to_human_labels[label[m]], prediction[0,
                                                                           m])
            num_classes = len(prediction[0])
            for k in range(num_classes):
                interp = interpolators[k]
                prev = interp.end
                interp.animate(prev, prediction[0, k], 1.0)
    return (in_data, pyaudio.paContinue)
Ejemplo n.º 5
0
def audio_samples(in_data):

    # get float data from string array
    in_data = in_data.replace("[", "").replace("]", "")
    in_data = in_data.split(",")
    in_data = np.array(in_data)
    in_data = in_data.astype(np.float)
    np_wav = in_data / 32768.0  #Convert to [-1.0, +1.0]
    #np_wav = np.fromstring(in_data, dtype=np.int16, sep=',') / 32768.0 # Convert to [-1.0, +1.0]

    # Compute RMS and convert to dB
    rms = np.sqrt(np.mean(np_wav**2))
    db = dbFS(rms)

    # Make predictions
    x = waveform_to_examples(np_wav, RATE)
    predictions = []
    with graph.as_default():
        if x.shape[0] != 0:
            x = x.reshape(len(x), 96, 64, 1)
            pred = model.predict(x)
            predictions.append(pred)

        for prediction in predictions:
            context_prediction = np.take(
                prediction[0], [homesounds.labels[x] for x in active_context])
            m = np.argmax(context_prediction)
            if (context_prediction[m] > PREDICTION_THRES
                    and db > DBLEVEL_THRES):
                #print ("Prediction: %s (%0.2f)" % (homesounds.to_human_labels[active_context[m]], context_prediction[m]))
                return ("Prediction: %s (%0.2f)" %
                        (homesounds.to_human_labels[active_context[m]],
                         context_prediction[m]))
Ejemplo n.º 6
0
def audio_samples(in_data):
    global graph
    print("======")
    np_wav = np.array(struct.unpack(
        'h' * RATE, in_data)) / 32768.0  # Convert to [-1.0, +1.0]
    print(np_wav)
    print(len(np_wav))
    # Compute RMS and convert to dB
    rms = np.sqrt(np.mean(np_wav**2))
    db = dbFS(rms)

    # Make predictions
    x = waveform_to_examples(np_wav, RATE)
    predictions = []
    with graph.as_default():
        if x.shape[0] != 0:
            x = x.reshape(len(x), 96, 64, 1)
            pred = model.predict(x)
            predictions.append(pred)

        for prediction in predictions:
            context_prediction = np.take(
                prediction[0], [homesounds.labels[x] for x in active_context])
            m = np.argmax(context_prediction)
            if (context_prediction[m] > PREDICTION_THRES
                    and db > DBLEVEL_THRES):
                print("Prediction: %s (%0.2f)" %
                      (homesounds.to_human_labels[active_context[m]],
                       context_prediction[m]))
                return homesounds.to_human_labels[active_context[m]]
Ejemplo n.º 7
0
def handle_source(json_data):
    data = str(json_data['data'])
    data = data[1:-1]
    global graph
    np_wav = np.fromstring(data, dtype=np.int16, sep=',') / \
        32768.0  # Convert to [-1.0, +1.0]
    # Compute RMS and convert to dB
    print('Successfully convert to NP rep', np_wav)
    rms = np.sqrt(np.mean(np_wav**2))
    db = dbFS(rms)
    print('Db...', db)
    # Make predictions
    print('Making prediction...')
    x = waveform_to_examples(np_wav, RATE)
    predictions = []
    if x.shape[0] != 0:
        x = x.reshape(len(x), 96, 64, 1)
    print('Successfully reshape x', x.shape)
    pred = model.predict(x)
    predictions.append(pred)

    with graph.as_default():
    if x.shape[0] != 0:
        x = x.reshape(len(x), 96, 64, 1)
        print('Successfully reshape x', x)
        # pred = model.predict(x)
        # predictions.append(pred)

    for prediction in predictions:
        context_prediction = np.take(
            prediction[0], [homesounds.labels[x] for x in active_context])
        m = np.argmax(context_prediction)
        print('Max prediction', str(
            homesounds.to_human_labels[active_context[m]]), str(context_prediction[m]))
        if (context_prediction[m] > PREDICTION_THRES and db > DBLEVEL_THRES):
            socketio.emit('audio_label',
                          {'label': str(homesounds.to_human_labels[active_context[m]]),
                           'accuracy': str(context_prediction[m])})
            print("Prediction: %s (%0.2f)" % (
                homesounds.to_human_labels[active_context[m]], context_prediction[m]))
    socket.emit('audio_label',
                {
                    'label': 'Unrecognized Sound',
                    'accuracy': '1.0'
                })


def background_thread():
    """Example of how to send server generated events to clients."""
    count = 0
    while True:
        socketio.sleep(10)
        count += 1
        socketio.emit('my_response',
                      {'data': 'Server generated event', 'count': count},
                      namespace='/test')


@app.route('/')
def index():
    return render_template('index.html',)

@socketio.on('send_message')
def handle_source(json_data):
    print('Receive message...' + str(json_data['message']))
    text = json_data['message'].encode('ascii', 'ignore')
    socketio.emit('echo', {'echo': 'Server Says: ' + str(text)})
    print('Sending message back..')

@socketio.on('disconnect_request', namespace='/test')
def disconnect_request():
    @copy_current_request_context
    def can_disconnect():
        disconnect()

    session['receive_count'] = session.get('receive_count', 0) + 1
    # for this emit we use a callback function
    # when the callback function is invoked we know that the message has been
    # received and it is safe to disconnect
    emit('my_response',
         {'data': 'Disconnected!', 'count': session['receive_count']},
         callback=can_disconnect)

@socketio.on('connect', namespace='/test')
def test_connect():
    global thread
    with thread_lock:
        if thread is None:
            thread = socketio.start_background_task(background_thread)
    emit('my_response', {'data': 'Connected', 'count': 0})


@socketio.on('disconnect', namespace='/test')
def test_disconnect():
    print('Client disconnected', request.sid)


if __name__ == '__main__':
    socketio.run(app, debug=True)