How do I make machine learning predictions using serial values from Arduino?

Viewed 37

I'm working on a project to create sign language recognition gloves that detect gestures and play corresponding audio outputs. from online tutorials, I have coded a basic decision tree classifier model in python and used my own collected dataset to make predictions, here is that code:

import pandas as pd 
data=pd.read_csv('mydata.csv') 
X = data.drop(columns=['Word'])
X = X.values
y = data['Word'] 
from sklearn.tree import DecisionTreeClassifier 
model = DecisionTreeClassifier()
model.fit(X,y)
predictions = model.predict([[-3.18,-8.92,-2.27,227,303,254,297,169]]) ##Sensor values from collected dataset
predictions 

This code works well and makes predictions accurately. And on the other hand, I followed another tutorial to interface Arduino with Python using PySerial and bring in the live values of the serial monitor into the Python

import time
import serial

arduinoData=serial.Serial('com6',115200)
time.sleep(1)

while True:
    while(arduinoData.inWaiting()==0):
        pass
    dataPacket=arduinoData.readline()
    dataPacket=str(dataPacket,'utf-8')
    dataPacket=dataPacket.strip('\r\n')
    splitPacket=dataPacket.split(",")
    X=float(splitPacket[0])
    Y=float(splitPacket[1])
    Z=float(splitPacket[2])
    T=float(splitPacket[3])
    I=float(splitPacket[4])
    M=float(splitPacket[5])
    R=float(splitPacket[6])
    P=float(splitPacket[7])
    print(X,Y,Z,T,I,M,R,P)
-0.72,-1.33,10.72,234,238,199,332,176

How do I make live predictions from the ML model code mentioned earlier using (X,Y,Z,T,I,M,R,P) values that are received from Python? So when I perform a gesture and if the sensor values match the prediction criteria, then it should print the 'predictions' variable.

1 Answers

You are almost there! I would approach the problem in to steps:

  1. Create/Train/Save the ML Mode:

You already have the code to create and train the ML model, you just need to save it to a file:

# open a file, where you ant to store the data
with open('important', 'wb') as file:
    # dump information to that file
    pickle.dump(model, file)

https://scikit-learn.org/stable/model_persistence.html

  1. Use the model to make predictions:

Now, load the model in your second python code:

with open('important', 'rb') as file:
    model = pickle.load(file)

and make a prediction with the values from arduino:

model.predict([[X,Y,Z,T,I,M,R,P]])

Full code:

import time
import serial
import pickle 

arduinoData=serial.Serial('com6',115200)
time.sleep(1)

with open('important', 'rb') as file:
    model = pickle.load(file)


while True:
    while(arduinoData.inWaiting()==0):
        pass
    dataPacket=arduinoData.readline()
    dataPacket=str(dataPacket,'utf-8')
    dataPacket=dataPacket.strip('\r\n')
    splitPacket=dataPacket.split(",")
    X=float(splitPacket[0])
    Y=float(splitPacket[1])
    Z=float(splitPacket[2])
    T=float(splitPacket[3])
    I=float(splitPacket[4])
    M=float(splitPacket[5])
    R=float(splitPacket[6])
    P=float(splitPacket[7])
    prediction = model.predict([[X,Y,Z,T,I,M,R,P]])
    print(prediction)
Related