Muthuraja18 commited on
Commit
ad637f5
·
verified ·
1 Parent(s): 98639b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -34
app.py CHANGED
@@ -1,13 +1,95 @@
1
- import streamlit as st
2
  import serial
 
3
  import time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- # Function to initialize serial connection
 
 
 
 
 
 
 
6
  def initialize_serial_connection():
7
- com_port = predict_serial_port(collect_serial_ports()) # Get the correct port from the AI model
 
 
8
  if com_port:
9
  try:
10
- ser = serial.Serial(com_port, 9600)
11
  time.sleep(2) # Wait for the connection to initialize
12
  print(f"Connected to {com_port}")
13
  return ser
@@ -21,44 +103,53 @@ def initialize_serial_connection():
21
  # Initialize serial connection
22
  ser = initialize_serial_connection()
23
 
24
- # Function to update bus information
25
  def display_bus_info():
26
- bus_info = {
27
- 'bus_name': 'N/A',
28
- 'seat_status': 'N/A',
29
- 'distance': 'N/A',
30
- 'led_status': 'OFF',
31
- 'buzzer_status': 'OFF',
32
- }
33
-
34
- while True:
35
- if ser.in_waiting > 0:
36
- # Read data from serial port
37
- data = ser.readline().decode('utf-8', errors='ignore').strip()
38
-
39
- # Process the incoming data
40
- if "Bus Name:" in data:
41
- bus_info['bus_name'] = data.split("Bus Name:")[1].strip()
42
- elif "Seat Status:" in data:
43
- bus_info['seat_status'] = data.split("Seat Status:")[1].strip()
44
- elif "Distance:" in data:
45
- bus_info['distance'] = data.split("Distance:")[1].strip()
46
- elif "LED:" in data:
47
- bus_info['led_status'] = data.split("LED:")[1].strip()
48
- elif "Buzzer:" in data:
49
- bus_info['buzzer_status'] = data.split("Buzzer:")[1].strip()
50
-
51
- time.sleep(1) # Delay to avoid flooding the interface
52
-
53
- return bus_info
 
 
 
 
 
 
54
 
55
  # Streamlit Web Interface
56
  st.title('Bus System Status')
57
 
 
58
  col1, col2 = st.columns(2)
 
59
  with col1:
60
  st.subheader('Bus Name')
61
  bus_name = st.empty()
 
62
  with col2:
63
  st.subheader('Seat Status')
64
  seat_status = st.empty()
@@ -75,7 +166,6 @@ buzzer_status_display = st.empty()
75
 
76
  # Update the information on the web interface every second
77
  while True:
78
- bus_info = display_bus_info()
79
  bus_name.text(bus_info['bus_name'])
80
  seat_status.text(bus_info['seat_status'])
81
  distance_display.text(f"Distance: {bus_info['distance']} cm")
 
 
1
  import serial
2
+ import serial.tools.list_ports
3
  import time
4
+ import streamlit as st
5
+ import threading
6
+ import pickle
7
+ from sklearn.tree import DecisionTreeClassifier
8
+ from sklearn.preprocessing import LabelEncoder
9
+ import pandas as pd
10
+
11
+ # Global variables to store bus information and hardware statuses
12
+ bus_info = {
13
+ 'bus_name': 'N/A',
14
+ 'seat_status': 'N/A',
15
+ 'distance': 'N/A',
16
+ 'led_status': 'OFF',
17
+ 'buzzer_status': 'OFF',
18
+ }
19
+
20
+ # Function to collect available serial ports data
21
+ def collect_serial_ports():
22
+ ports = serial.tools.list_ports.comports()
23
+ port_data = []
24
+ for port in ports:
25
+ data = {
26
+ 'port': port.device,
27
+ 'description': port.description,
28
+ 'manufacturer': port.manufacturer if port.manufacturer else 'Unknown'
29
+ }
30
+ port_data.append(data)
31
+ return port_data
32
+
33
+ # Function to preprocess and label the data for model training
34
+ def preprocess_data(port_data):
35
+ df = pd.DataFrame(port_data)
36
+
37
+ # Encode the 'description' and 'manufacturer' fields to numeric
38
+ le = LabelEncoder()
39
+ df['description'] = le.fit_transform(df['description'])
40
+ df['manufacturer'] = le.fit_transform(df['manufacturer'])
41
+
42
+ return df, le
43
+
44
+ # Train a simple decision tree classifier (this is just an example)
45
+ def train_model():
46
+ # Example: manually labeled data where '1' represents Arduino and '0' is other devices
47
+ port_data = collect_serial_ports()
48
+
49
+ df, le = preprocess_data(port_data)
50
+ df['label'] = [1, 0] # 1 = Arduino, 0 = Other device (Replace with actual labeled data)
51
+
52
+ # Train the classifier
53
+ X = df.drop(columns=['port', 'label'])
54
+ y = df['label']
55
+ clf = DecisionTreeClassifier()
56
+ clf.fit(X, y)
57
+
58
+ # Save the model and label encoder
59
+ with open('serial_port_model.pkl', 'wb') as model_file:
60
+ pickle.dump(clf, model_file)
61
+ with open('label_encoder.pkl', 'wb') as le_file:
62
+ pickle.dump(le, le_file)
63
+
64
+ # Function to predict the serial port based on the AI model
65
+ def predict_serial_port(port_data):
66
+ # Load the trained model and label encoder
67
+ with open('serial_port_model.pkl', 'rb') as model_file:
68
+ clf = pickle.load(model_file)
69
+ with open('label_encoder.pkl', 'rb') as le_file:
70
+ le = pickle.load(le_file)
71
+
72
+ # Preprocess the data
73
+ df, _ = preprocess_data(port_data)
74
+
75
+ # Predict the label for the serial ports (Arduino or not)
76
+ prediction = clf.predict(df.drop(columns=['port', 'label']))
77
 
78
+ # Return the port if it's predicted to be an Arduino (label = 1)
79
+ for i, pred in enumerate(prediction):
80
+ if pred == 1: # If prediction is 1, it's the Arduino
81
+ return port_data[i]['port']
82
+
83
+ return None # No Arduino found
84
+
85
+ # Function to initialize the serial connection with the correct port
86
  def initialize_serial_connection():
87
+ port_data = collect_serial_ports() # Get available ports data
88
+ com_port = predict_serial_port(port_data) # Get the correct port from the AI model
89
+
90
  if com_port:
91
  try:
92
+ ser = serial.Serial(com_port, 9600) # Open serial port
93
  time.sleep(2) # Wait for the connection to initialize
94
  print(f"Connected to {com_port}")
95
  return ser
 
103
  # Initialize serial connection
104
  ser = initialize_serial_connection()
105
 
106
+ # Function to update bus information based on received serial data
107
  def display_bus_info():
108
+ global bus_info
109
+ try:
110
+ while True:
111
+ if ser.in_waiting > 0:
112
+ # Read data from serial port
113
+ data = ser.readline().decode('utf-8', errors='ignore').strip()
114
+
115
+ # Debug print to see the raw serial data
116
+ print(f"Raw Data: {data}")
117
+
118
+ if "Bus Name:" in data:
119
+ bus_info['bus_name'] = data.split("Bus Name:")[1].strip()
120
+
121
+ elif "Seat Status:" in data:
122
+ bus_info['seat_status'] = data.split("Seat Status:")[1].strip()
123
+
124
+ elif "Distance:" in data:
125
+ bus_info['distance'] = data.split("Distance:")[1].strip()
126
+
127
+ elif "LED:" in data:
128
+ bus_info['led_status'] = data.split("LED:")[1].strip()
129
+
130
+ elif "Buzzer:" in data:
131
+ bus_info['buzzer_status'] = data.split("Buzzer:")[1].strip()
132
+
133
+ time.sleep(1) # Small delay to prevent flooding the terminal
134
+
135
+ except KeyboardInterrupt:
136
+ print("Exiting...")
137
+
138
+ # Start the bus information display in a separate thread
139
+ bus_info_thread = threading.Thread(target=display_bus_info)
140
+ bus_info_thread.daemon = True
141
+ bus_info_thread.start()
142
 
143
  # Streamlit Web Interface
144
  st.title('Bus System Status')
145
 
146
+ # Display the bus information
147
  col1, col2 = st.columns(2)
148
+
149
  with col1:
150
  st.subheader('Bus Name')
151
  bus_name = st.empty()
152
+
153
  with col2:
154
  st.subheader('Seat Status')
155
  seat_status = st.empty()
 
166
 
167
  # Update the information on the web interface every second
168
  while True:
 
169
  bus_name.text(bus_info['bus_name'])
170
  seat_status.text(bus_info['seat_status'])
171
  distance_display.text(f"Distance: {bus_info['distance']} cm")