-
Notifications
You must be signed in to change notification settings - Fork 0
/
cam2hue.py
executable file
·95 lines (74 loc) · 2.86 KB
/
cam2hue.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
#!/usr/bin/python3
import os
import math
import time
import fire
import logging
import socket
from colorsys import rgb_to_hls
from detect_color import get_dominant_colors
from hue import set_lights_color, set_lights_colors, is_any_light_on
from datetime import datetime
def color_distance(left_color,right_color):
rd = abs(left_color[0] - right_color[0])
gd = abs(left_color[1] - right_color[1])
bd = abs(left_color[2] - right_color[2])
return math.sqrt(rd**2 + gd**2 + bd**2)
def color_list_distance(left_list,right_list):
max_distance = 0
# calculate the maximum distance between lists
for (left_color, right_color) in zip(left_list, right_list):
distance = color_distance(left_color, right_color)
if distance > max_distance:
max_distance = distance
return max_distance
def color_difference(color_tuple):
min_val = 255
max_val = 0
for val in color_tuple:
if val < min_val:
min_val = val
if val > max_val:
max_val = val
return max_val - min_val
def color_magnitude(color_tuple):
min_val = 255
max_val = 0
for val in color_tuple:
if val < min_val:
min_val = val
if val > max_val:
max_val = val
return max_val / min_val
def color_tuple_to_hex(color_tuple):
return '%02x%02x%02x' % color_tuple
def cam2hue(image_path="/tmp/camera-color.png",loop=False):
last_colors = []
while True:
current_time = datetime.now()
try:
if not loop or (current_time.hour > 7 and current_time.hour < 22 and is_any_light_on()):
os.system(f"fswebcam -q -r 320x240 --skip 2 --no-banner --png 9 {image_path}")
dominant_colors = get_dominant_colors(image_path, n_colors=5)
logging.info(dominant_colors)
high_contrast_colors = list(filter(lambda c: color_difference(c) > 20 and color_magnitude(c) > 3, dominant_colors))
# Sort by hue
high_contrast_colors = sorted(high_contrast_colors, key=lambda c: rgb_to_hls(*c)[0])
if (len(high_contrast_colors) > 0 and
(len(high_contrast_colors) != len(last_colors) or
color_list_distance(high_contrast_colors, last_colors) > 40)):
hex_colors = list(map(color_tuple_to_hex, high_contrast_colors))
logging.info(hex_colors)
set_lights_colors(hex_colors)
last_colors = high_contrast_colors
if not loop:
break
except socket.timeout:
logging.warning("could not connect to hue bridge")
time.sleep(2)
if __name__ == "__main__":
logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=logging.INFO,
datefmt='%Y-%m-%d %H:%M:%S')
fire.Fire(cam2hue)