File size: 7,641 Bytes
87d5e36
bd820cc
 
 
9463232
293c1c2
 
 
 
 
 
9463232
 
bd820cc
 
 
9463232
 
 
 
 
 
 
 
 
 
 
 
 
bd820cc
293c1c2
 
 
 
 
 
 
 
 
9463232
bd820cc
9463232
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293c1c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5de9a88
 
9463232
 
 
 
 
 
 
5449034
caa7567
e3ddefb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import io
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:
    # CSV 파일 읽기
    data = pd.read_csv(uploaded_file)

    # μ‹œκ°„ ν”„λ ˆμž„μ— λ”°λ₯Έ 데이터 필터링
    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()

    # 차트 생성
    fig, axes = plt.subplots(2, 1, figsize=(10, 8))

    # 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')
    
# νšŒκ·€ 뢄석 μˆ˜ν–‰
X = np.arange(len(data)).reshape(-1, 1)

# μ„ ν˜• νšŒκ·€
if regression_type == "Linear Regression":
    model = LinearRegression()
    model.fit(X, data['R'])
    axes[0].plot(X, model.predict(X), 'r--')
    st.write(f"R: y = {model.coef_[0]} * x + {model.intercept_}")
    model.fit(X, data['G'])
    axes[0].plot(X, model.predict(X), 'g--')
    st.write(f"G: y = {model.coef_[0]} * x + {model.intercept_}")
    model.fit(X, data['B'])
    axes[0].plot(X, model.predict(X), 'b--')
    st.write(f"B: y = {model.coef_[0]} * x + {model.intercept_}")

# λ‹€ν•­ νšŒκ·€
elif regression_type == "Polynomial Regression":
    polynomial_features = PolynomialFeatures(degree=2)
    model = make_pipeline(polynomial_features, LinearRegression())
    model.fit(X, data['R'])
    axes[0].plot(X, model.predict(X), 'r--')
    model.fit(X, data['G'])
    axes[0].plot(X, model.predict(X), 'g--')
    model.fit(X, data['B'])
    axes[0].plot(X, model.predict(X), 'b--')
    st.write("Polynomial regression equation is not easily representable.")

# SVR (Support Vector Regression)
elif regression_type == "SVR (Support Vector Regression)":
    model = SVR()
    model.fit(X, data['R'])
    axes[0].plot(X, model.predict(X), 'r--')
    model.fit(X, data['G'])
    axes[0].plot(X, model.predict(X), 'g--')
    model.fit(X, data['B'])
    axes[0].plot(X, model.predict(X), 'b--')
    st.write("SVR equation is not easily representable.")

# Random Forest Regression
elif regression_type == "Random Forest Regression":
    model = RandomForestRegressor()
    model.fit(X, data['R'])
    axes[0].plot(X, model.predict(X), 'r--')
    model.fit(X, data['G'])
    axes[0].plot(X, model.predict(X), 'g--')
    model.fit(X, data['B'])
    axes[0].plot(X, model.predict(X), 'b--')
    st.write("Random Forest equation is not easily representable.")
    axes[0].legend(loc='upper right')
    axes[0].set_title('RGB Values')

    # HSV 차트
    axes[1].plot(data['H'], 'r', label='H')
    axes[1].plot(data['S'], 'g', label='S')
    axes[1].plot(data['V'], 'b', label='V')
    axes[1].legend(loc='upper right')
    axes[1].set_title('HSV Values')

    st.pyplot(fig)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:
    # CSV 파일 읽기
    data = pd.read_csv(uploaded_file)

    # μ‹œκ°„ ν”„λ ˆμž„μ— λ”°λ₯Έ 데이터 필터링
    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()

    # 차트 생성
    fig, axes = plt.subplots(2, 1, figsize=(10, 8))

    # 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')
    
# νšŒκ·€ 뢄석 μˆ˜ν–‰
X = np.arange(len(data)).reshape(-1, 1)

# μ„ ν˜• νšŒκ·€
if regression_type == "Linear Regression":
    model = LinearRegression()
    model.fit(X, data['R'])
    axes[0].plot(X, model.predict(X), 'r--')
    st.write(f"R: y = {model.coef_[0]} * x + {model.intercept_}")
    model.fit(X, data['G'])
    axes[0].plot(X, model.predict(X), 'g--')
    st.write(f"G: y = {model.coef_[0]} * x + {model.intercept_}")
    model.fit(X, data['B'])
    axes[0].plot(X, model.predict(X), 'b--')
    st.write(f"B: y = {model.coef_[0]} * x + {model.intercept_}")

# λ‹€ν•­ νšŒκ·€
elif regression_type == "Polynomial Regression":
    polynomial_features = PolynomialFeatures(degree=2)
    model = make_pipeline(polynomial_features, LinearRegression())
    model.fit(X, data['R'])
    axes[0].plot(X, model.predict(X), 'r--')
    model.fit(X, data['G'])
    axes[0].plot(X, model.predict(X), 'g--')
    model.fit(X, data['B'])
    axes[0].plot(X, model.predict(X), 'b--')
    st.write("Polynomial regression equation is not easily representable.")

# SVR (Support Vector Regression)
elif regression_type == "SVR (Support Vector Regression)":
    model = SVR()
    model.fit(X, data['R'])
    axes[0].plot(X, model.predict(X), 'r--')
    model.fit(X, data['G'])
    axes[0].plot(X, model.predict(X), 'g--')
    model.fit(X, data['B'])
    axes[0].plot(X, model.predict(X), 'b--')
    st.write("SVR equation is not easily representable.")

# Random Forest Regression
elif regression_type == "Random Forest Regression":
    model = RandomForestRegressor()
    model.fit(X, data['R'])
    axes[0].plot(X, model.predict(X), 'r--')
    model.fit(X, data['G'])
    axes[0].plot(X, model.predict(X), 'g--')
    model.fit(X, data['B'])
    axes[0].plot(X, model.predict(X), 'b--')
    st.write("Random Forest equation is not easily representable.")
    axes[0].legend(loc='upper right')
    axes[0].set_title('RGB Values')

    # HSV 차트
    axes[1].plot(data['H'], 'r', label='H')
    axes[1].plot(data['S'], 'g', label='S')
    axes[1].plot(data['V'], 'b', label='V')
    axes[1].legend(loc='upper right')
    axes[1].set_title('HSV Values')