From 273fd18deffe400ce34f752708e875af3b704b19 Mon Sep 17 00:00:00 2001 From: Lucca Ketterer Date: Wed, 29 Jul 2020 16:33:16 +0200 Subject: [PATCH] improve color detection performance --- client/client.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/client/client.py b/client/client.py index 76a8b42..2409a24 100755 --- a/client/client.py +++ b/client/client.py @@ -64,14 +64,12 @@ def ambient_light_thread(): print(r,g,b) send(rgb_to_hex(int(r*brighness),int(g*brighness),int(b*brighness))) time.sleep(0.01) - - def ambient_light(): t = Thread(target=ambient_light_thread) t.start() - + global _r,_g,_b while True: @@ -79,15 +77,21 @@ def ambient_light(): img = pyscreenshot.grab(backend="mss", childprocess=False, bbox=(1920,0,4480,1440)) # find dominant color + ''' img = np.array(img) pixels = np.float32(img.reshape(-1, 3)) + img = cv2.UMat(img) n_colors = 1 criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 200, .1) flags = cv2.KMEANS_RANDOM_CENTERS - _, labels, palette = cv2.kmeans(pixels, n_colors, None, criteria, 10, flags) + _, labels, palette = cv2.kmeans(img, n_colors, None, criteria, 10, flags) _, counts = np.unique(labels, return_counts=True) dominant = palette[np.argmax(counts)] _r,_g,_b = int(dominant[0]), int(dominant[1]), int(dominant[2]) + ''' + + img.thumbnail((1,1)) + _r,_g,_b = img.getpixel((0, 0)) def rgb_to_hex(r,g,b): return "%02x%02x%02x" % (r,g,b)