Caltech 歩行者データセットは特殊なフォーマットになっている. これを通常のビデオ・ファイル に変換するプログラムをのせる. ファイル名につく拡張子は .avi となっているが,.mpg にすることもできる.
下記のプログラムを caltechVideo.py というファイルに格納したときの使用例はつぎのとおり. これによって 'set00' というディレクトリにある 'V000' というビデオが変換され,'Images' というディレクトリに AVI ファイルがつくられる.
import caltechVideo caltechVideo.genVideo('set00', 'V000')
### Generate an AVI file from Caltech Dataset ###
import cv2, cv, time, struct
import Image, utils, random, os, gzip
import numpy as np
def createPedestrianWriter(outFile, width, height):
writer = cv2.VideoWriter()
writer.open(outFile, cv.CV_FOURCC('P','I','M','1'), fps = 30,
frameSize = (width, height), isColor = False)
return writer
def genVideo1(inFile, outFile):
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]}
print('From {} to {}'.format(inFile, outFile))
params = read_header(open(inFile, 'rb'))
bytes = open(inFile, 'rb').read()
writer = createPedestrianWriter(outFile, 640, 480)
# this is freaking magic, but it works
extra = 8
s = 1024
seek = [0]*(params['num_frames']+1)
seek[0] = 1024
# print("Params:{0}".format(params)) #!!!!!!
for i in range(0, params['num_frames']-1): ##!!! why -1?
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]
# print("Frame:{0} seek:{1}".format(i, s))
I = bytes[s+4:s+nbytes]
tmp_file = '/tmp/img{0}_{1}.jpg'.format(os.getpid(), i)
open(tmp_file, 'wb+').write(I)
img = cv2.cvtColor(cv2.imread(tmp_file), cv2.COLOR_BGR2GRAY)
writer.write(img)
return
def genVideo(dir, name):
inFile = dir + '/' + name + '.seq'
outFile = 'Images/' + dir + '_' + name + '.avi'
genVideo1(inFile, outFile)
