-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcal_ext_coef.py
More file actions
99 lines (77 loc) · 3 KB
/
cal_ext_coef.py
File metadata and controls
99 lines (77 loc) · 3 KB
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
#!/usr/bin/env python3
#
# Copyright 2021-2022 Sijung Co., Ltd.
#
# Authors:
# cotjdals5450@gmail.com (Seong Min Chae)
# 5jx2oh@gmail.com (Jongjin Oh)
import itertools
import os
import time
import numpy as np
import pandas as pd
import traceback
from scipy.optimize import curve_fit
from model import JS08Settings
from save_log import log
class Coef:
def __init__(self):
self.hanhwa_dist = []
self.hanhwa_x = []
self.hanhwa_r = []
self.hanhwa_g = []
self.hanhwa_b = []
def select_max_rgb(self, r, g, b):
c_list = [r, g, b]
c_index = c_list.index(max(c_list))
if c_index == 0:
select_color = 'red'
elif c_index == 1:
select_color = 'green'
else:
select_color = 'blue'
return select_color
def cal_curve(self, hanhwa: pd.DataFrame):
hanhwa = hanhwa.sort_values(by=['distance'])
self.hanhwa_dist = hanhwa[['distance']].squeeze().to_numpy()
self.hanhwa_x = np.linspace(self.hanhwa_dist[0], self.hanhwa_dist[-1], 100, endpoint=True)
self.hanhwa_x.sort()
self.hanhwa_r = hanhwa[['r']].squeeze().to_numpy()
self.hanhwa_g = hanhwa[['g']].squeeze().to_numpy()
self.hanhwa_b = hanhwa[['b']].squeeze().to_numpy()
r1_init = self.hanhwa_r[0] * 0.7
g1_init = self.hanhwa_g[0] * 0.7
b1_init = self.hanhwa_b[0] * 0.7
r2_init = self.hanhwa_r[-1] * 1.3
g2_init = self.hanhwa_g[-1] * 1.3
b2_init = self.hanhwa_b[-1] * 1.3
select_color = self.select_max_rgb(r2_init, g2_init, b2_init)
r_ext_init = [r1_init, r2_init, 1]
g_ext_init = [g1_init, g2_init, 1]
b_ext_init = [b1_init, b2_init, 1]
try:
hanhwa_opt_r, hanhwa_cov_r = curve_fit(self.func, self.hanhwa_dist, self.hanhwa_r, p0=r_ext_init, maxfev=5000)
hanhwa_opt_g, hanhwa_cov_g = curve_fit(self.func, self.hanhwa_dist, self.hanhwa_g, p0=g_ext_init, maxfev=5000)
hanhwa_opt_b, hanhwa_cov_b = curve_fit(self.func, self.hanhwa_dist, self.hanhwa_b, p0=b_ext_init, maxfev=5000)
JS08Settings.set('maxfev_flag', False)
except RuntimeError as e:
# print(f'[{time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}] - {e}')
# JS08Settings.set('maxfev_flag', True)
# JS08Settings.set('maxfev_count', JS08Settings.get('maxfev_count') + 1)
log(JS08Settings.get('current_id'), 'maxfev Error')
return
list1 = []
list2 = []
list3 = []
list1.append(hanhwa_opt_r[0])
list1.append(hanhwa_opt_g[0])
list1.append(hanhwa_opt_b[0])
list2.append(hanhwa_opt_r[1])
list2.append(hanhwa_opt_g[1])
list2.append(hanhwa_opt_b[1])
list3.append(hanhwa_opt_r[2])
list3.append(hanhwa_opt_g[2])
list3.append(hanhwa_opt_b[2])
return list1, list2, list3, select_color
def func(self, x, c1, c2, a):
return c2 + (c1 - c2) * np.exp(-a * x)