-
-
Save dj1711572002/fd62c6ed9f0a19ebd42a01c8cf4d0b27 to your computer and use it in GitHub Desktop.
PyMahouAcc Python IMU Acc gesture Model Creating and Testing using I2C BNO055 Acc
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 # 学習用 | |
length = 20 #ジェスチャの長さ(連続で受信するデータの個数) | |
gestures = 3 #ジェスチャの種類の数 | |
times = 1 #各ジェスチャを行う回数 | |
#==================================================== | |
now = datetime.datetime.now() | |
name = now.strftime('%Y%m%d_%H%M%S') | |
filename = 'model_' + name + '.sav' # 入力した名前の学習モデルのファイル名 | |
print('Connecting...') | |
ser = serial.Serial('COM19',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 = [] | |
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 | |
m = mx + my + mz #三軸の加速度を横一列にする | |
X.append(m) #学習データ用リストへ追加 | |
print(X) | |
Y.append(str(gesture_num)) #学習ラベル用リストへ追加 | |
#print("serial closed") | |
#ser.close() #シリアルを閉じる | |
model = SVC(kernel = 'linear', C=1, gamma=1) #学習モデルのパラメータを指定 | |
model.fit(X,np.ravel(Y)) #学習を行う | |
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!') | |
#ser.close() # シリアルを閉じる |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment