Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,12 @@
|
|
2 |
import streamlit as st
|
3 |
import pandas as pd
|
4 |
import matplotlib.pyplot as plt
|
5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
st.title("Webcam Color Detection Charting")
|
8 |
|
@@ -22,11 +27,20 @@ time_frame_options = [
|
|
22 |
]
|
23 |
time_frame = st.selectbox("Data Time Frame", time_frame_options)
|
24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
if uploaded_file is not None:
|
26 |
-
# CSV
|
27 |
data = pd.read_csv(uploaded_file)
|
28 |
|
29 |
-
#
|
30 |
if time_frame != "All":
|
31 |
seconds = {
|
32 |
"1 second": 1,
|
@@ -43,21 +57,59 @@ if uploaded_file is not None:
|
|
43 |
data.set_index('timestamp', inplace=True)
|
44 |
data = data.resample(f"{seconds[time_frame]}S").mean().dropna().reset_index()
|
45 |
|
46 |
-
#
|
47 |
fig, axes = plt.subplots(2, 1, figsize=(10, 8))
|
48 |
|
49 |
-
# RGB
|
50 |
axes[0].plot(data['R'], 'r', label='R')
|
51 |
axes[0].plot(data['G'], 'g', label='G')
|
52 |
axes[0].plot(data['B'], 'b', label='B')
|
53 |
-
axes[0].legend(loc='upper right')
|
54 |
-
axes[0].set_title('RGB Values')
|
55 |
|
56 |
-
# HSV
|
57 |
axes[1].plot(data['H'], 'r', label='H')
|
58 |
axes[1].plot(data['S'], 'g', label='S')
|
59 |
axes[1].plot(data['V'], 'b', label='V')
|
|
|
|
|
|
|
60 |
axes[1].legend(loc='upper right')
|
61 |
axes[1].set_title('HSV Values')
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
st.pyplot(fig)
|
|
|
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 |
|
|
|
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,
|
|
|
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)
|