Mercurial > hg > nsaunier > traffic-intelligence
comparison scripts/learn-motion-patterns.py @ 979:cc89267b5ff9
work on learning and assigning
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Mon, 19 Feb 2018 10:47:19 -0500 |
| parents | 989917b1ed85 |
| children | 23f98ebb113f |
comparison
equal
deleted
inserted
replaced
| 978:184f1dd307f9 | 979:cc89267b5ff9 |
|---|---|
| 40 # TODO add possibility to cluster with velocities | 40 # TODO add possibility to cluster with velocities |
| 41 # TODO add possibilite to load all trajectories and use minclustersize | 41 # TODO add possibilite to load all trajectories and use minclustersize |
| 42 # save the objects that match the prototypes | 42 # save the objects that match the prototypes |
| 43 # write an assignment function for objects | 43 # write an assignment function for objects |
| 44 | 44 |
| 45 trajectoryType = args.trajectoryType | 45 # load trajectories to cluster or assign |
| 46 prototypeType = args.trajectoryType | |
| 47 if args.trajectoryType == 'objectfeatures': | 46 if args.trajectoryType == 'objectfeatures': |
| 48 trajectoryType = 'object' | 47 trajectoryType = 'feature' |
| 49 prototypeType = 'feature' | |
| 50 | |
| 51 if args.trajectoryType == 'objectfeatures': | |
| 52 objectFeatureNumbers = storage.loadObjectFeatureFrameNumbers(args.databaseFilename, objectNumbers = args.nTrajectories) | 48 objectFeatureNumbers = storage.loadObjectFeatureFrameNumbers(args.databaseFilename, objectNumbers = args.nTrajectories) |
| 53 featureNumbers = [] | 49 featureNumbers = [] |
| 54 for numbers in objectFeatureNumbers.values(): | 50 for numbers in objectFeatureNumbers.values(): |
| 55 featureNumbers += numbers[:min(len(numbers), args.maxNObjectFeatures)] | 51 featureNumbers += numbers[:min(len(numbers), args.maxNObjectFeatures)] |
| 56 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, 'feature', objectNumbers = featureNumbers, timeStep = args.positionSubsamplingRate) | 52 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, 'feature', objectNumbers = featureNumbers, timeStep = args.positionSubsamplingRate) |
| 57 else: | 53 else: |
| 58 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, withFeatures = (args.trajectoryType == 'objectfeatures'), objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate) | 54 trajectoryType = args.trajectoryType |
| 55 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, trajectoryType, objectNumbers = args.nTrajectories, timeStep = args.positionSubsamplingRate) | |
| 59 | 56 |
| 60 | |
| 61 trajectories = [o.getPositions().asArray().T for o in objects] | 57 trajectories = [o.getPositions().asArray().T for o in objects] |
| 58 | |
| 59 # load initial prototypes, if any | |
| 62 if args.inputPrototypeDatabaseFilename is not None: | 60 if args.inputPrototypeDatabaseFilename is not None: |
| 63 initialPrototypes = storage.loadPrototypesFromSqlite(args.inputPrototypeDatabaseFilename, True) | 61 initialPrototypes = storage.loadPrototypesFromSqlite(args.inputPrototypeDatabaseFilename, True) |
| 64 trajectories = [p.getMovingObject().getPositions().asArray().T for p in initialPrototypes]+trajectories | 62 trajectories = [p.getMovingObject().getPositions().asArray().T for p in initialPrototypes]+trajectories |
| 65 if len(initialPrototypes) > 0: | 63 if len(initialPrototypes) > 0: |
| 66 initialPrototypeIndices = range(len(initialPrototypes)) | 64 initialPrototypeIndices = range(len(initialPrototypes)) |
| 97 nMatchings = 0 | 95 nMatchings = 0 |
| 98 if i<len(initialPrototypes): | 96 if i<len(initialPrototypes): |
| 99 initialPrototypes[i].nMatchings += nMatchings | 97 initialPrototypes[i].nMatchings += nMatchings |
| 100 prototypes.append(initialPrototypes[i]) | 98 prototypes.append(initialPrototypes[i]) |
| 101 else: | 99 else: |
| 102 prototypes.append(moving.Prototype(args.databaseFilename, objects[i-len(initialPrototypes)].getNum(), prototypeType, nMatchings)) | 100 prototypes.append(moving.Prototype(args.databaseFilename, objects[i-len(initialPrototypes)].getNum(), trajectoryType, nMatchings)) |
| 103 | 101 |
| 104 if args.outputPrototypeDatabaseFilename is None: | 102 if args.outputPrototypeDatabaseFilename is None: |
| 105 outputPrototypeDatabaseFilename = args.databaseFilename | 103 outputPrototypeDatabaseFilename = args.databaseFilename |
| 106 else: | 104 else: |
| 107 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename | 105 outputPrototypeDatabaseFilename = args.outputPrototypeDatabaseFilename |
| 112 if args.saveSimilarities: | 110 if args.saveSimilarities: |
| 113 # todo save trajectories and prototypes | 111 # todo save trajectories and prototypes |
| 114 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') | 112 np.savetxt(utils.removeExtension(args.databaseFilename)+'-prototype-similarities.txt.gz', similarities, '%.4f') |
| 115 | 113 |
| 116 labelsToProtoIndices = {protoId: i for i, protoId in enumerate(prototypeIndices)} | 114 labelsToProtoIndices = {protoId: i for i, protoId in enumerate(prototypeIndices)} |
| 117 if args.assign and args.saveMatches: # or args.assign | 115 if args.assign and args.saveMatches: |
| 118 # save in the db that contained originally the data | 116 storage.savePrototypeAssignmentsToSqlite(args.databaseFilename, objects, trajectoryType, [labelsToProtoIndices[l] for l in labels], prototypes) |
| 119 # retirer les assignations anterieures? | |
| 120 storage.savePrototypeAssignmentsToSqlite(args.databaseFilename, objects, [labelsToProtoIndices[l] for l in labels], prototypes) | |
| 121 | 117 |
| 122 if args.display and args.assign: | 118 if args.display and args.assign: |
| 123 from matplotlib.pyplot import figure, show, axis | 119 from matplotlib.pyplot import figure, show, axis |
| 124 figure() | 120 figure() |
| 125 for i,o in enumerate(objects): | 121 for i,o in enumerate(objects): |
