Skip to content

Instantly share code, notes, and snippets.

@psycharo-zz
Created May 15, 2014 16:11
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psycharo-zz/7e6422a491d93e1e3219 to your computer and use it in GitHub Desktop.
Save psycharo-zz/7e6422a491d93e1e3219 to your computer and use it in GitHub Desktop.
reading .seq files from caltech pedestrian dataset
def read_seq(path):
def read_header(ifile):
feed = ifile.read(4)
norpix = ifile.read(24)
version = struct.unpack('@i', ifile.read(4))
length = struct.unpack('@i', ifile.read(4))
assert(length != 1024)
descr = ifile.read(512)
params = [struct.unpack('@i', ifile.read(4))[0] for i in range(0,9)]
fps = struct.unpack('@d', ifile.read(8))
# skipping the rest
ifile.read(432)
image_ext = {100:'raw', 102:'jpg',201:'jpg',1:'png',2:'png'}
return {'w':params[0],'h':params[1],
'bdepth':params[2],
'ext':image_ext[params[5]],
'format':params[5],
'size':params[4],
'true_size':params[8],
'num_frames':params[6]}
ifile = open(path, 'rb')
params = read_header(ifile)
bytes = open(path, 'rb').read()
# this is freaking magic, but it works
extra = 8
s = 1024
seek = [0]*(params['num_frames']+1)
seek[0] = 1024
images = []
for i in range(0, params['num_frames']):
tmp = struct.unpack_from('@I', bytes[s:s+4])[0]
s = seek[i] + tmp + extra
if i == 0:
val = struct.unpack_from('@B', bytes[s:s+1])[0]
if val != 0:
s -= 4
else:
extra += 8
s += 8
seek[i+1] = s
nbytes = struct.unpack_from('@i', bytes[s:s+4])[0]
I = bytes[s+4:s+nbytes]
tmp_file = '/tmp/img%d.jpg' % i
open(tmp_file, 'wb+').write(I)
img = cv2.imread(tmp_file)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
images.append(img)
return images
@anupamsobti
Copy link

Does this work?
I got the following error while I was trying this:

    nbytes = struct.unpack_from('@i', bytes[s:s+4])[0]
struct.error: unpack_from requires a buffer of at least 4 bytes

@mckees
Copy link

mckees commented May 22, 2017

@anupamsobti it looks like the last bytes that it tries to unpack are blank. I just got it working by changing line 35.
for i in range(0, params['num_frames']-1):

I do have concerns that this might affect how annotations are read, but I haven't gotten there yet.

@PyPatel
Copy link

PyPatel commented Dec 15, 2017

What all packages are required to run this Code? I am not getting any output now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment