def main(doTubeFormat, inFileName, outTrajIDfileName, featPath, nrTrajThresh4Tube=0): tube2trajIDs.check() outPath = os.path.dirname(outTrajIDfileName) if not os.path.exists( outPath ): os.makedirs(outPath) # read trajectory positions print '\tGet trajectory positions;', geoFeat = denseTraj.getFeatFromFileByName(featPath, 'geo') # if proposals are stored as Tubes already, read them if doTubeFormat: tubeProposals = TubeList() tubeProposals.readHDF5(inFileName) # initialize with a single name inHDF5fileClusts = ['tubes'] else: print '\tGet video dimensions;', vidInfo = denseTraj.getFeatFromFileByName(featPath, 'vidinfo') xmax = vidInfo[1] ymax = vidInfo[2] # input cluster file inHDF5fileClusts = h5py.File( inFileName, 'r') # go over all features totNrProposals = 0 for featName in inHDF5fileClusts: if not doTubeFormat: # get the clustered proposals for this feature type in SLINK pointer-representation dset = inHDF5fileClusts[featName] mergedTracks = dset[()] # convert pointer-representation to tube proposals if len(mergedTracks.shape) > 1 and mergedTracks.shape[0] > 1: tubeProposals = denseTraj.createProposals(mergedTracks, geoFeat, nrTrajThresh4Tube, xmax, ymax) nrProposals = len(tubeProposals) print '\tFeature: "%s", number of proposals: %d;' % (featName,nrProposals) # if ok if nrProposals > 0: # write as traj IDs if totNrProposals==0: outHDF5file = h5py.File( outTrajIDfileName, 'w', compression="gzip", compression_opts=9) for j in range(nrProposals): trajIDs = tubeProposals[j].tube2trajIDs(geoFeat) outHDF5file.create_dataset(str(totNrProposals), data=trajIDs) totNrProposals += 1 print '\tWrite trajectory IDs to', outTrajIDfileName outHDF5file.close()
def main(doTubeFormat, inFileName, outIoUfile, gtPath, nrTrajThresh4Tube=-1, featPath=''): # check if cython is used tubeIoU.check() # read ground truth tubes print '\tRead ground truth;', gtTubes = TubeList() gtTubes.readHDF5(gtPath) outPath = os.path.dirname(outIoUfile) if not os.path.exists( outPath ): os.makedirs(outPath) # if proposals are stored as Tubes already, read them if doTubeFormat: tubeProposals = TubeList() tubeProposals.readHDF5(inFileName) # initialize with a single name inHDF5fileClusts = ['tubes'] else: # otherwise, read trajectory positions and vidinfo to generate proposals later print '\tGet trajectory positions;', geoFeat = denseTraj.getFeatFromFileByName(featPath, 'geo') print '\tGet video dimensions;', vidInfo = denseTraj.getFeatFromFileByName(featPath, 'vidinfo') xmax = vidInfo[1] ymax = vidInfo[2] # input cluster file inHDF5fileClusts = h5py.File( inFileName, 'r') aboMax = [] # go over all features for featName in inHDF5fileClusts: if not doTubeFormat: # get the clustered proposals for this feature type in SLINK pointer-representation dset = inHDF5fileClusts[featName] mergedTracks = dset[()] # convert pointer-representation to tube proposals if len(mergedTracks.shape) > 1 and mergedTracks.shape[0] > 1: tubeProposals = denseTraj.createProposals(mergedTracks, geoFeat, nrTrajThresh4Tube, xmax, ymax) nrProposals = len(tubeProposals) print '\tFeature: "%s", number of proposals: %d;' % (featName,nrProposals), # if ok if nrProposals > 0: # get Intersection over Union scores aboMatCur = tubeProposals.computeTubeOverlap(gtTubes) # if first feature, write to new file otherwise append to existing file if aboMax == []: fileOut = open(outIoUfile, 'w') else: fileOut = open(outIoUfile, 'a') # write scores for prop in range(aboMatCur.shape[0]): for score in aboMatCur[prop,:]: fileOut.write('%f ' % score) fileOut.write('\n') fileOut.close() # keep track of the maximum score curMax = np.max(aboMatCur, axis=0) if aboMax == []: aboMax = curMax else: aboMax = np.maximum( aboMax, curMax) print 'Best IoUs:', curMax, '; Best so far:', aboMax print '\tWriting IoU scores to', outIoUfile if not doTubeFormat: inHDF5fileClusts.close()
def createProposals(mergedTracks, featSpat, cutOffLevel, xmax=np.inf, ymax=np.inf, regionSize=15): """create SingleTube proposals from the 'mergedTracks' clusters in trajectory clusters format""" # how many proposals nrProposals = mergedTracks.shape[0] # how many trajectory tracks nrTracks = featSpat.shape[0] zeroFound = True while zeroFound: # the list of tubes that will be created tubeLst = TubeList() # the list of tubes that will be sub-sampled selectedTubes = TubeList() # keep track of the nr of trajectories per merge nrTraj = np.zeros(nrProposals) nrTrajT1 = np.zeros(nrProposals) nrTrajT2 = np.zeros(nrProposals) for p in xrange(nrProposals): # clusterformat: a merged ID, and trackIDs t1 and t2 [mergedID, t1, t2] = mergedTracks[p][0:3] if t1 < nrTracks: # if a trackID is lower than nrTracks it is a trajectory tube1 = traj2tube(featSpat[t1,0], featSpat[t1,1:], xmax, ymax, regionSize) nrT1 = 1 else: # otherwise it is a previously merged proposal idx = int(t1) - nrTracks tube1 = tubeLst[idx] nrT1 = nrTraj[idx] if t2 < nrTracks: # if a trackID is lower than nrTracks it is a trajectory tube2 = traj2tube(featSpat[t2,0], featSpat[t2,1:], xmax, ymax, regionSize) nrT2 = 1 else: # otherwise it is a previously merged proposal idx = int(t2) - nrTracks tube2 = tubeLst[idx] nrT2 = nrTraj[idx] # the nr of trajectories of this merge nrTraj[p] = nrT1 + nrT2 nrTrajT1[p] = nrT1 nrTrajT2[p] = nrT2 # make a copy of t1 and merge it with t2 newTube = tube1.copy() newTube.merge(tube2) tubeLst.append(newTube) # if to subsample, add to the selected list if nrT1 >= cutOffLevel and nrT2 >= cutOffLevel: selectedTubes.append(newTube) if len(selectedTubes) == 0: cutOffLevel = cutOffLevel/2 if cutOffLevel <= 0: return selectedTubes else: return selectedTubes
def main(doTubeFormat, inFileName, outIoUfile, gtPath, nrTrajThresh4Tube=-1, featPath=''): # check if cython is used tubeIoU.check() # read ground truth tubes print '\tRead ground truth;', gtTubes = TubeList() gtTubes.readHDF5(gtPath) outPath = os.path.dirname(outIoUfile) if not os.path.exists(outPath): os.makedirs(outPath) # if proposals are stored as Tubes already, read them if doTubeFormat: tubeProposals = TubeList() tubeProposals.readHDF5(inFileName) # initialize with a single name inHDF5fileClusts = ['tubes'] else: # otherwise, read trajectory positions and vidinfo to generate proposals later print '\tGet trajectory positions;', geoFeat = denseTraj.getFeatFromFileByName(featPath, 'geo') print '\tGet video dimensions;', vidInfo = denseTraj.getFeatFromFileByName(featPath, 'vidinfo') xmax = vidInfo[1] ymax = vidInfo[2] # input cluster file inHDF5fileClusts = h5py.File(inFileName, 'r') aboMax = [] # go over all features for featName in inHDF5fileClusts: if not doTubeFormat: # get the clustered proposals for this feature type in SLINK pointer-representation dset = inHDF5fileClusts[featName] mergedTracks = dset[()] # convert pointer-representation to tube proposals if len(mergedTracks.shape) > 1 and mergedTracks.shape[0] > 1: tubeProposals = denseTraj.createProposals( mergedTracks, geoFeat, nrTrajThresh4Tube, xmax, ymax) nrProposals = len(tubeProposals) print '\tFeature: "%s", number of proposals: %d;' % (featName, nrProposals), # if ok if nrProposals > 0: # get Intersection over Union scores aboMatCur = tubeProposals.computeTubeOverlap(gtTubes) # if first feature, write to new file otherwise append to existing file if aboMax == []: fileOut = open(outIoUfile, 'w') else: fileOut = open(outIoUfile, 'a') # write scores for prop in range(aboMatCur.shape[0]): for score in aboMatCur[prop, :]: fileOut.write('%f ' % score) fileOut.write('\n') fileOut.close() # keep track of the maximum score curMax = np.max(aboMatCur, axis=0) if aboMax == []: aboMax = curMax else: aboMax = np.maximum(aboMax, curMax) print 'Best IoUs:', curMax, '; Best so far:', aboMax print '\tWriting IoU scores to', outIoUfile if not doTubeFormat: inHDF5fileClusts.close()
def createProposals(mergedTracks, featSpat, cutOffLevel, xmax=np.inf, ymax=np.inf, regionSize=15): """create SingleTube proposals from the 'mergedTracks' clusters in trajectory clusters format""" # how many proposals nrProposals = mergedTracks.shape[0] # how many trajectory tracks nrTracks = featSpat.shape[0] zeroFound = True while zeroFound: # the list of tubes that will be created tubeLst = TubeList() # the list of tubes that will be sub-sampled selectedTubes = TubeList() # keep track of the nr of trajectories per merge nrTraj = np.zeros(nrProposals) nrTrajT1 = np.zeros(nrProposals) nrTrajT2 = np.zeros(nrProposals) for p in xrange(nrProposals): # clusterformat: a merged ID, and trackIDs t1 and t2 [mergedID, t1, t2] = mergedTracks[p][0:3] if t1 < nrTracks: # if a trackID is lower than nrTracks it is a trajectory tube1 = traj2tube(featSpat[t1, 0], featSpat[t1, 1:], xmax, ymax, regionSize) nrT1 = 1 else: # otherwise it is a previously merged proposal idx = int(t1) - nrTracks tube1 = tubeLst[idx] nrT1 = nrTraj[idx] if t2 < nrTracks: # if a trackID is lower than nrTracks it is a trajectory tube2 = traj2tube(featSpat[t2, 0], featSpat[t2, 1:], xmax, ymax, regionSize) nrT2 = 1 else: # otherwise it is a previously merged proposal idx = int(t2) - nrTracks tube2 = tubeLst[idx] nrT2 = nrTraj[idx] # the nr of trajectories of this merge nrTraj[p] = nrT1 + nrT2 nrTrajT1[p] = nrT1 nrTrajT2[p] = nrT2 # make a copy of t1 and merge it with t2 newTube = tube1.copy() newTube.merge(tube2) tubeLst.append(newTube) # if to subsample, add to the selected list if nrT1 >= cutOffLevel and nrT2 >= cutOffLevel: selectedTubes.append(newTube) if len(selectedTubes) == 0: cutOffLevel = cutOffLevel / 2 if cutOffLevel <= 0: return selectedTubes else: return selectedTubes
def main(doTubeFormat, inFileName, outTrajIDfileName, featPath, nrTrajThresh4Tube=0): tube2trajIDs.check() outPath = os.path.dirname(outTrajIDfileName) if not os.path.exists(outPath): os.makedirs(outPath) # read trajectory positions print '\tGet trajectory positions;', geoFeat = denseTraj.getFeatFromFileByName(featPath, 'geo') # if proposals are stored as Tubes already, read them if doTubeFormat: tubeProposals = TubeList() tubeProposals.readHDF5(inFileName) # initialize with a single name inHDF5fileClusts = ['tubes'] else: print '\tGet video dimensions;', vidInfo = denseTraj.getFeatFromFileByName(featPath, 'vidinfo') xmax = vidInfo[1] ymax = vidInfo[2] # input cluster file inHDF5fileClusts = h5py.File(inFileName, 'r') # go over all features totNrProposals = 0 for featName in inHDF5fileClusts: if not doTubeFormat: # get the clustered proposals for this feature type in SLINK pointer-representation dset = inHDF5fileClusts[featName] mergedTracks = dset[()] # convert pointer-representation to tube proposals if len(mergedTracks.shape) > 1 and mergedTracks.shape[0] > 1: tubeProposals = denseTraj.createProposals( mergedTracks, geoFeat, nrTrajThresh4Tube, xmax, ymax) nrProposals = len(tubeProposals) print '\tFeature: "%s", number of proposals: %d;' % (featName, nrProposals) # if ok if nrProposals > 0: # write as traj IDs if totNrProposals == 0: outHDF5file = h5py.File(outTrajIDfileName, 'w', compression="gzip", compression_opts=9) for j in range(nrProposals): trajIDs = tubeProposals[j].tube2trajIDs(geoFeat) outHDF5file.create_dataset(str(totNrProposals), data=trajIDs) totNrProposals += 1 print '\tWrite trajectory IDs to', outTrajIDfileName outHDF5file.close()