implement P-Controller for smooth color change

This commit is contained in:
Lucca Ketterer 2021-04-26 22:01:09 +02:00
parent c54c3a3a23
commit 9eaf33aea1

View File

@ -3,7 +3,7 @@ import socket
import getopt import getopt
import sys import sys
import subprocess import subprocess
#import pyscreenshot import pyscreenshot
#from PIL import Image, ImageDraw, ImageFont #from PIL import Image, ImageDraw, ImageFont
#import cv2 #import cv2
import numpy as np import numpy as np
@ -72,7 +72,7 @@ def get_base_color(color):
_r,_g,_b = 0,0,0 _r,_g,_b = 0,0,0
def vibrant(r,g,b): def vibrant(r,g,b):
intensity = 1 # usabel range 1-100 max:1000 intensity = 100 # usabel range 1-100 max:1000
intensity = 1+intensity/1000 intensity = 1+intensity/1000
rgb = [r,g,b] rgb = [r,g,b]
@ -88,30 +88,31 @@ def vibrant(r,g,b):
#rgb[min_idx] = int(rgb[min_idx]*(rgb[min_idx]/d)**2) #rgb[min_idx] = int(rgb[min_idx]*(rgb[min_idx]/d)**2)
return rgb return rgb
def p_controller(soll, ist, K):
return K*(soll-ist)
def ambient_light_thread(): def ambient_light_thread():
r,g,b = 0,0,0 r,g,b = 0,0,0
brighness = 1 brighness = 1
while True: while True:
# P-Regler
if r > _r: r,g,b = [w+((y-w)*0.05) for y,w in zip((_r,_g,_b),(r,g,b))]
r-=1 print(int(r), int(g), int(b))
elif r < _r: print(_r,_g,_b)
r+=1
if g > _g:
g-=1
elif g < _g:
g+=1
if b > _b:
b-=1
elif b < _b:
b+=1
print(r,g,b)
send(rgb_to_hex(int(r*brighness),int(g*brighness),int(b*brighness))) send(rgb_to_hex(int(r*brighness),int(g*brighness),int(b*brighness)))
time.sleep(0.01) time.sleep(0.01)
ups_counter = 0
start_time = time.time()
def ups():
global ups_counter
global start_time
ups_counter += 1
time_d = time.time()-start_time
ups = ups_counter/time_d
print(ups)
def ambient_light(): def ambient_light():
t = Thread(target=ambient_light_thread) t = Thread(target=ambient_light_thread)
@ -119,22 +120,10 @@ def ambient_light():
global _r,_g,_b global _r,_g,_b
#bus = SessionBus()
#RecorderPipeline = "vp8enc min_quantizer=10 max_quantizer=50 cq_level=13 cpu-used=1 deadline=1000000 threads=%T ! queue ! webmmux"
#GNOMEScreencast = bus.get('org.gnome.Shell.Screencast', '/org/gnome/Shell/Screencast')
#GNOMEScreencast.Screencast('/tmp/a', {})
#GNOMEScreencast.StopScreencast()
#time.sleep(1)
amp = [1,1,0.8] amp = [1,1,0.8]
counter = 0 counter = 0
start_time = time.time() start_time = time.time()
while True: while True:
counter += 1
time_d = time.time()-start_time
ups = counter/time_d
#print(ups)
# screenshot # screenshot
# Xorg # Xorg
@ -152,22 +141,8 @@ def ambient_light():
#img = Image.fromarray(frame) #img = Image.fromarray(frame)
#cap.release() #cap.release()
'''
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(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])
'''
# find dominant color # find dominant color
img.thumbnail((1,1)) img.thumbnail((2,2))
r,g,b = img.getpixel((0, 0)) r,g,b = img.getpixel((0, 0))
_r,_g,_b = vibrant(r,g,b) _r,_g,_b = vibrant(r,g,b)