charting / app.py
JUNGU's picture
Update app.py
ffdb35d
raw
history blame
4.15 kB
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
from sklearn.svm import SVR
from sklearn.ensemble import RandomForestRegressor
st.title("Webcam Color Detection Charting")
uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
time_frame_options = [
"All",
"1 second",
"5 seconds",
"10 seconds",
"30 seconds",
"1 minute",
"5 minutes",
"10 minutes",
"30 minutes",
"60 minutes",
]
time_frame = st.selectbox("Data Time Frame", time_frame_options)
regression_options = [
"None",
"Linear Regression",
"Polynomial Regression",
"SVR (Support Vector Regression)",
"Random Forest Regression",
]
regression_type = st.selectbox("Regression Analysis Type", regression_options)
if uploaded_file is not None:
# Read CSV file
data = pd.read_csv(uploaded_file)
# Filter data according to the time frame
if time_frame != "All":
seconds = {
"1 second": 1,
"5 seconds": 5,
"10 seconds": 10,
"30 seconds": 30,
"1 minute": 60,
"5 minutes": 300,
"10 minutes": 600,
"30 minutes": 1800,
"60 minutes": 3600,
}
data['timestamp'] = pd.to_datetime(data['timestamp'], unit='ms')
data.set_index('timestamp', inplace=True)
data = data.resample(f"{seconds[time_frame]}S").mean().dropna().reset_index()
# Create charts
fig, axes = plt.subplots(2, 1, figsize=(10, 8))
# RGB chart
color_space_options = ["RGB", "HSV"]
color_space = st.selectbox("Select Color Space", color_space_options)
# Depending on the selection, plot the corresponding values
if color_space == "RGB":
axes[0].plot(data['R'], 'r', label='R')
axes[0].plot(data['G'], 'g', label='G')
axes[0].plot(data['B'], 'b', label='B')
elif color_space == "HSV":
axes[0].plot(data['H'], 'r', label='H')
axes[0].plot(data['S'], 'g', label='S')
axes[0].plot(data['V'], 'b', label='V')
axes[0].legend(loc='upper right')
axes[0].legend(loc='upper right')
axes[0].set_title('RGB Values')
axes[1].legend(loc='upper right')
axes[1].set_title('HSV Values')
# Perform regression analysis if selected
if regression_type != "None":
X = np.arange(len(data)).reshape(-1, 1)
# Linear Regression
if regression_type == "Linear Regression":
model = LinearRegression()
for color, code in zip(['R', 'G', 'B'], ['r', 'g', 'b']):
model.fit(X, data[color])
axes[0].plot(X, model.predict(X), f'{code}--')
st.write(f"{color}: y = {model.coef_[0]} * x + {model.intercept_}")
# Polynomial Regression
elif regression_type == "Polynomial Regression":
polynomial_features = PolynomialFeatures(degree=2)
model = make_pipeline(polynomial_features, LinearRegression())
for color, code in zip(['R', 'G', 'B'], ['r', 'g', 'b']):
model.fit(X, data[color])
axes[0].plot(X, model.predict(X), f'{code}--')
st.write("Polynomial regression equation is not easily representable.")
# SVR (Support Vector Regression)
elif regression_type == "SVR (Support Vector Regression)":
model = SVR()
for color, code in zip(['R', 'G', 'B'], ['r', 'g', 'b']):
model.fit(X, data[color])
axes[0].plot(X, model.predict(X), f'{code}--')
st.write("SVR equation is not easily representable.")
# Random Forest Regression
elif regression_type == "Random Forest Regression":
model = RandomForestRegressor()
for color, code in zip(['R', 'G', 'B'], ['r', 'g', 'b']):
model.fit(X, data[color])
axes[0].plot(X, model.predict(X), f'{code}--')
st.write("Random Forest equation is not easily representable.")
st.pyplot(fig)