# HG changeset patch # User Nicolas Saunier # Date 1530740379 14400 # Node ID b735895c8815adfa0d58e30c55c8af059595dec6 # Parent b1ba6d44fcb9e088c685c36b827be38985ec4d54 work in progress on process (learn motion patterns) diff -r b1ba6d44fcb9 -r b735895c8815 scripts/learn-motion-patterns.py --- a/scripts/learn-motion-patterns.py Wed Jul 04 16:21:09 2018 -0400 +++ b/scripts/learn-motion-patterns.py Wed Jul 04 17:39:39 2018 -0400 @@ -12,7 +12,7 @@ parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) parser.add_argument('-o', dest = 'outputPrototypeDatabaseFilename', help = 'name of the Sqlite database file to save prototypes') parser.add_argument('-i', dest = 'inputPrototypeDatabaseFilename', help = 'name of the Sqlite database file for prototypes to start the algorithm with') -parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to learn from', choices = ['objectfeature', 'feature', 'object'], default = 'objectfeatures') +parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to learn from', choices = ['objectfeature', 'feature', 'object'], default = 'objectfeature') parser.add_argument('--max-nobjectfeatures', dest = 'maxNObjectFeatures', help = 'maximum number of features per object to load', type = int, default = 1) parser.add_argument('-n', dest = 'nTrajectories', help = 'number of the object or feature trajectories to load', type = int, default = None) parser.add_argument('-e', dest = 'epsilon', help = 'distance for the similarity of trajectory points', type = float, required = True) @@ -24,6 +24,7 @@ parser.add_argument('--random', dest = 'randomInitialization', help = 'random initialization of clustering algorithm', action = 'store_true') parser.add_argument('--subsample', dest = 'positionSubsamplingRate', help = 'rate of position subsampling (1 every n positions)', type = int) parser.add_argument('--display', dest = 'display', help = 'display trajectories', action = 'store_true') +parser.add_argument('--similarities-filename', dest = 'similaritiesFilename', help = 'filename of the similarities') parser.add_argument('--save-similarities', dest = 'saveSimilarities', help = 'save computed similarities (in addition to prototypes)', action = 'store_true') parser.add_argument('--save-assignments', dest = 'saveAssignments', help = 'saves the assignments of the objects to the prototypes', action = 'store_true') parser.add_argument('--assign', dest = 'assign', help = 'assigns the objects to the prototypes and saves the assignments', action = 'store_true') @@ -40,17 +41,7 @@ # TODO add possibility to load all trajectories and use minclustersize # load trajectories to cluster or assign -if args.trajectoryType == 'objectfeature': - trajectoryType = 'feature' - objectFeatureNumbers = storage.loadObjectFeatureFrameNumbers(args.databaseFilename, objectNumbers = args.nTrajectories) - featureNumbers = [] - for numbers in objectFeatureNumbers.values(): - featureNumbers += numbers[:min(len(numbers), args.maxNObjectFeatures)] - objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, 'feature', objectNumbers = featureNumbers, timeStep = args.positionSubsamplingRate) -else: - trajectoryType = args.trajectoryType - objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate) - +objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, args.trajectoryType, args.nTrajectories, timeStep = args.positionSubsamplingRate) trajectories = [o.getPositions().asArray().T for o in objects] # load initial prototypes, if any @@ -66,9 +57,10 @@ initialPrototypeIndices = None lcss = utils.LCSS(metric = args.metric, epsilon = args.epsilon) -nTrajectories = len(trajectories) - -similarities = -np.ones((nTrajectories, nTrajectories)) +if args.similaritiesFilename is not None: + similarities = np.loadtxt(args.similaritiesFilename) +if args.similaritiesFilename is None or similarities.shape[0] != len(trajectories) or similarities.shape[1] != len(trajectories): + similarities = -np.ones((len(trajectories), len(trajectories))) similarityFunc = lambda x,y : lcss.computeNormalized(x, y) # the next line can be called again without reinitializing similarities if args.learn: @@ -90,6 +82,10 @@ if args.learn and not args.assign: prototypes = [] + if args.trajectoryType == 'objectfeature': + trajectoryType = 'feature' + else: + trajectoryType = args.trajectoryType for i in prototypeIndices: if i group videos by site (or by camera view? TODO add cameraviews) + # by default, load all objects, learn and then assign + objects = {siteId: [] for siteId in siteIds} + for vs in videoSequences: + print('Loading '+vs.getDatabaseFilename()) + objects[vs.cameraView.siteIdx] += storage.loadTrajectoriesFromSqlite(str(parentPath/vs.getDatabaseFilename()), args.trajectoryType, args.nTrajectories, timeStep = args.positionSubsamplingRate) + elif args.process == 'interaction': # safety analysis TODO make function in safety analysis script @@ -183,10 +212,6 @@ row.append(aggSpeeds) data.append(row) data = DataFrame(data, columns = headers) - if args.siteIds is None: - siteIds = set([vs.cameraView.siteIdx for vs in videoSequences]) - else: - siteIds = set(args.siteIds) if args.output == 'figure': for name in headers[4:]: plt.ioff()