import os import time import smbus import csv import numpy as np import pandas as pd import warnings from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score from imusensor.MPU9250 import MPU9250 from imusensor.filters import kalman #from gpiozero import Buzzer warnings.filterwarnings('ignore') #buzzer = Buzzer(17) dfn = pd.read_csv("/home/pi/Desktop/sarsinti/data_normal.csv") dfn['result'] = 0 dfs = pd.read_csv("/home/pi/Desktop/sarsinti/data_sarsinti.csv") dfs['result'] = 1 df = dfn.merge(dfs, how='outer') dforj = df.copy() y = df['result'] df.drop('result', axis=1, inplace=True) X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.3, random_state=100) logreg = LogisticRegression(solver='liblinear', random_state = 0) logreg.fit(X_train, y_train) y_pred = logreg.predict(X_test) startscore = accuracy_score(y_test, y_pred) print("Start AccScore: {0:0.4f}".format(startscore)) address = 0x68 bus = smbus.SMBus(1) imu = MPU9250.MPU9250(bus, address) imu.begin() sensorfusion = kalman.Kalman() imu.readSensor() imu.computeOrientation() sensorfusion.roll = imu.roll sensorfusion.pitch = imu.pitch sensorfusion.yaw = imu.yaw count = 0 currTime = time.time() #dfnew = pd.DataFrame(columns=['roll','pitch','result']) while True: imu.readSensor() imu.computeOrientation() newTime = time.time() dt = newTime - currTime currTime = newTime sensorfusion.computeAndUpdateRollPitchYaw(imu.AccelVals[0], imu.AccelVals[1], imu.AccelVals[2], imu.GyroVals[0], imu.GyroVals[1], imu.GyroVals[2],\ imu.MagVals[0], imu.MagVals[1], imu.MagVals[2], dt) row = [sensorfusion.roll, sensorfusion.pitch] nprow = np.array([row]) rowresult = int(logreg.predict(nprow)) #if rowresult == 1: #buzzer.on() #time.sleep(0.01) #buzzer.off() print("Sarsıntı Var/Yok: ", rowresult) #dfrow = {'roll': sensorfusion.roll, 'pitch': sensorfusion.pitch, 'result': rowresult} #dfnew = pd.concat([dfnew, pd.DataFrame([dfrow])], ignore_index = True) #lendfnew = len(dfnew) #if lendfnew == 1000: #df = dforj.merge(dfnew, how='outer') #dfcsv = df.copy() #y = df['result'] #y = y.astype('int') #df.drop('result', axis=1, inplace=True) #X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.3, random_state=100) #logreg.fit(X_train, y_train) #y_pred = logreg.predict(X_test) #newscore = accuracy_score(y_test, y_pred) #if newscore > startscore: #print("New AccScore: {0:0.4f}".format(newscore)) #dfcsv.to_csv('/home/pi/Desktop/sarsinti/data.csv') #startscore = newscore #dfnew = pd.DataFrame(columns=['roll','pitch','result']) time.sleep(0.01)