stop leaking memory through jpeg decoders
[sparrow.git] / sparrow.py
blob8526f00dae5aa4bc8799160be5b4921c21df2063
1 import struct
2 import numpy as np
3 import heapq
4 from itertools import count
7 INDEX_FILE = 'content/jpeg.index'
8 TEXT_INDEX_FILE = 'content/jpeg.index.txt'
9 FRAME_STRUCTURE = 'II48s8I'
10 BLOB_NAME='content/jpeg.blob'
13 def save_frames(frames, filename):
14 f = open(filename, 'w')
15 for frame in frames:
16 f.write(struct.pack(FRAME_STRUCTURE,
17 frame.glob_index,
18 frame.jpeg_len,
19 frame.summary,
20 *frame.successors
24 def save_frames_text(frames, filename):
25 f = open(filename, 'w')
26 for frame in frames:
27 print >> f, ("Frame:")
28 print >> f, (" index: %d" % frame.index)
29 print >> f, (" glob_index: %d" % frame.glob_index)
30 print >> f, (" jpeg_len: %d" % frame.jpeg_len)
31 print >> f, (" summary: %s" % list(frame.summary))
32 print >> f, (" successors: %s" % frame.successors)
35 class Frame:
36 def __init__(self, packed=None):
37 if packed is not None:
38 data = struct.unpack(FRAME_STRUCTURE, packed)
39 self.glob_index = data[0]
40 self.jpeg_len = data[1]
41 self.summary = data[2]
42 self.array = np.fromstring(data[2], dtype=np.uint8)
43 self.successors = list(data[3:])
44 self.next = self.successors[0]
48 def distance_gen(tail, heads):
49 s = tail.array
50 for h in heads:
51 if h is not tail.head:
52 yield((sum((s - h.array) ** 2), h))
55 def link_frames(frames):
56 tails = []
57 heads = []
58 isbreak = True
59 for f in frames:
60 if isbreak:
61 heads.append(f)
62 isbreak = False
63 if f.successors[0] == 0:
64 f.head = heads[-1]
65 tails.append(f)
66 isbreak = True
68 for f in heads + tails:
69 if not hasattr(f, 'array'):
70 f.array = np.fromstring(f.summary, dtype=np.uint8)
72 #print frames, heads, tails
74 for t in tails:
75 closest = [x[1] for x in heapq.nsmallest(7, distance_gen(t, heads), key=lambda x: x[0])]
76 t.successors = [0] + [x.index for x in closest]
77 print t.index, t.successors
79 return frames