Brainmu-SpikeCamera / src /ui /media_server.py
sunbaby's picture
Upload 69 files
4719196
Raw History Blame Contribute Delete
2.22 kB
"""Read-only spike GIF and grayscale GT previews for the fixed migrated dataset."""
from http.server import BaseHTTPRequestHandler,ThreadingHTTPServer
from pathlib import Path
from urllib.parse import urlparse,parse_qs
import io,re,threading,json,urllib.request,hashlib
import numpy as np
from PIL import Image
ROOT=Path(__file__).resolve().parents[1]
DATA=ROOT/'data/test'
CACHE=ROOT/'.cache/ui_spike_gifs';CACHE.mkdir(parents=True,exist_ok=True)
LOCK=threading.Lock()
def spike_gif(dat,cache):
if cache.is_file():return cache.read_bytes()
with LOCK:
if cache.is_file():return cache.read_bytes()
raw=np.fromfile(dat,dtype=np.uint8)
if raw.size!=512500:raise ValueError('Invalid 41-frame DAT')
x=np.unpackbits(raw.reshape(41,12500),axis=1,bitorder='little').reshape(41,250,400)
x=np.flip(x,axis=1)
frames=[Image.fromarray(f*255).convert('P') for f in x]
buf=io.BytesIO();frames[0].save(buf,format='GIF',save_all=True,append_images=frames[1:],duration=80,loop=0,optimize=False,disposal=2)
cache.write_bytes(buf.getvalue());return buf.getvalue()
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
try:
q=parse_qs(urlparse(self.path).query);name=q['id'][0];kind=urlparse(self.path).path
with urllib.request.urlopen('http://127.0.0.1:8997/api/state',timeout=10) as response:
current=json.load(response)
row=next((r for r in current['rows'] if r['id']==name),None)
if row is None:raise ValueError('Unknown sample')
dat=Path(row['spike'])
cache_id=hashlib.sha256((str(dat.resolve())+str(dat.stat().st_mtime_ns)).encode()).hexdigest()
if kind=='/spike':
body=spike_gif(dat,CACHE/(cache_id+'.gif'));mime='image/gif'
elif kind=='/gt':
with Image.open(row['gt']) as im:
b=io.BytesIO();im.convert('L').save(b,format='PNG');body=b.getvalue();mime='image/png'
else:raise ValueError('Unknown preview')
self.send_response(200);self.send_header('Content-Type',mime);self.send_header('Content-Length',str(len(body)));self.send_header('Access-Control-Allow-Origin','*');self.end_headers();self.wfile.write(body)
except (ValueError,KeyError,OSError):
self.send_error(404)
if __name__=='__main__':ThreadingHTTPServer(('127.0.0.1',8998),Handler).serve_forever()