JUNGU commited on
Commit
3deb8b3
·
1 Parent(s): e741394

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -108
app.py CHANGED
@@ -1,115 +1,68 @@
1
 
2
  import streamlit as st
3
  import pandas as pd
4
- import matplotlib.pyplot as plt
5
  import numpy as np
6
  from sklearn.linear_model import LinearRegression
7
  from sklearn.preprocessing import PolynomialFeatures
8
- from sklearn.pipeline import make_pipeline
9
- from sklearn.svm import SVR
10
- from sklearn.ensemble import RandomForestRegressor
11
-
12
- st.title("Webcam Color Detection Charting")
13
-
14
- uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
15
-
16
- time_frame_options = [
17
- "All",
18
- "1 second",
19
- "5 seconds",
20
- "10 seconds",
21
- "30 seconds",
22
- "1 minute",
23
- "5 minutes",
24
- "10 minutes",
25
- "30 minutes",
26
- "60 minutes",
27
- ]
28
- time_frame = st.selectbox("Data Time Frame", time_frame_options)
29
-
30
- regression_options = [
31
- "None",
32
- "Linear Regression",
33
- "Polynomial Regression",
34
- "SVR (Support Vector Regression)",
35
- "Random Forest Regression",
36
- ]
37
- regression_type = st.selectbox("Regression Analysis Type", regression_options)
38
-
39
- if uploaded_file is not None:
40
- # Read CSV file
41
- data = pd.read_csv(uploaded_file)
42
-
43
- # Filter data according to the time frame
44
- if time_frame != "All":
45
- seconds = {
46
- "1 second": 1,
47
- "5 seconds": 5,
48
- "10 seconds": 10,
49
- "30 seconds": 30,
50
- "1 minute": 60,
51
- "5 minutes": 300,
52
- "10 minutes": 600,
53
- "30 minutes": 1800,
54
- "60 minutes": 3600,
55
- }
56
- data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms')
57
- data.set_index('timestamp', inplace=True)
58
- data = data.resample(f"{seconds[time_frame]}S").mean().dropna().reset_index()
59
-
60
- # Create charts
61
- fig, axes = plt.subplots(2, 1, figsize=(10, 8))
62
-
63
- # RGB chart
64
- axes[0].plot(data['R'], 'r', label='R')
65
- axes[0].plot(data['G'], 'g', label='G')
66
- axes[0].plot(data['B'], 'b', label='B')
67
-
68
- # HSV chart
69
- axes[1].plot(data['H'], 'r', label='H')
70
- axes[1].plot(data['S'], 'g', label='S')
71
- axes[1].plot(data['V'], 'b', label='V')
72
-
73
- axes[0].legend(loc='upper right')
74
- axes[0].set_title('RGB Values')
75
- axes[1].legend(loc='upper right')
76
- axes[1].set_title('HSV Values')
77
-
78
- # Perform regression analysis if selected
79
- if regression_type != "None":
80
- X = np.arange(len(data)).reshape(-1, 1)
81
-
82
- # Linear Regression
83
- if regression_type == "Linear Regression":
84
- model = LinearRegression()
85
- for color, code in zip(['R', 'G', 'B'], ['r', 'g', 'b']):
86
- model.fit(X, data[color])
87
- axes[0].plot(X, model.predict(X), f'{code}--')
88
- st.write(f"{color}: y = {model.coef_[0]} * x + {model.intercept_}")
89
-
90
- # Polynomial Regression
91
- elif regression_type == "Polynomial Regression":
92
- polynomial_features = PolynomialFeatures(degree=2)
93
- model = make_pipeline(polynomial_features, LinearRegression())
94
- for color, code in zip(['R', 'G', 'B'], ['r', 'g', 'b']):
95
- model.fit(X, data[color])
96
- axes[0].plot(X, model.predict(X), f'{code}--')
97
- st.write("Polynomial regression equation is not easily representable.")
98
-
99
- # SVR (Support Vector Regression)
100
- elif regression_type == "SVR (Support Vector Regression)":
101
- model = SVR()
102
- for color, code in zip(['R', 'G', 'B'], ['r', 'g', 'b']):
103
- model.fit(X, data[color])
104
- axes[0].plot(X, model.predict(X), f'{code}--')
105
- st.write("SVR equation is not easily representable.")
106
-
107
- # Random Forest Regression
108
- elif regression_type == "Random Forest Regression":
109
- model = RandomForestRegressor()
110
- for color, code in zip(['R', 'G', 'B'], ['r', 'g', 'b']):
111
- model.fit(X, data[color])
112
- axes[0].plot(X, model.predict(X), f'{code}--')
113
- st.write("Random Forest equation is not easily representable.")
114
 
115
- st.pyplot(fig)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
  import streamlit as st
3
  import pandas as pd
 
4
  import numpy as np
5
  from sklearn.linear_model import LinearRegression
6
  from sklearn.preprocessing import PolynomialFeatures
7
+ import matplotlib.pyplot as plt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
+ def load_data(file):
10
+ return pd.read_csv(file)
11
+
12
+ def plot_data(data, x_values, y_values, model=None, prediction=None):
13
+ plt.scatter(x_values, y_values, label='Data')
14
+ if model is not None and prediction is not None:
15
+ plt.plot(x_values, prediction, color='red', label='Model')
16
+ plt.xlabel('Index')
17
+ plt.ylabel('Value')
18
+ plt.legend()
19
+ plt.show()
20
+
21
+ def fit_model(data, model_type, x_values, y_values):
22
+ if model_type == 'Linear Regression':
23
+ model = LinearRegression()
24
+ x_values = x_values.reshape(-1, 1)
25
+ model.fit(x_values, y_values)
26
+ prediction = model.predict(x_values)
27
+ equation = f'y = {{model.coef_[0]:.4f}}x + {{model.intercept_:.4f}}'
28
+ elif model_type == 'Polynomial Regression':
29
+ polynomial_features = PolynomialFeatures(degree=2)
30
+ x_values_poly = polynomial_features.fit_transform(x_values.reshape(-1, 1))
31
+ model = LinearRegression()
32
+ model.fit(x_values_poly, y_values)
33
+ prediction = model.predict(x_values_poly)
34
+ equation = 'Polynomial equation (degree 2)'
35
+ else:
36
+ model = None
37
+ prediction = None
38
+ equation = "No model selected"
39
+ return model, prediction, equation
40
+
41
+ def app():
42
+ st.title('RGB and HSV Analysis and Prediction')
43
+ uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
44
+
45
+ if uploaded_file is not None:
46
+ data = load_data(uploaded_file)
47
+ st.dataframe(data.head())
48
+
49
+ # Selecting R, G, B, H, S, V
50
+ color_component = st.selectbox("Select color component", ['R', 'G', 'B', 'H', 'S', 'V'])
51
+ st.write(f"Selected component: {{color_component}}")
52
+ selected_data = data[color_component].values
53
+
54
+ # Selecting regression model
55
+ regression_model = st.selectbox("Select a regression model", ["None", "Linear Regression", "Polynomial Regression"])
56
+
57
+ x_values = np.arange(len(selected_data))
58
+ y_values = selected_data
59
+
60
+ # Fitting the selected model
61
+ model, prediction, equation = fit_model(data, regression_model, x_values, y_values)
62
+ st.write(f"Equation: {{equation}}")
63
+
64
+ # Plotting the data and model
65
+ plot_data(data, x_values, y_values, model, prediction)
66
+
67
+ # Running the app (uncomment this line to run the app locally)
68
+ # app()