from pandas import read_csv
from sklearn.preprocessing import OneHotEncoder
from tensorflow.keras.models import load_model
import numpy as np
import pickle
import tkinter as tk
from tkinter import messagebox
class predict_gui():
def predict_all(self, inputVecs):
testresult =
for vec in inputVecs:
pred = self.predict(vec)
testresult.append(pred)
return testresult
def predict(self, inputVec):
inputVec = self.StdScales.transform([inputVec])
pred = self.best_model.predict_classes(inputVec)
pred = self.ohe.inverse_transform(pred)
pred = list(np.reshape(pred, (1,)))
return pred
def __init__(self):
with open('StdScales', 'rb') as f:
self.StdScales = pickle.load(f)
self.best_model = load_model('bestmodel.h5')
self.ohe = OneHotEncoder(drop='first')
self.ohe.fit([["No Rain"], ["Rain"]])
self.MainMenu = tk.Tk()
self.MainMenu.title("Main Menu")
self.MainMenu.protocol("WM_DELETE_WINDOW",self.quit_window)
tk.Label(self.MainMenu, text="Temperature").grid(
row=0,column=0,padx = 20 ,pady=20, sticky='W')
tk.Label(self.MainMenu, text="Humidity").grid(
row=1,column=0,padx = 20 ,pady=20, sticky='W')
tk.Label(self.MainMenu, text="Wind Speed").grid(
row=2,column=0,padx = 20 ,pady=20, sticky='W')
self.TempEntry = tk.Entry(self.MainMenu, font = "Times 14", width = 12)
self.TempEntry.grid(row=0,column=1,padx = 20 ,pady=20, sticky='W')
self.HumidEntry = tk.Entry(self.MainMenu, font = "Times 14", width = 12)
self.HumidEntry.grid(row=1,column=1,padx = 20 ,pady=20, sticky='W')
self.WindSpdEntry = tk.Entry(self.MainMenu, font = "Times 14", width = 12)
self.WindSpdEntry.grid(row=2,column=1,padx = 20 ,pady=20, sticky='W')
tk.Label(self.MainMenu, text="Predict :", font = "Times 18 bold").grid(
row=3,column=0,padx = 20 ,pady=20, sticky='W')
self.PredictEntry = tk.Entry(self.MainMenu, font = "Times 18 bold", width = 8, justify='center')
self.PredictEntry.grid(row=3,column=1,padx = 20 ,pady=20)
self.PredictEntry.configure(state='disabled')
tk.Button(self.MainMenu, text='Send', font = "Times 14", command=self.send_to_node_red, width = 10, height = 3).grid(
row=3,column=2,padx=20, pady=20, sticky='W')
self.MainMenu.mainloop()
def quit_window(self): # Exit confirmation window
if messagebox.askokcancel("Quit", "Do you want to quit?"):
self.MainMenu.destroy()
self.ExitStatus = True
def send_to_node_red(self):
Datasample = [self.TempEntry.get(), self.HumidEntry.get(), self.WindSpdEntry.get()]
print(Datasample)
result = self.predict(Datasample)
print(result[0])
self.PredictEntry.configure(state="normal")
if self.PredictEntry.get() != "":
self.PredictEntry.delete(0,"end")
self.PredictEntry.insert(0, result[0])
self.PredictEntry.configure(state="readonly")
# Send result[0] to your node red
if name == "main":
GUI = predict_gui()