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')
16 f
.write(struct
.pack(FRAME_STRUCTURE
,
24 def save_frames_text(frames
, filename
):
25 f
= open(filename
, 'w')
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
)
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
):
51 if h
is not tail
.head
:
52 yield((sum((s
- h
.array
) ** 2), h
))
55 def link_frames(frames
):
63 if f
.successors
[0] == 0:
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
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