-
-
Save dj1711572002/ce3b42b50ef9cc92c5b1f330110f6bb4 to your computer and use it in GitHub Desktop.
py_seaborn_IMU IMU data serial reciving and Plot Scatter seaborn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding utf-8 | |
import datetime | |
import pickle # モデルをファイルとして使用する用 | |
import keyboard | |
import numpy as np # numpyで2次元配列を扱うため | |
import serial # シリアル通信を行うため | |
from sklearn.svm import SVC # 学習用 | |
import csv | |
import os | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
import seaborn as sns | |
path = os.getcwd() | |
print(path) | |
print(type(path)) | |
#now = datetime.datetime.now() | |
#filename =now.strftime('%Y%m%d_%H%M%S') + '.csv' | |
filenamei ='imu.csv' | |
length = 20 #ジェスチャの長さ(連続で受信するデータの個数) | |
gestures = 3 #ジェスチャの種類の数 | |
times = 1 #各ジェスチャを行う回数 | |
#==================================================== | |
now = datetime.datetime.now() | |
name = now.strftime('%Y%m%d_%H%M%S') | |
#filename = 'model_' + name + '.sav' # 入力した名前の学習モデルのファイル名 | |
filename = 'model_' + 'test' + '.sav' # 入力した名前の学習モデルのファイル名 | |
print('Connecting...') | |
ser = serial.Serial('COM17',115200,timeout=None) #ポート番号19からシリアル受信 | |
while True: | |
print("-----Input c:CreateModel,g:Gesture Measuring +ENTER") | |
keys=input() | |
if keys=='c': | |
#------------------------Create Model BLOCK-------------------- | |
print("File name="+filename) | |
X = [] | |
Y = [] | |
with open(filenamei, 'w', newline='') as f: | |
writer = csv.writer(f) | |
for i in range(gestures * times): | |
cnt = 0 #受信したデータのカウント用 | |
mx = [] #各軸ごとの配列を初期化 | |
my = [] | |
mz = [] | |
gesture_num = i % gestures + 1 #ジェスチャ番号を指定 | |
print('Push a then Do gesture No.', gesture_num) | |
while True: | |
line=ser.readline()#Binary | |
linestr= line.decode('utf8') | |
#print(linestr) | |
strarr=linestr.split(',') | |
x=strarr[1] | |
y=strarr[2] | |
z=strarr[3] | |
switch='' | |
if keyboard.is_pressed('a'): | |
print("key hit") | |
switch = "a" | |
if switch =='a' and cnt == 0: #クリックされたらカウント開始 | |
print("cnt=1") | |
cnt = 1 | |
if cnt != 0: #ジェスチャ中a | |
print("In gesture cnt="+str(cnt)) | |
mx.append(x.rstrip('\r\n')) # シリアルから受け取ってたデータのゴミを取って配列へ追加 | |
my.append(y.rstrip('\r\n')) | |
mz.append(z.rstrip('\r\n')) | |
cnt += 1 | |
if cnt == length: #lengthまでいったら次のジェスチャ待ちへ | |
print("length Up") | |
break | |
wr = [gesture_num,x, y, z] | |
writer.writerow(wr) | |
m = mx + my + mz #三軸の加速度を横一列にする | |
X.append(m) #学習データ用リストへ追加 | |
print(X) | |
Y.append(str(gesture_num)) #学習ラベル用リストへ追加 | |
#print("serial closed") | |
#ser.close() #シリアルを閉じる | |
#print(m) | |
model = SVC(kernel = 'linear', C=1, gamma=1) #学習モデルのパラメータを指定 | |
model.fit(X,np.ravel(Y)) #学習を行う | |
# CSV読み込みして散布図プロット | |
#df = pd.read_csv("imu.csv", sep=",", header=None, names=['gnum', 'gyX', 'gyY', 'gyZ']) | |
df = pd.read_csv("imu.csv", sep=",", header=None, names=['gnum', 'acX', 'acY', 'acZ']) | |
print(df) | |
print(len(df.columns)) | |
print(df.info()) | |
sns.pairplot(df, hue='gnum') | |
plt.tight_layout() | |
plt.show() | |
#df = pd.DataFrame(model) | |
#sns.pairplot(df, height=1.5) | |
with open(filename, 'wb') as fp_model: #学習モデルを保存するためのファイルを開く | |
pickle.dump(model, fp_model) #モデルを保存 | |
print('Model created.') | |
#-----------------------------start gesture measuring----------------------------------------------- | |
elif keys=='g': | |
length = 20 # ジェスチャの長さ(連続で受信するデータの個数) | |
print('-----------Start Gesture Measuring------------') | |
#ser = serial.Serial('COM19', 115200, timeout=None) # ポート番号19からシリアル受信 | |
#print('Input your name:') | |
#name = input() | |
#filename = 'models/model_' + name + '.sav' # 入力した名前の学習モデルのファイル名 | |
print('Loading...') | |
with open(filename, 'rb') as fp_model: # 学習モデルファイルを開く | |
loaded_model = pickle.load(fp_model) # モデルをロード | |
print('Do any gesture!') | |
try: | |
while True: | |
cnt = 0 # 受信したデータのカウント用 | |
mx = [] # 各軸ごとの配列を初期化 | |
my = [] | |
mz = [] | |
print("push a measuring start") | |
while True: | |
line = ser.readline() # Binary | |
linestr = line.decode('utf8') | |
# print(linestr) | |
strarr = linestr.split(',') | |
x = strarr[1] | |
y = strarr[2] | |
z = strarr[3] | |
switch='' | |
if keyboard.is_pressed('a'): | |
#print("key hit") | |
switch = "a" | |
if switch == 'a' and cnt == 0: # クリックされたらカウント開始 | |
print("gesture cnt=1") | |
cnt = 1 | |
if cnt != 0: # ジェスチャ中 | |
mx.append(x.rstrip('\r\n')) # シリアルから受け取ってたデータのゴミを取って配列へ追加 | |
my.append(y.rstrip('\r\n')) | |
mz.append(z.rstrip('\r\n')) | |
cnt += 1 | |
if cnt == length: # lengthまでいったら次のジェスチャ待ちへ | |
break | |
m = mx + my + mz # 三軸の加速度を横一列にする | |
m2 = [] # リスト型にするため | |
m2.append(m) # リストへ追加 | |
pre = loaded_model.predict(m2) # ジェスチャの判定 | |
print('This gesture is No.', pre[0][0]) | |
except KeyboardInterrupt: # Ctrl-C を捕まえたら終了 | |
print('Close!') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment