Skip to content
This repository was archived by the owner on Jun 8, 2022. It is now read-only.

Commit fd8ea3a

Browse files
committed
Use k-means clustering to determine color in Ambient.py
1 parent 319d633 commit fd8ea3a

1 file changed

Lines changed: 31 additions & 12 deletions

File tree

Ambient.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22
from PIL import ImageGrab #pip3 install Pillow
33
from openrgb import OpenRGBClient #pip3 install openrgb-python
44
from openrgb.utils import RGBColor
5-
5+
from sklearn.cluster import KMeans
6+
from collections import Counter
7+
import numpy as np
8+
69
#//////////////////////////////////////////////////////////////////////////////////////////////////////////
710
# GLOBAL DEFINES
811
#//////////////////////////////////////////////////////////////////////////////////////////////////////////
912
#HEIGHT = 1920 #now using image.size[1] dynamically
1013
#WIDTH = 1200 #now using image.size[0] dynamically
1114
LOOP_INTERVAL = 0.05 # how often we calculate screen colour (in seconds)
12-
DURATION = 3 # how long it takes bulb to switch colours (in seconds)
13-
DECIMATE = 10 # skip every DECIMATE number of pixels to speed up calculation
15+
DURATION = 3 # how long it takes bulb to switch colours (in seconds)
16+
DECIMATE = 100 # sample only 1% of pixels to speed up
17+
K_MEANS = 3 # number of clusters to calculate (returns average color if 1 and dominant color if >1)
1418

1519
XSCREENCAPTURE = True # Set to true if on X11, else false
1620
X_RES = 2560 # Screen pixel in x direction
1721
Y_RES = 1440 # screen pixel in y direction
1822

19-
2023
client = OpenRGBClient() #will only work if you use default ip/port for OpenRGB server. This is an easy fix, read the documentation if need be https://openrgb-python.readthedocs.io/en/latest/
2124

2225
Dlist = client.devices
@@ -59,6 +62,13 @@ def hex_to_rgb(value):
5962
lv = len(value)
6063
return tuple(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3))
6164

65+
# init K-Means model
66+
clt = KMeans(n_clusters = 3, n_init=3, max_iter=200)
67+
68+
# init vars for colors before current
69+
old2 = np.zeros(3)
70+
old = np.zeros(3)
71+
6272
# run loop
6373
while True:
6474
#init counters/accumulators
@@ -77,18 +87,27 @@ def hex_to_rgb(value):
7787
image = ImageGrab.grab() # take a screenshot using PIL
7888
#print image.size
7989

80-
90+
data = []
8191
for y in range(0, image.size[1], DECIMATE): #loop over the height
8292
for x in range(0, image.size[0], DECIMATE): #loop over the width
8393
color = image.getpixel((x, y)) #grab a pixel
84-
red += color[0]
85-
green += color[1]
86-
blue += color[2]
87-
94+
data.append(color)
8895

89-
red = (( red / ( (image.size[1]/DECIMATE) * (image.size[0]/DECIMATE) ) ) )
90-
green = ((green / ( (image.size[1]/DECIMATE) * (image.size[0]/DECIMATE) ) ) )
91-
blue = ((blue / ( (image.size[1]/DECIMATE) * (image.size[0]/DECIMATE) ) ) )
96+
#cluster and assign labels to the pixels
97+
labels = clt.fit_predict(data)
98+
#count labels to find most popular
99+
label_counts = Counter(labels)
100+
#subset out most popular centroid
101+
dominant_color = clt.cluster_centers_[label_counts.most_common()[0][0]]
102+
103+
# current color is blended with colors 2 frames before to smooth effect
104+
dominant_color += (old + 0.5*old2)
105+
dominant_color /= 2.5
106+
# reset vars for next iteration
107+
old = dominant_color
108+
old2 = old
109+
110+
red, green, blue = dominant_color
92111
#print(red, green, blue)
93112
for Device in Dlist:
94113
Device.set_color(RGBColor(int(red), int(green), int(blue)))

0 commit comments

Comments
 (0)