Demo entry 6359817



Submitted by anonymous on Apr 29, 2017 at 21:30
Language: Python. Code size: 2.0 kB.

import serial
import numpy as np
import time
from keras.models import model_from_json
from scipy.signal import square, sawtooth
import numpy as np
from scipy.fftpack import fft
from keras.utils import np_utils 
from keras.models import Sequential 
from keras.layers import Dense, Activation, Dropout 
from sklearn.cross_validation import train_test_split 
import matplotlib.pyplot as plt

def get_data(port, n=100):
    n = int(n)
    out = [None] * n
    t = list()
    ser = serial.Serial(port, 9600)
    start = time.time()
    for i in range(n):
        cont = False
        while cont is False:
            df = ser.readline().decode().strip('\r\n')
                cont = True
            except ValueError:
                cont = False
        out[i] = df
        t.append(start - time.time())
    return t, out

def load_model(model):
    json_file = open('{}.json'.format(model), 'r')
    loaded_model_json =
    loaded_model = model_from_json(loaded_model_json)
    loaded_model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return loaded_model

def classify(model, data):
    return model.predict_proba(data)

def convert_to_fft(data, s=500, N=1000):
    dt = (max(data[0])-min(data[0]))/len(data[0])

    yf = fft(data[1])
    tf = np.linspace(.0, 1./(2.*dt), N/2)
    plt.plot(yf, yf)
    spectrum = 2./N * np.abs(yf[0:N//2]) 
    return tf, spectrum

def convert_to_model_form(data, N=1000):
    return np.array(data[1]).reshape(1, len(data[0]))

def cli():
    numpoints = input('How Many Points: ')
    data = get_data('/dev/cu.usbmodem1411', n=numpoints)
    data = convert_to_fft(data)
    data = convert_to_model_form(data)
    return classify(load_model('4Class'), data=data)

if __name__ == '__main__':

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).