This repository has been archived by the owner on Jun 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadaptive_brightness.py
213 lines (162 loc) · 6.53 KB
/
adaptive_brightness.py
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
import cv2
import numpy as np
from subprocess import call, check_output
import time
import datetime
import psutil
import tensorflow as tf
tf.enable_eager_execution()
def to_range(value, minimum, maximum):
return min(max(value, minimum), maximum)
def log(output):
print output
class LightSensor:
def __init__(self, camera_port=0):
self.camera = None
self.camera_port = camera_port
self.enabled = False
@staticmethod
def __set_auto_exposure(auto_exposure_on):
call(["v4l2-ctl", "--set-ctrl", "exposure_auto_priority=" + str(int(auto_exposure_on))])
def enable(self):
try:
self.camera = cv2.VideoCapture(self.camera_port)
LightSensor.__set_auto_exposure(False)
self.enabled = True
except Exception:
self.enabled = False
def get(self):
if self.enabled:
try:
_, frame = self.camera.read()
yuv = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV)
channels = cv2.split(yuv)
return np.mean(channels[0])
except Exception:
return 0.0
else:
return 0.0
def disable(self):
LightSensor.__set_auto_exposure(True)
if self.enabled:
self.camera.release()
class Backlight:
def __init__(self):
pass
def set_brightness(self, percentage):
try:
percentage = int(to_range(round(percentage), 0, 100))
log("Setting brightness to " + str(percentage))
call(['gdbus', 'call', '--session', '--dest', 'org.gnome.SettingsDaemon.Power',
'--object-path', '/org/gnome/SettingsDaemon/Power', '--method', 'org.freedesktop.DBus.Properties.Set',
'org.gnome.SettingsDaemon.Power.Screen', 'Brightness', '<int32 ' + str(percentage) + '>'])
except Exception:
pass
def get_brightness(self):
try:
output = check_output(['gdbus', 'call', '--session', '--dest', 'org.gnome.SettingsDaemon.Power',
'--object-path', '/org/gnome/SettingsDaemon/Power', '--method',
'org.freedesktop.DBus.Properties.Get',
'org.gnome.SettingsDaemon.Power.Screen', 'Brightness'])
number = ""
for char in output:
if char.isdigit():
number += char
return int(number)
except Exception:
return 0
class Battery:
def __init__(self):
pass
def get_percent(self):
return psutil.sensors_battery().percent
def is_plugged_in(self):
return psutil.sensors_battery().power_plugged
class Clock:
def __init__(self):
pass
def get_as_float(self):
time_of_day = self.get()
return time_of_day.hour + time_of_day.minute / 60.0
def get(self):
return datetime.datetime.now().time()
class LowPassFilter:
def __init__(self, filter_coef):
self.filter_coef = to_range(filter_coef, 0, 1)
self.last_value = 0.0
def filter(self, value):
self.last_value = self.filter_coef * self.last_value + (1 - self.filter_coef) * value
return self.last_value
class AdaptiveBrightness:
def __init__(self, light_sensor=LightSensor(), backlight=Backlight()):
self.light_sensor = light_sensor
self.backlight = backlight
def get_light(self):
self.light_sensor.enable()
light = self.light_sensor.get()
log("Read light as " + str(int(round(light))))
self.light_sensor.disable()
return light
def set_brightness(self, percentage):
self.backlight.set_brightness(percentage)
class SimpleAdaptiveBrightness(AdaptiveBrightness):
def __init__(self, brightness_compensation, change_threshold=6, light_sensor=LightSensor(), backlight=Backlight()):
AdaptiveBrightness.__init__(self, light_sensor, backlight)
self.brightness_compensation = brightness_compensation
self.last_change = -1
self.change_threshold = change_threshold
def run(self):
light = self.get_light()
if self.last_change == -1 or abs(light - self.last_change) > self.change_threshold:
self.set_brightness(light * self.brightness_compensation)
self.last_change = light
class MLAdaptiveBrightness(AdaptiveBrightness):
def __init__(self, change_threshold=6, light_sensor=LightSensor(), backlight=Backlight()):
AdaptiveBrightness.__init__(self, light_sensor, backlight)
self.change_threshold = change_threshold
self.last_change = -1
self.data = []
self.learning_rate = 0.01
self.num_steps = 10
self.batch_size = 10
self.my_optimizer = tf.train.GradientDescentOptimizer(learning_rate=self.learning_rate)
self.my_optimizer = tf.contrib.estimator.clip_gradients_by_norm(self.my_optimizer, 5.0)
self.model = tf.keras.models.load_model(
"model",
custom_objects=None,
compile=False
)
self.model.compile(optimizer=self.my_optimizer, loss=tf.keras.losses.mean_squared_error)
self.last_brightness = self.backlight.get_brightness()
def run(self):
light = self.get_light()
current_brightness = self.backlight.get_brightness()
if current_brightness != self.last_brightness:
self.learn(self.last_change, current_brightness)
self.last_brightness = self.backlight.get_brightness()
if self.last_change == -1 or abs(light - self.last_change) > self.change_threshold:
self.set_brightness(self.model.predict(np.array([light]))[0][0])
self.last_change = light
self.last_brightness = self.backlight.get_brightness()
def learn(self, light, brightness):
remove_list = []
for value in self.data:
if value[0] == light:
remove_list.append(value)
for value in remove_list:
self.data.remove(value)
self.data.append([light, brightness])
features = np.array([x[0] for x in self.data])
labels = np.array([x[1] for x in self.data])
self.model.fit(features, labels, batch_size=self.batch_size, epochs=self.num_steps)
tf.keras.models.save_model(
self.model,
"model",
overwrite=True,
include_optimizer=False
)
if __name__ == "__main__":
adaptive_brightness = MLAdaptiveBrightness()
while True:
adaptive_brightness.run()
time.sleep(6)