Created
January 15, 2011 16:46
-
-
Save daien/781034 to your computer and use it in GitHub Desktop.
Pytables demo to store tracks of a video database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import numpy as np | |
import tables | |
# class containing infos to identify a track | |
class Track(tables.IsDescription): | |
id = tables.IntCol() # track ID (should be unique) | |
frame = tables.IntCol() # frame where the current track bounding box was found | |
ulx = tables.IntCol() # upper left corner's x coordinate of the bbox (upper left is origin (0,0) of frame) | |
uly = tables.IntCol() # upper left corner's y coordinate of the bbox | |
width = tables.IntCol() # width of the bbox | |
height = tables.IntCol() # height of the bbox | |
score = tables.FloatCol() # detector score of the bbox (if not found manually) | |
# create dummy data | |
np.random.seed(1) | |
movies = {} | |
movies['train'] = [ 'tr_movie%d' % i for i in range(1000) ] | |
movies['test'] = [ 'te_movie%d' % i for i in range(1000) ] | |
sids = {} | |
for s in "train test".split(): | |
sids[s] = {} | |
for m in movies[s]: | |
sids[s][m] = ( '%s-frames-%d-%d' % \ | |
(m, np.random.randint(100), np.random.randint(100, 200)) | |
for i in range(10) ) | |
print "beginning writing .h5 file" | |
filename = 'test.h5' | |
# Open a file in "w"rite mode | |
h5file = tables.openFile(filename, mode = "w", title = "hollywood2 dataset") | |
# Create the tracks group at the root | |
tracks_g = h5file.createGroup(h5file.root, 'tracks', 'Tracks for the dataset') | |
# Create the training and test groups | |
trt_g = h5file.createGroup(tracks_g, 'train', 'Tracks for the training samples') | |
tet_g = h5file.createGroup(tracks_g, 'test', 'Tracks for the test samples') | |
# Create the automatic and ground truth groups of tracks in each case | |
gnames = ['autom', 'gt'] | |
gtitles = ['Automatically detected tracks', 'Ground truth tracks'] | |
for s in "train test".split(): | |
for gname, gtitle in zip(gnames, gtitles): | |
ttr = h5file.createGroup("/tracks/%s" % s, gname, gtitle) | |
# Create the per-movie tables of tracks | |
tid = -1 | |
for s in "train test".split(): | |
for t in "autom gt".split(): | |
for m in movies[s]: | |
pg = "/tracks/%s/%s" % (s,t) | |
table = h5file.createTable(pg, m, Track, "Movie %s" % m) | |
trow = table.row | |
# add the sample tracks | |
for sid in sids[s][m]: | |
# add multiple tracks | |
for atn in range(10): | |
tid += 1 | |
trow['id'] = tid | |
trow['frame'] = np.random.randint(100) | |
trow['ulx'] = np.random.randint(200) | |
trow['uly'] = np.random.randint(100) | |
trow['width'] = np.random.randint(120) | |
trow['height'] = np.random.randint(140) | |
trow['score'] = np.random.random() | |
trow.append() | |
table.flush() | |
# Close (and flush) the file | |
h5file.close() | |
print ".h5 written" | |
# open the file and display its contents | |
ff = tables.openFile("test.h5") | |
#ff # prints all infos in interactive session | |
print ff.root | |
print ff.root.tracks | |
print ff.root.tracks.train | |
print ff.root.tracks.train.autom | |
print ff.root.tracks.train.getattr('autom') | |
mv1 = ff.root.tracks.train.autom.tr_movie1 | |
print mv1 | |
print mv1[:3] | |
print mv1.cols.frame[:3] | |
print [ row['frame'] for row in mv1.where('(score >= 0.5) & (width > 25)') ] | |
ff.close() | |
raw_input() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment