forked from itcentralng/fire-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
122 lines (99 loc) · 3.56 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import serial
import streamlit as st
from joblib import load
import time
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import ast
model = load('model.joblib')
import warnings
warnings.filterwarnings("ignore")
plt.rcParams.update({'font.size': 5})
field = st.empty()
ser = serial.Serial()
ser.setPort("COM9")
monitor = st.button("Monitor")
if "port_state" not in st.session_state:
st.session_state["port_state"] = False
if monitor:
#if st.session_state.port_state:
#ser.close()
#st.session_state.port_state = False
#global RUN
#RUN = False
curr_time = time.strftime("%H:%M:%S ", time.localtime())
print("Point 1")
with st.spinner("Loading..."):
with open('log.txt', 'r') as f:
reading = ast.literal_eval(f.readlines()[-1])
LPG, CO, Smoke, Temperature, Humidity = reading[1:]
st.markdown(f"#### ⌚{curr_time} `LPG` {LPG} `CO` {CO} `Smoke` {Smoke} `Temp` {Temperature} `Humidity` {Humidity} ")
details = st.expander("See full report")
print("Point 2")
LPGs = []
COs = []
Smokes = []
Temperatures = []
Humiditys = []
print("Point 3")
with open('log.txt', 'r') as f:
readings = f.readlines()[-20:]
for reading in readings:
reading = ast.literal_eval(reading.strip())
LPGs.append(reading[1])
COs.append(reading[2])
Smokes.append(reading[3])
Temperatures.append(reading[4])
Humiditys.append(reading[5])
fig, axis = plt.subplots(2, 3)
axis[0, 0].plot(LPGs, color = 'g')
axis[0, 0].title.set_text("LPG")
axis[0, 0].set_ylim(0, max(LPGs)+1)
axis[0, 0].get_xaxis().set_visible(False)
axis[0, 1].plot(COs, color = 'g')
axis[0, 1].title.set_text("CO")
axis[0, 1].set_ylim(0, max(COs)+1)
axis[0, 1].get_xaxis().set_visible(False)
axis[0, 2].plot(Smokes, color = 'g')
axis[0, 2].title.set_text("Smoke")
axis[0, 2].set_ylim(0, max(Smokes)+1)
axis[0, 2].get_xaxis().set_visible(False)
axis[1, 0].plot(Temperatures, color = 'g')
axis[1, 0].title.set_text("Temperature")
axis[1, 0].set_ylim(0, max(Temperatures)+1)
axis[1, 0].get_xaxis().set_visible(False)
axis[1, 1].plot(Humiditys, color = 'g')
axis[1, 1].title.set_text("Humidity")
axis[1, 1].set_ylim(0, max(Humiditys)+1)
axis[1, 1].get_xaxis().set_visible(False)
axis[1, 2].set_visible(False)
print("Point 4")
with details:
st.pyplot(fig)
print("Point 5")
RUN = True
while RUN:
#if st.session_state.port_state:
# ser.close()
#if st.session_state.port_state == False:
# ser.open()
# time.sleep(1)
# st.session_state.port_state = True
#output = ser.readline()
#decoded_out = output.decode()
#stripped_out = decoded_out.rstrip()
#splitted_out = stripped_out.split()
splitted_out = list(range(10))
if len(splitted_out) == 10:
features = splitted_out[0:len(splitted_out):2]
values = splitted_out[1:len(splitted_out):2]
values = list(map(float, values))
if model.predict([values]).item() == 0:
cond = "## Everything looks good ✅"
else: cond = "## Fire Detected ⚠️"
with field.container():
st.markdown(cond)
curr_time = time.strftime("%H:%M:%S", time.localtime())
with open('log.txt', 'a') as f:
f.write(f"{curr_time, *values}\n")