[ Top page ]

« Method for packing 8-bit (int8) arrays into GPU memory by Theano | Main | A program that removes bottom part of an MPEG video »

Computer Vision

MPEG video file generation from Caltech dataset

The Caltech pedestrian dataset contains a special format videos. The following program converts them to normal video format files. The extension of the generated file is ".avi", but ".mpg" can also be used (can be rewritten).

The usage when the program below is stored in file "caltechVideo.py" is as follows. This invocation converts a video file 'V000' in directory 'set00' to an AVI file in directory 'Images'.

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)
Keywords:

TrackBack

TrackBack URL for this entry:
https://www.kanadas.com/mt/mt-tb.cgi/7117

Post a comment

About

This page contains a single entry from the blog posted on January 26, 2016 12:52 AM.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.
Powered by Movable Type