Compare commits

..

No commits in common. "ea90d8701916c7d12307872f56f3da4470cd204e" and "73c31c0fb3e0c246b62c1c4caf330e662e107877" have entirely different histories.

9 changed files with 51 additions and 260 deletions

2
.gitignore vendored
View File

@ -1,2 +0,0 @@
__pycache__/

View File

@ -1,6 +1,7 @@
from tkinter import TOP
import pyautogui
import scipy.cluster as cluster
import scipy
import sys
import numpy as np
from time import sleep, time
@ -11,38 +12,29 @@ from time import perf_counter as timestamp
from telemetrix_rpi_pico import telemetrix_rpi_pico
from multiprocessing import Pool
import PIL.ImageGrab
from PIL import Image
import colorsys
import win32gui, win32ui, win32con
class size:
width = None
height = None
def get_screenshot():
return PIL.ImageGrab.grab()
def sigint_handler(signal=None, frame=None):
print ('KeyboardInterrupt is caught')
board.neopixel_clear()
sleep(0.75)
board.shutdown()
exit()
sys.exit(0)
FAST_MODE = True
SATURATION_BOOST_FACTOR = 2.5
FAST_MODE = False
def find_dorminant_color(im):
if FAST_MODE:
color = numpy.reshape(numpy.asarray(im),(-1,3))
color = numpy.median(color,axis=0)
h,s,v = colorsys.rgb_to_hsv(r=color[0]/255,g=color[1]/255,b=color[2]/255)
s = min(1,s*SATURATION_BOOST_FACTOR)
r,g,b = colorsys.hsv_to_rgb(h=h,s=s,v=v)
return [r*255,g*255,b*255]
return color
else:
NUM_CLUSTERS = 2
NUM_CLUSTERS = 3
im = im.resize((100, 100))
im = im.resize((50, 50))
ar = np.asarray(im)
shape = ar.shape
ar = ar.reshape(np.product(shape[:2]), shape[2]).astype(float)
@ -54,14 +46,8 @@ def find_dorminant_color(im):
index_max = np.argmax(counts)
peak = codes[index_max]
h,s,v = colorsys.rgb_to_hsv(r=peak[0]/255,g=peak[1]/255,b=peak[2]/255)
s = min(1,s*SATURATION_BOOST_FACTOR)
r,g,b = colorsys.hsv_to_rgb(h=h,s=s,v=v)
return [r*255,g*255,b*255]
return peak
if __name__ == '__main__':
size = size()
size.width=pyautogui.size().width
size.height=pyautogui.size().height
signal.signal(signal.SIGINT, sigint_handler)
atexit.register(sigint_handler)
global NUM_LEDS,BOARDER_SIZE,TOP_LEDS,RIGHT_LEDS,BUTTOM_LEDS,LEFT_LEDS,screenshot,board
@ -70,25 +56,29 @@ if __name__ == '__main__':
BOARDER_SIZE = 50
BOARDER_SIZE = 150
TOP_LEDS = 19
RIGHT_LEDS = 11
BUTTOM_LEDS = 19
LEFT_LEDS = 11
MIN_TIME = 0.05
BUTTOM_LEDS = 17
LEFT_LEDS = 10
REDUCTION = 4
NUM_LEDS = TOP_LEDS+RIGHT_LEDS+BUTTOM_LEDS+LEFT_LEDS
board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS)
board.neopixel_clear(auto_show=True)
size = size()
size.width=pyautogui.size().width/REDUCTION
size.height=pyautogui.size().height/REDUCTION
board.neopixel_show()
screenshot = pyautogui.screenshot()
pool = Pool(8)
pool = Pool(16)
while True:
try:
screenshot = get_screenshot()
screenshot = PIL.ImageGrab.grab()
lastTime = timestamp()
chunk = []
top = screenshot.crop(box=[0,0,size.width,BOARDER_SIZE])
@ -97,26 +87,25 @@ if __name__ == '__main__':
right = screenshot.crop(box=[size.width-BOARDER_SIZE,0,size.width,size.height])
for i in range(0,TOP_LEDS):
segment = top.crop(box=[i*size.width/TOP_LEDS,0,(i+1)*size.width/TOP_LEDS,BOARDER_SIZE])
chunk.append(segment)
chunk.insert(i,segment)
for i in range(0,LEFT_LEDS):
segment = left.crop(box=[0,i*size.height/LEFT_LEDS,BOARDER_SIZE,(i+1)*size.height/LEFT_LEDS])
chunk.insert(TOP_LEDS+RIGHT_LEDS+BUTTOM_LEDS+LEFT_LEDS-i,segment)
for i in range(0,BUTTOM_LEDS):
segment = buttom.crop(box=[i*size.width/BUTTOM_LEDS,0,(i+1)*size.width/BUTTOM_LEDS,BOARDER_SIZE])
chunk.insert(TOP_LEDS+RIGHT_LEDS+BUTTOM_LEDS-i,segment)
board.neopixel_show()
for i in range(0,RIGHT_LEDS):
segment = right.crop(box=[0,i*size.height/RIGHT_LEDS,BOARDER_SIZE,(i+1)*size.height/RIGHT_LEDS])
chunk.append(segment)
for i in reversed(range(0,BUTTOM_LEDS)):
segment = buttom.crop(box=[i*size.width/BUTTOM_LEDS,0,(i+1)*size.width/BUTTOM_LEDS,BOARDER_SIZE])
chunk.append(segment)
for i in reversed(range(0,LEFT_LEDS)):
segment = left.crop(box=[0,i*size.height/LEFT_LEDS,BOARDER_SIZE,(i+1)*size.height/LEFT_LEDS])
chunk.append(segment)
chunk.insert(i+TOP_LEDS,segment)
colors = pool.map(find_dorminant_color,chunk)
for i in range(0,len(colors)):
board.neo_pixel_set_value(i,r=int(colors[i][0]),g=int(colors[i][1]),b=int(colors[i][2]))
board.neopixel_show()
while timestamp()-lastTime < MIN_TIME:
sleep(0.001)
print("loop time : "+str(timestamp()-lastTime))
except Exception as e:
print(e)
print("Retrying")
#print("loop time : "+str(timestamp()-lastTime))

View File

@ -2,48 +2,23 @@ import scipy.signal as signal
import pyaudio
from matplotlib import pyplot as plt
import numpy as np
from time import sleep, time
import cupy
import atexit
import signal
from telemetrix_rpi_pico import telemetrix_rpi_pico
from time import sleep
def sigint_handler(signal=None, frame=None):
print ('KeyboardInterrupt is caught')
board.neopixel_clear()
sleep(0.75)
board.shutdown()
exit()
signal.signal(signal.SIGINT, sigint_handler)
atexit.register(sigint_handler)
NUM_LEDS = 60
board = telemetrix_rpi_pico.TelemetrixRpiPico()
board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS)
board.neopixel_clear(auto_show=True)
SAMPLE_SIZE = 4096
SAMPLE_RATE = 48000
SAMPLE_SIZE = 1024
audio = pyaudio.PyAudio()
audioStream = audio.open(format=pyaudio.paInt16, channels=1, rate=SAMPLE_RATE, input=True, frames_per_buffer=SAMPLE_SIZE)
audioStream = audio.open(format=pyaudio.paInt16, channels=1, rate=1000, input=True, frames_per_buffer=SAMPLE_SIZE)
while True:
data = audioStream.read(SAMPLE_SIZE)
sample = cupy.frombuffer(data, dtype=np.int16)
power = cupy.sum(cupy.abs(sample))/SAMPLE_SIZE
if power > 1000:
freq_dom = cupy.fft.fft(sample,10000)
freqs = cupy.fft.fftfreq(len(freq_dom))
power_bass = cupy.sum(cupy.abs(freq_dom[0:30]))/cupy.sum(cupy.abs(freq_dom))*power
power_bass = max(0,power_bass-275)
print(power_bass)
idmax = cupy.argmax(cupy.abs(freq_dom))
freqmax = abs(freqs[idmax]*SAMPLE_RATE)
board.neopixel_fill(r=int(min(255,power_bass)),g=0,b=0,auto_show=True)
else:
board.neopixel_clear(auto_show=True)
sample = np.frombuffer(data, dtype=np.int16)
# plot data
plt.plot(sample)
plt.show()
freqdom_signal = signal.stft(sample)
print(freqdom_signal)
# close stream
audioStream.stop_stream()
audioStream.close()
audio.terminate()

View File

@ -1,51 +0,0 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico
from time import sleep as delay
import signal
import sys
import colorsys
import numpy
from time import perf_counter as millis
def sigint_handler(signal, frame):
print ('KeyboardInterrupt is caught')
board.shutdown()
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
board = telemetrix_rpi_pico.TelemetrixRpiPico()
NUM_LEDS = 60
board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS)
board.neopixel_clear(auto_show=True)
color = [[255,0,0],[255,127,0],[255,255,0],
[127,255,0],[0,255,0],[0,255,127],
[0,255,255],[0,127,255],[0,0,255],
[127,0,255],[255,0,255],[255,0,127]]
timecounter = 0
array = numpy.random.randint(low=0,high=9,size=60)
def updateLED():
for i in range(0,NUM_LEDS):
board.neo_pixel_set_value(i,r=color[array[i]][0],g=color[array[i]][1],b=color[array[i]][2])
board.neopixel_show()
while True:
array = numpy.random.randint(low=0,high=9,size=60)
isSorted = False
for i in range(len(array)):
# loop to compare array elements
for j in range(0, len(array) - i - 1):
# compare two adjacent elements
# change > to < to sort in descending order
if array[j] > array[j + 1]:
# swapping elements if elements
# are not in the intended order
temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
updateLED()
delay(0.1)
board.shutdown()

View File

@ -1,28 +0,0 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico
from time import sleep as delay
import signal
import sys
import colorsys
from time import perf_counter as millis
def sigint_handler(signal, frame):
print ('KeyboardInterrupt is caught')
board.shutdown()
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
board = telemetrix_rpi_pico.TelemetrixRpiPico()
board.set_pin_mode_neopixel(pin_number=2,num_pixels=60)
board.neopixel_clear(auto_show=True)
SEGMENT_SIZE = 1
VELOCITY = 10
NUM_LEDS = 60
color = []
k = 0
timecounter = 0
while(True):
for j in range(0,NUM_LEDS):
r, g, b = colorsys.hsv_to_rgb(((-timecounter*VELOCITY+j*4)%360)/360,1,1)
board.neo_pixel_set_value(j,r=int(r*255),g=int(g*255),b=int(b*255))
timecounter+=1
delay(0.1)
board.neopixel_show()

View File

@ -1,34 +0,0 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico
from time import sleep as delay
import signal
import sys
import numpy
from time import perf_counter as millis
def sigint_handler(signal, frame):
print ('KeyboardInterrupt is caught')
board.shutdown()
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
board = telemetrix_rpi_pico.TelemetrixRpiPico()
NUM_LEDS = 60
board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS)
board.neopixel_clear(auto_show=True)
color = [[255,0,0],[255,127,0],[255,255,0],
[127,255,0],[0,255,0],[0,255,127],
[0,255,255],[0,127,255],[0,0,255],
[127,0,255],[255,0,255],[255,0,127]]
array = numpy.random.randint(low=0,high=9,size=60)
def updateLED():
for i in range(0,NUM_LEDS):
board.neo_pixel_set_value(i,r=color[array[i]][0],g=color[array[i]][1],b=color[array[i]][2])
board.neopixel_show()
while True:
array = numpy.random.randint(low=0,high=9,size=60)
updateLED()
delay(0.1)
board.shutdown()

View File

@ -1,7 +0,0 @@
pyautogui
pillow
telemetrix_rpi_pico
numpy
cupy
scipy
pywin32

View File

@ -1,47 +0,0 @@
from telemetrix_rpi_pico import telemetrix_rpi_pico
from time import sleep as delay
import signal
import sys
import colorsys
import numpy
from scipy.ndimage.interpolation import shift
from time import perf_counter as millis
import threading
def sigint_handler(signal, frame):
print ('KeyboardInterrupt is caught')
board.shutdown()
sys.exit(0)
signal.signal(signal.SIGINT, sigint_handler)
board = telemetrix_rpi_pico.TelemetrixRpiPico()
NUM_LEDS = 60
TIME_UNIT = 0.1
global spaces
spaces = numpy.zeros(NUM_LEDS)
board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS)
board.neopixel_clear(auto_show=True)
def updateLED():
for i in range(0,NUM_LEDS):
board.neo_pixel_set_value(i,r=int(spaces[i]*255),g=int(spaces[i]*255),b=int(spaces[i]*255))
board.neopixel_show()
def advanceTimeUnit():
global spaces
while True:
spaces = shift(spaces,1,cval=0)
updateLED()
delay(TIME_UNIT)
atu = threading.Thread(target=advanceTimeUnit)
atu.start()
while True:
input("Press Enter to Particle")
spaces[0] = 1

16
test.py
View File

@ -1,11 +1,7 @@
import cupy
import colorsys
peak = [124.3,231.5, 64.4]
h,s,v = colorsys.rgb_to_hsv(r=peak[0]/255,g=peak[1]/255,b=peak[2]/255)
SATURATION_BOOST_FACTOR = 1.5
s = min(1,s*SATURATION_BOOST_FACTOR)
print([h,s,v])
r,g,b = colorsys.hsv_to_rgb(h=h,s=s,v=v)
print([r,g,b])
a = cupy.asarray([[[1, 2, 3],[4,5,6],[7,8,9]],[[10, 11, 12],[13,14,15],[16,17,18]]])
print(a)
a= cupy.reshape(a,(-1,3))
print(a)
a = cupy.median(a,axis=0)
print(a)