Image pre-calculation working
This commit is contained in:
parent
cb9f422a0d
commit
d123096b49
1 changed files with 28 additions and 24 deletions
52
clippy.py
52
clippy.py
|
|
@ -8,13 +8,13 @@ import time
|
||||||
import numpy
|
import numpy
|
||||||
import bz2
|
import bz2
|
||||||
import os
|
import os
|
||||||
|
import functools
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
from pixelterm import pixelterm
|
from pixelterm import pixelterm
|
||||||
|
|
||||||
HOST, PORT = "172.23.42.29",2342
|
HOST, PORT = "172.23.42.29",2342
|
||||||
DISPLAY_WIDTH, DISPLAY_HEIGHT = 56*8, 12*19
|
|
||||||
CMD_LED_DRAW = 18
|
CMD_LED_DRAW = 18
|
||||||
|
|
||||||
def resize_image(img, size):
|
def resize_image(img, size):
|
||||||
|
|
@ -31,16 +31,12 @@ def resize_image(img, size):
|
||||||
class Display:
|
class Display:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
|
self.size = 56*8, 8*20
|
||||||
|
|
||||||
def sendframe(self, frame):
|
def sendframe(self, frame):
|
||||||
buf = numpy.frombuffer(frame.tobytes(), dtype='1b')
|
|
||||||
print('sendbuf: len', len(buf))
|
|
||||||
buf_narfed = numpy.concatenate([ buf[(i*12)*DISPLAY_WIDTH: (i*12+8)*DISPLAY_WIDTH] for i in range(20) ])
|
|
||||||
# txdata = numpy.packbits(buf_narfed).tobytes()
|
|
||||||
txdata = buf_narfed
|
|
||||||
self.sock.sendto(struct.pack('!HHHHH', CMD_LED_DRAW, 0,
|
self.sock.sendto(struct.pack('!HHHHH', CMD_LED_DRAW, 0,
|
||||||
(56*8*(12*20-8))%65536, 0x627a, 0) + # do. not. fucking. ask.
|
(56*8*(12*20-8))%65536, 0x627a, 0) + # do. not. fucking. ask.
|
||||||
bz2.compress(txdata), (HOST, PORT))
|
bz2.compress(frame), (HOST, PORT))
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def do_gamma(im, gamma):
|
def do_gamma(im, gamma):
|
||||||
|
|
@ -51,6 +47,10 @@ class Display:
|
||||||
im = im.point(lut)
|
im = im.point(lut)
|
||||||
return im
|
return im
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def encode_image(img, displaysize):
|
||||||
|
return numpy.frombuffer(Display.do_gamma(resize_image(img, displaysize), 0.5).convert('1').tobytes(), dtype='1b')
|
||||||
|
|
||||||
def weightedChoice(choices, default=None):
|
def weightedChoice(choices, default=None):
|
||||||
acc = 0
|
acc = 0
|
||||||
r = random.random()
|
r = random.random()
|
||||||
|
|
@ -74,10 +74,6 @@ class Agent:
|
||||||
f['next'] = lambda f, idx: f['exitBranch']
|
f['next'] = lambda f, idx: f['exitBranch']
|
||||||
else:
|
else:
|
||||||
f['next'] = lambda f, idx: idx+1
|
f['next'] = lambda f, idx: idx+1
|
||||||
if 'images' in f:
|
|
||||||
self.sendframe(
|
|
||||||
Display.do_gamma(
|
|
||||||
resize_image(img, (DISPLAY_WIDTH, DISPLAY_HEIGHT)), 0.5).convert('1'))
|
|
||||||
self.picmap = Image.open(path / 'map.png')
|
self.picmap = Image.open(path / 'map.png')
|
||||||
self.path = path
|
self.path = path
|
||||||
|
|
||||||
|
|
@ -85,9 +81,22 @@ class Agent:
|
||||||
print('Playing:', action)
|
print('Playing:', action)
|
||||||
for frame in self._animate(action):
|
for frame in self._animate(action):
|
||||||
print('frame:', frame)
|
print('frame:', frame)
|
||||||
if 'images' in frame: # some frames contain branch info and sound, but no images
|
if 'images_encoded' in frame: # some frames contain branch info and sound, but no images
|
||||||
yield self.get_image(*frame['images'][0])
|
yield frame['images_encoded']
|
||||||
time.sleep(frame['duration']/1000)
|
time.sleep(frame['duration']/1000)
|
||||||
|
|
||||||
|
def precalculate_images(self, dsp, termsize):
|
||||||
|
for ani in self.config['animations'].values():
|
||||||
|
for f in ani['frames']:
|
||||||
|
if 'images' in f:
|
||||||
|
f['images_encoded'] = self._precalculate_one_image(tuple(f['images'][0]), dsp, termsize)
|
||||||
|
self._precalculate_one_image.cache_clear()
|
||||||
|
|
||||||
|
@functools.lru_cache(maxsize=None)
|
||||||
|
def _precalculate_one_image(self, coords, dsp, termsize):
|
||||||
|
img = self.get_image(*coords)
|
||||||
|
return ( dsp.encode_image(img, dsp.size) if dsp else None,
|
||||||
|
pixelterm.termify_pixels(resize_image(img, termsize)) if termsize else None )
|
||||||
|
|
||||||
def _animate(self, action):
|
def _animate(self, action):
|
||||||
anim, idx = self.config['animations'][action]['frames'], 0
|
anim, idx = self.config['animations'][action]['frames'], 0
|
||||||
|
|
@ -125,12 +134,13 @@ if __name__ == '__main__':
|
||||||
print('\n'.join(Agent(agent_path).animations))
|
print('\n'.join(Agent(agent_path).animations))
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
dsp = Display()
|
dsp = Display() if args.display else None
|
||||||
|
agent = Agent(agent_path)
|
||||||
tx, ty = os.get_terminal_size()
|
tx, ty = os.get_terminal_size()
|
||||||
termsize = (tx, ty*2)
|
termsize = (tx, ty*2) if args.terminal else None
|
||||||
|
agent.precalculate_images(dsp, termsize)
|
||||||
|
|
||||||
if args.endless:
|
if args.endless:
|
||||||
agent = Agent(agent_path)
|
|
||||||
while True:
|
while True:
|
||||||
if random.random() > 0.2:
|
if random.random() > 0.2:
|
||||||
for img_dsp, img_term in agent(random.choice(agent.animations)):
|
for img_dsp, img_term in agent(random.choice(agent.animations)):
|
||||||
|
|
@ -141,16 +151,10 @@ if __name__ == '__main__':
|
||||||
if args.display:
|
if args.display:
|
||||||
dsp.sendframe(img_dsp)
|
dsp.sendframe(img_dsp)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
for img_dsp, img_term in Agent(agent_path)(args.action):
|
for img_dsp, img_term in agent(args.action):
|
||||||
if args.terminal:
|
if args.terminal:
|
||||||
print(pixelterm.termify_pixels(
|
print(pixelterm.termify_pixels(
|
||||||
resize_image(img, termsize)))
|
resize_image(img, termsize)))
|
||||||
# print(pixelterm.termify_pixels(
|
|
||||||
# img.thumbnail((80, 40))
|
|
||||||
# .convert('1', dither=Image.FLOYDSTEINBERG)
|
|
||||||
# .convert('RGBA', dither=Image.FLOYDSTEINBERG)))
|
|
||||||
# print('display size:', (DISPLAY_WIDTH, DISPLAY_HEIGHT))
|
|
||||||
if args.display:
|
if args.display:
|
||||||
dsp.sendframe(img_dsp)
|
dsp.sendframe(img_dsp)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue