File size: 2,220 Bytes
4719196
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""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()